sticky_blox 1.1.0 → 1.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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 1.2.0
@@ -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.each do |method_name|
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
- clz.send(method_name).bind(self).call(*args)
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 name, &block
55
- my_meta_class.instance_eval do
56
- @stuck_methods ||= [] #{}
57
- @stuck_methods << name unless @stuck_methods.include?(name) #[name] = block unless @stuck_methods.has_key?(name)
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
- # def method_missing(meth, *args, &blk)
67
- # raise "#{clz} doesn't respond to #{meth}"
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
- block
87
+ my_methods
72
88
  end
73
89
  end
74
90
  end
@@ -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.class_eval do
63
- stick :chew, &@original
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
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sticky_blox}
8
- s.version = "1.1.0"
8
+ s.version = "1.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jason Rogers"]
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 1.1.0
9
+ version: 1.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jason Rogers