binding_ninja 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +2 -1
- data/README.md +26 -0
- data/ext/binding_ninja/binding_ninja.c +66 -16
- data/lib/binding_ninja/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3d3854a6100c403b0db08ad5f0c4c6c4e4b2c527
|
4
|
+
data.tar.gz: b2b6748aa0b7c8195c94f69c5d80798908d87d69
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e62aebf9862aa927ef2112aa8f6abac2f4b85ac93bef9a4c15417e2bc720000ee21051c033acfc17a0585d302949d9aed8c56352e9df33aced3741a4efb41776
|
7
|
+
data.tar.gz: af4d430af39734eaa1f63205aa4f115dc258eb4a724c2e662828d381d892056b29d3304ed1f3c626fb9baefc09058467202885029c1d40e826a7fe546aee3dba
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# BindingNinja
|
2
|
+
[](https://travis-ci.org/joker1007/binding_ninja)
|
2
3
|
|
3
4
|
This is method wrapper for passing binding of method caller implcitly.
|
4
5
|
|
@@ -30,14 +31,39 @@ class Foo
|
|
30
31
|
p arg2
|
31
32
|
end
|
32
33
|
auto_inject_binding :foo
|
34
|
+
|
35
|
+
def foo2(binding, arg1, arg2)
|
36
|
+
p binding
|
37
|
+
p arg1
|
38
|
+
p arg2
|
39
|
+
end
|
40
|
+
auto_inject_binding :foo2, if: ENV["ENABLE_BINDING_NINJA"]
|
41
|
+
# or
|
42
|
+
auto_inject_binding :foo2, if: ->(obj) { obj.enable_auto_inject_binding? }
|
43
|
+
# or
|
44
|
+
auto_inject_binding :foo2, if: :enable_auto_inject_binding?
|
45
|
+
|
46
|
+
def enable_auto_inject_binding?
|
47
|
+
true
|
48
|
+
end
|
33
49
|
end
|
34
50
|
|
35
51
|
Foo.new.foo(1, 2)
|
36
52
|
# => <Binding of toplevel>
|
37
53
|
# => 1
|
38
54
|
# => 2
|
55
|
+
|
56
|
+
# if ENABLE_BINDING_NINJA environment variable is nil or false,
|
57
|
+
# binding arguments is nil.
|
58
|
+
Foo.new.foo2(1, 2)
|
59
|
+
# => nil
|
60
|
+
# => 1
|
61
|
+
# => 2
|
39
62
|
```
|
40
63
|
|
64
|
+
`:if` option can accept Proc object and Symbol object.
|
65
|
+
If option accepts a proc or symbol, uses result of evaluating the proc or method named by the symbol.
|
66
|
+
|
41
67
|
## Development
|
42
68
|
|
43
69
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -3,30 +3,55 @@
|
|
3
3
|
VALUE rb_mBindingNinja;
|
4
4
|
static VALUE auto_inject_binding_invoke(int argc, VALUE *argv, VALUE self);
|
5
5
|
|
6
|
+
static ID options_id;
|
7
|
+
|
6
8
|
static VALUE
|
7
|
-
auto_inject_binding(VALUE
|
9
|
+
auto_inject_binding(int argc, VALUE *argv, VALUE mod)
|
8
10
|
{
|
9
11
|
ID mid;
|
10
|
-
|
12
|
+
static ID extensions_id;
|
13
|
+
static ID keyword_ids[1];
|
14
|
+
VALUE extensions, ext_mod, method_sym, options, opt, cond;
|
15
|
+
|
16
|
+
if (!keyword_ids[0]) {
|
17
|
+
keyword_ids[0] = rb_intern("if");
|
18
|
+
}
|
19
|
+
|
20
|
+
if (!extensions_id) {
|
21
|
+
extensions_id = rb_intern("@auto_inject_binding_extensions");
|
22
|
+
}
|
23
|
+
|
24
|
+
if (rb_cvar_defined(mod, options_id)) {
|
25
|
+
options = rb_cvar_get(mod, options_id);
|
26
|
+
} else {
|
27
|
+
options = rb_hash_new();
|
28
|
+
rb_cvar_set(mod, options_id, options);
|
29
|
+
}
|
30
|
+
|
31
|
+
rb_scan_args(argc, argv, "1:", &method_sym, &opt);
|
32
|
+
if (!NIL_P(opt)) {
|
33
|
+
rb_get_kwargs(opt, keyword_ids, 0, 1, &cond);
|
34
|
+
if (cond != Qundef) {
|
35
|
+
rb_hash_aset(options, method_sym, cond);
|
36
|
+
}
|
37
|
+
}
|
11
38
|
|
12
39
|
mid = SYM2ID(method_sym);
|
13
|
-
|
14
|
-
if (abs(arity) < 1) {
|
40
|
+
if (abs(rb_mod_method_arity(mod, mid)) < 1) {
|
15
41
|
rb_raise(rb_eArgError, "target method receives 1 or more arguments");
|
16
42
|
}
|
17
43
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
if (ext_mod == Qnil) {
|
44
|
+
extensions = rb_ivar_get(rb_mBindingNinja, extensions_id);
|
45
|
+
ext_mod = rb_hash_aref(extensions, mod);
|
46
|
+
if (NIL_P(ext_mod)) {
|
22
47
|
ext_mod = rb_module_new();
|
23
|
-
rb_hash_aset(extensions,
|
48
|
+
rb_hash_aset(extensions, mod, ext_mod);
|
24
49
|
}
|
50
|
+
|
25
51
|
if (rb_mod_include_p(mod, ext_mod) == Qfalse) {
|
26
52
|
rb_prepend_module(mod, ext_mod);
|
27
53
|
}
|
28
|
-
|
29
|
-
rb_define_method_id(ext_mod, SYM2ID(method_sym), auto_inject_binding_invoke, -1);
|
54
|
+
rb_define_method_id(ext_mod, mid, auto_inject_binding_invoke, -1);
|
30
55
|
|
31
56
|
return method_sym;
|
32
57
|
}
|
@@ -34,12 +59,36 @@ auto_inject_binding(VALUE mod, VALUE method_sym)
|
|
34
59
|
static VALUE
|
35
60
|
auto_inject_binding_invoke(int argc, VALUE *argv, VALUE self)
|
36
61
|
{
|
37
|
-
VALUE binding, args_ary;
|
62
|
+
VALUE method_sym, options, binding, args_ary, cond;
|
63
|
+
static VALUE dummy_proc_args, dummy_method_arg[0];
|
38
64
|
|
39
|
-
|
40
|
-
|
41
|
-
|
65
|
+
if (!dummy_proc_args) {
|
66
|
+
dummy_proc_args = rb_ary_new();
|
67
|
+
rb_obj_freeze(dummy_proc_args);
|
68
|
+
}
|
69
|
+
|
70
|
+
method_sym = ID2SYM(rb_frame_this_func());
|
71
|
+
options = rb_cvar_get(CLASS_OF(self), options_id);
|
42
72
|
|
73
|
+
cond = rb_hash_lookup2(options, method_sym, Qtrue);
|
74
|
+
|
75
|
+
if (rb_obj_is_proc(cond)) {
|
76
|
+
if (abs(rb_proc_arity(cond)) > 0) {
|
77
|
+
cond = rb_proc_call(cond, rb_ary_new_from_args(1, self));
|
78
|
+
} else {
|
79
|
+
cond = rb_proc_call(cond, dummy_proc_args);
|
80
|
+
}
|
81
|
+
} else if (SYMBOL_P(cond)) {
|
82
|
+
cond = rb_method_call(0, dummy_method_arg, rb_obj_method(self, cond));
|
83
|
+
}
|
84
|
+
|
85
|
+
args_ary = rb_ary_new_from_values(argc, argv);
|
86
|
+
if (RTEST(cond)) {
|
87
|
+
binding = rb_binding_new();
|
88
|
+
rb_ary_unshift(args_ary, binding);
|
89
|
+
} else {
|
90
|
+
rb_ary_unshift(args_ary, Qnil);
|
91
|
+
}
|
43
92
|
return rb_call_super(argc + 1, RARRAY_CONST_PTR(args_ary));
|
44
93
|
}
|
45
94
|
|
@@ -47,6 +96,7 @@ void
|
|
47
96
|
Init_binding_ninja(void)
|
48
97
|
{
|
49
98
|
rb_mBindingNinja = rb_define_module("BindingNinja");
|
99
|
+
options_id = rb_intern("@@__auto_inject_binding_options");
|
50
100
|
rb_ivar_set(rb_mBindingNinja, rb_intern("@auto_inject_binding_extensions"), rb_hash_new());
|
51
|
-
rb_define_private_method(rb_mBindingNinja, "auto_inject_binding", auto_inject_binding, 1);
|
101
|
+
rb_define_private_method(rb_mBindingNinja, "auto_inject_binding", auto_inject_binding, -1);
|
52
102
|
}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: binding_ninja
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- joker1007
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-09-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|