Skip to content

Commit 697242f

Browse files
committed
add match_entry
1 parent cf2b36d commit 697242f

File tree

7 files changed

+71
-18
lines changed

7 files changed

+71
-18
lines changed

ext/rr3/match_entry.c

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <r3/r3.h>
2+
#include "match_entry.h"
3+
4+
extern VALUE rb_mRr3;
5+
VALUE rb_cMatchEntry;
6+
7+
void init_match_entry(){
8+
rb_cMatchEntry = rb_define_class_under(rb_mRr3, "MatchEntry", rb_cObject);
9+
rb_define_method(rb_cMatchEntry, "initialize", rb_match_entry_initialize, -1);
10+
rb_define_attr(rb_cMatchEntry, "route_data", 1, 0);
11+
rb_define_attr(rb_cMatchEntry, "path", 1, 0);
12+
rb_define_attr(rb_cMatchEntry, "request_method", 1, 0);
13+
rb_define_attr(rb_cMatchEntry, "host", 1, 0);
14+
rb_define_attr(rb_cMatchEntry, "remote_addr", 1, 0);
15+
rb_define_attr(rb_cMatchEntry, "vars", 1, 0);
16+
}
17+
18+
static VALUE rb_match_entry_initialize(int argc, VALUE *argv, VALUE self){
19+
VALUE opt;
20+
rb_scan_args(argc, argv, "0:", &opt);
21+
if(!NIL_P(opt)) rb_hash_foreach(opt, mass_assign_i, self);
22+
return self;
23+
}
24+
25+
static int mass_assign_i(VALUE key, VALUE value, VALUE self){
26+
VALUE key_str = rb_id2str(SYM2ID(key));
27+
char iv_str[100] = "@";
28+
strncat(iv_str, RSTRING_PTR(key_str), RSTRING_LEN(key_str));
29+
rb_iv_set(self, iv_str, value);
30+
return ST_CONTINUE;
31+
}

ext/rr3/match_entry.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef MATCH_ENTRY
2+
#define MATCH_ENTRY 1
3+
4+
#include <r3/r3.h>
5+
#include "ruby.h"
6+
7+
void init_match_entry();
8+
9+
// def initialize(options)
10+
static VALUE rb_match_entry_initialize(int argc, VALUE *argv, VALUE self);
11+
12+
static int mass_assign_i(VALUE key, VALUE value, VALUE self);
13+
14+
#endif /* MATCH_ENTRY */

ext/rr3/rr3.c

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#include "rr3.h"
22
#include "tree.h"
3+
#include "match_entry.h"
34

45
VALUE rb_mRr3;
56

67
void Init_rr3(void){
78
rb_mRr3 = rb_define_module("Rr3");
9+
init_match_entry();
810
init_tree();
911
}

ext/rr3/tree.c

+11-11
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,20 @@ VALUE rb_cTree;
66

77
void init_tree(){
88
rb_cTree = rb_define_class_under(rb_mRr3, "Tree", rb_cObject);
9-
rb_define_attr(rb_cTree, "root", 1, 0);
10-
rb_define_method(rb_cTree, "initialize", rb_initialize, 1);
11-
rb_define_method(rb_cTree, "insert_path", rb_insert_path, -1);
12-
rb_define_method(rb_cTree, "compile!", rb_compile, 0);
13-
rb_define_method(rb_cTree, "match", rb_match, 1);
14-
rb_define_method(rb_cTree, "dump", rb_dump, 1);
9+
rb_define_method(rb_cTree, "initialize", rb_tree_initialize, 1);
10+
rb_define_method(rb_cTree, "insert_path", rb_tree_insert_path, -1);
11+
rb_define_method(rb_cTree, "compile!", rb_tree_compile, 0);
12+
rb_define_method(rb_cTree, "match", rb_tree_match, 1);
13+
rb_define_method(rb_cTree, "dump", rb_tree_dump, 1);
1514
}
1615

17-
static VALUE rb_initialize(VALUE self, VALUE size){
16+
static VALUE rb_tree_initialize(VALUE self, VALUE size){
1817
node *n = r3_tree_create(FIX2INT(size));
1918
rb_ivar_set(self, rb_intern("@root"), Data_Wrap_Struct(rb_cObject, NULL, release, n));
2019
return self;
2120
}
2221

23-
static VALUE rb_insert_path(int argc, VALUE *argv, VALUE self){
22+
static VALUE rb_tree_insert_path(int argc, VALUE *argv, VALUE self){
2423
VALUE path, *data;
2524
Data_Make_Struct(rb_cObject, VALUE, NULL, -1, data);
2625
rb_scan_args(argc, argv, "11", &path, data);
@@ -33,7 +32,7 @@ static VALUE rb_insert_path(int argc, VALUE *argv, VALUE self){
3332
return Qnil;
3433
}
3534

36-
static VALUE rb_compile(VALUE self){
35+
static VALUE rb_tree_compile(VALUE self){
3736
char *errstr = NULL;
3837
if(r3_tree_compile(root(self), &errstr) != 0){
3938
rb_raise(rb_eRuntimeError, "%s", errstr);
@@ -42,12 +41,13 @@ static VALUE rb_compile(VALUE self){
4241
return Qnil;
4342
}
4443

45-
static VALUE rb_match(VALUE self, VALUE path){
44+
static VALUE rb_tree_match(VALUE self, VALUE path){
45+
// match_entry *entry = match_entry_createl(RSTRING_PTR(path), RSTRING_LEN(path));
4646
node *matched_node = r3_tree_matchl(root(self), RSTRING_PTR(path), RSTRING_LEN(path), NULL); // TODO: support entry
4747
return matched_node ? *((VALUE*) matched_node->data) : Qfalse;
4848
}
4949

50-
static VALUE rb_dump(VALUE self, VALUE level){
50+
static VALUE rb_tree_dump(VALUE self, VALUE level){
5151
r3_tree_dump(root(self), FIX2INT(level));
5252
return Qnil;
5353
}

ext/rr3/tree.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
#define RR3_TREE 1
33

44
#include <r3/r3.h>
5-
#include <ruby.h>
5+
#include "ruby.h"
66

77
void init_tree();
88

99
// def initialize(size)
10-
static VALUE rb_initialize(VALUE self, VALUE size);
10+
static VALUE rb_tree_initialize(VALUE self, VALUE size);
1111
// def insert_path(path, data = nil)
12-
static VALUE rb_insert_path(int argc, VALUE *argv, VALUE self);
12+
static VALUE rb_tree_insert_path(int argc, VALUE *argv, VALUE self);
1313
// def compile()
14-
static VALUE rb_compile(VALUE self);
14+
static VALUE rb_tree_compile(VALUE self);
1515
// def compile(path)
16-
static VALUE rb_match(VALUE self, VALUE path);
16+
static VALUE rb_tree_match(VALUE self, VALUE path);
1717
// def dump(level)
18-
static VALUE rb_dump(VALUE self, VALUE level);
18+
static VALUE rb_tree_dump(VALUE self, VALUE level);
1919

2020
static void release(node *n);
2121
inline static node* root(VALUE self);

spec/match_entry_spec.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'spec_helper'
2+
3+
describe Rr3::MatchEntry do
4+
it '#initialize' do
5+
x = Rr3::MatchEntry.new path: 'asdf', a: 123
6+
end
7+
end

spec/rr3_spec.rb

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
$: << File.expand_path('../../ext', __FILE__)
21
require 'spec_helper'
32

43
describe Rr3 do

0 commit comments

Comments
 (0)