sticky_blox 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/sticky_blox/behavior.rb +34 -18
- data/spec/sticky_blox_spec.rb +46 -6
- data/sticky_blox.gemspec +1 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.2.0
|
data/lib/sticky_blox/behavior.rb
CHANGED
@@ -4,11 +4,34 @@ module StickyBlox
|
|
4
4
|
def self.included(base)
|
5
5
|
base.extend ClassMethods
|
6
6
|
end
|
7
|
+
|
8
|
+
class BindingArray < Array
|
9
|
+
|
10
|
+
def bind(object)
|
11
|
+
@binding = object
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(*args)
|
16
|
+
value = @binding
|
17
|
+
self.reject{|e| e.nil?}.each do |blk|
|
18
|
+
value = blk.bind(value).call(*args)
|
19
|
+
end
|
20
|
+
value
|
21
|
+
end
|
22
|
+
end
|
7
23
|
|
8
24
|
module ClassMethods
|
9
25
|
def my_meta_class
|
10
26
|
class << self; self; end
|
11
27
|
end
|
28
|
+
|
29
|
+
def stuck_methods
|
30
|
+
my_meta_class.instance_eval do
|
31
|
+
@stuck_methods ||= {}
|
32
|
+
@stuck_methods
|
33
|
+
end
|
34
|
+
end
|
12
35
|
|
13
36
|
# Defines methods on the class identified by "binding"
|
14
37
|
# for each method in @stuck_methods that will call
|
@@ -17,10 +40,10 @@ module StickyBlox
|
|
17
40
|
def stick_to( binding )
|
18
41
|
my_meta_class.instance_eval do
|
19
42
|
clz = self
|
20
|
-
@stuck_methods.
|
43
|
+
@stuck_methods.each_pair do |method_name, binding_array|
|
21
44
|
binding.class_eval do
|
22
45
|
define_method method_name do | *args |
|
23
|
-
|
46
|
+
binding_array.bind(self).call(*args)
|
24
47
|
end
|
25
48
|
end
|
26
49
|
end
|
@@ -48,27 +71,20 @@ module StickyBlox
|
|
48
71
|
end
|
49
72
|
|
50
73
|
def unstick_all_from( binding, ignore_errors=true )
|
51
|
-
unstick_from( binding => my_meta_class.instance_variable_get("@stuck_methods"), :ignore_errors => ignore_errors )
|
74
|
+
unstick_from( binding => my_meta_class.instance_variable_get("@stuck_methods").keys, :ignore_errors => ignore_errors )
|
52
75
|
end
|
53
76
|
|
54
|
-
def stick
|
55
|
-
|
56
|
-
|
57
|
-
|
77
|
+
def stick(name, replace=false, &block)
|
78
|
+
my_methods = stuck_methods
|
79
|
+
if replace && my_methods.has_key?(name)
|
80
|
+
my_methods.delete(name)
|
58
81
|
end
|
59
|
-
|
60
|
-
clz = self.class
|
61
|
-
clz.class_eval do
|
62
|
-
# define_method "#{name}=" do |arg|
|
63
|
-
# my_meta_class.instance_variable_get("@stuck_methods")[name] = block
|
64
|
-
# end
|
82
|
+
(my_methods[name] ||= BindingArray.new) << block
|
65
83
|
|
66
|
-
|
67
|
-
|
68
|
-
# my_meta_class.instance_variable_get("@stuck_methods")[meth]
|
69
|
-
# end
|
84
|
+
my_methods = my_methods[name]
|
85
|
+
self.class.class_eval do
|
70
86
|
define_method name do
|
71
|
-
|
87
|
+
my_methods
|
72
88
|
end
|
73
89
|
end
|
74
90
|
end
|
data/spec/sticky_blox_spec.rb
CHANGED
@@ -50,18 +50,17 @@ end
|
|
50
50
|
|
51
51
|
describe "Overriding stuck methods" do
|
52
52
|
before :all do
|
53
|
-
@original = Spearmint.chew
|
53
|
+
@original = Spearmint.chew.dup
|
54
54
|
Spearmint.class_eval do
|
55
|
-
stick :chew do
|
55
|
+
stick :chew, true do
|
56
56
|
self.upcase
|
57
57
|
end
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
61
|
after :all do
|
62
|
-
Spearmint.
|
63
|
-
|
64
|
-
end
|
62
|
+
Spearmint.chew.clear
|
63
|
+
@original.each {|e| Spearmint.chew << e}
|
65
64
|
end
|
66
65
|
|
67
66
|
it "should override the previously stuck method" do
|
@@ -71,6 +70,28 @@ describe "Overriding stuck methods" do
|
|
71
70
|
end
|
72
71
|
end
|
73
72
|
|
73
|
+
describe "Composing behavior" do
|
74
|
+
before :all do
|
75
|
+
@original = Spearmint.chew.dup
|
76
|
+
Spearmint.class_eval do
|
77
|
+
stick :chew do
|
78
|
+
self.gsub(/[a,e,i,o,u]/, '')
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
after :all do
|
84
|
+
Spearmint.chew.clear
|
85
|
+
@original.each {|e| Spearmint.chew << e}
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should add to the previously defined behavior" do
|
89
|
+
Spearmint.stick_to String
|
90
|
+
s = 'a string'
|
91
|
+
s.chew.should == 'gnrts '
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
74
95
|
describe "Undefining stuck methods" do
|
75
96
|
it "should not throw an error if we try do unstick something that's never been stuck" do
|
76
97
|
Spearmint.stick_to String
|
@@ -85,25 +106,44 @@ describe "Undefining stuck methods" do
|
|
85
106
|
s = ''
|
86
107
|
s.should respond_to(:chew)
|
87
108
|
s.should respond_to(:chew_it_up_and_spit_it_out)
|
109
|
+
s.should respond_to(:open)
|
110
|
+
validate_sticks_of_spearmint
|
88
111
|
|
89
112
|
Spearmint.unstick_from String => [:chew], :ignore_errors => false
|
90
113
|
s.should_not respond_to(:chew)
|
91
114
|
s.should respond_to(:chew_it_up_and_spit_it_out)
|
115
|
+
validate_sticks_of_spearmint
|
92
116
|
|
93
117
|
Spearmint.unstick_from String => [:chew_it_up_and_spit_it_out], :ignore_errors => false
|
94
118
|
s.should_not respond_to(:chew_it_up_and_spit_it_out)
|
119
|
+
validate_sticks_of_spearmint
|
120
|
+
|
121
|
+
Spearmint.unstick_from String => [:open], :ignore_errors => false
|
122
|
+
s.should_not respond_to(:open)
|
123
|
+
validate_sticks_of_spearmint
|
95
124
|
end
|
96
125
|
|
97
126
|
it 'should be able to undefine all stuck methods' do
|
98
127
|
Spearmint.stick_to String
|
99
|
-
s = ''
|
128
|
+
s = 'jason'
|
100
129
|
s.should respond_to(:chew)
|
101
130
|
s.should respond_to(:chew_it_up_and_spit_it_out)
|
102
131
|
s.should respond_to(:open)
|
132
|
+
s.chew.should == 'nosaj'
|
133
|
+
|
134
|
+
validate_sticks_of_spearmint
|
103
135
|
|
104
136
|
Spearmint.unstick_all_from String, false
|
105
137
|
s.should_not respond_to(:chew)
|
106
138
|
s.should_not respond_to(:chew_it_up_and_spit_it_out)
|
107
139
|
s.should_not respond_to(:open)
|
140
|
+
|
141
|
+
validate_sticks_of_spearmint
|
142
|
+
end
|
143
|
+
|
144
|
+
def validate_sticks_of_spearmint
|
145
|
+
Spearmint.chew.should_not be_empty
|
146
|
+
Spearmint.chew_it_up_and_spit_it_out.should_not be_empty
|
147
|
+
Spearmint.open.should_not be_empty
|
108
148
|
end
|
109
149
|
end
|
data/sticky_blox.gemspec
CHANGED