sticky_blox 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -15,6 +15,12 @@ without having to add the methods to every class in the system. StickyBlox allow
15
15
  shoe-horned into classes such that the functionalities (sticks) become instance methods or just have
16
16
  the stick available to be bound to anything, at anytime.
17
17
 
18
+ h2. Installing
19
+
20
+ What else?
21
+
22
+ <pre><code>[sudo] gem install sticky_blox</code></pre>
23
+
18
24
  h2. Example
19
25
 
20
26
  With the later releases of Sinatra, your code is not reload-able in development. You either have to
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.0.1
@@ -6,21 +6,49 @@ module StickyBlox
6
6
  end
7
7
 
8
8
  module ClassMethods
9
- def stick_to binding
10
- (class << self; self; end).instance_eval do
9
+ def my_meta_class
10
+ class << self; self; end
11
+ end
12
+
13
+ def stick_to( binding )
14
+ my_meta_class.instance_eval do
11
15
  clz = self
12
- @stuck_methods.each do |route_name|
16
+ @stuck_methods.each do |method_name|
13
17
  binding.class_eval do
14
- define_method route_name do | *args |
15
- clz.send(route_name).bind(self).call(*args)
18
+ define_method method_name do | *args |
19
+ clz.send(method_name).bind(self).call(*args)
16
20
  end
17
21
  end
18
22
  end
19
23
  end
20
24
  end
21
25
 
26
+ def unstick_from( options={} )
27
+ ignore_errors = options.delete(:ignore_errors)
28
+ options.each_pair do |binding, method_names|
29
+ method_names = [method_names].flatten
30
+ method_names.each do |method_name|
31
+ binding.class_eval do
32
+ begin
33
+ # remove_method removes it from a specific class, but not its parent
34
+ # Do we want to use undef_method which will remove it from the whole
35
+ # inheritance chain?
36
+ remove_method method_name
37
+ rescue NoMethodError => e
38
+ # unless told otherwise, ignore the error -- if we don't respond to the method, no harm done.
39
+ raise e unless ignore_errors
40
+ end
41
+ end if binding.instance_methods.detect{|e| e.to_s == method_name.to_s}
42
+ end
43
+ end
44
+ end
45
+
46
+ def unstick_all_from( binding, ignore_errors=true )
47
+ unstick_from( binding => my_meta_class.instance_variable_get("@stuck_methods"), :ignore_errors => ignore_errors )
48
+ end
49
+
22
50
  def stick name, &block
23
- (class << self; self; end).instance_eval do
51
+ my_meta_class.instance_eval do
24
52
  @stuck_methods ||= []
25
53
  @stuck_methods << name unless @stuck_methods.include?(name)
26
54
  end
@@ -22,6 +22,7 @@ describe "Instance-based approaches" do
22
22
  end
23
23
 
24
24
  it "should turn the String into an Integer and return an array of numbers" do
25
+ Spearmint.stick_to String
25
26
  thing = "2"
26
27
  thing.open.should == [1, 2]
27
28
  thing.open(5).should == [1, 2, 3, 4, 5]
@@ -45,4 +46,56 @@ describe "Class-based approaches" do
45
46
  thing = Object.new
46
47
  proc {Spearmint.chew.bind(thing).call}.should raise_error
47
48
  end
49
+ end
50
+
51
+ # describe "Overriding stuck methods" do
52
+ # class NewSpearmint
53
+ # include StickyBlox
54
+ # stick :chew do
55
+ # self.upcase
56
+ # end
57
+ # end
58
+ #
59
+ # it "should override the previously stuck method" do
60
+ # NewSpearmint.stick_to String
61
+ # s = 'a string'
62
+ # s.chew.should == 'GNIRTS A'
63
+ # end
64
+ # end
65
+
66
+ describe "Undefining stuck methods" do
67
+ it "should not throw an error if we try do unstick something that's never been stuck" do
68
+ Spearmint.stick_to String
69
+ s = ''
70
+ s.should_not respond_to(:bogus_method_name)
71
+ Spearmint.unstick_from String => [:bogus_method_name], :ignore_errors => false
72
+ s.should_not respond_to(:bogus_method_name)
73
+ end
74
+
75
+ it 'should be able to undefine methods that have been stuck to a class' do
76
+ Spearmint.stick_to String
77
+ s = ''
78
+ s.should respond_to(:chew)
79
+ s.should respond_to(:chew_it_up_and_spit_it_out)
80
+
81
+ Spearmint.unstick_from String => [:chew], :ignore_errors => false
82
+ s.should_not respond_to(:chew)
83
+ s.should respond_to(:chew_it_up_and_spit_it_out)
84
+
85
+ Spearmint.unstick_from String => [:chew_it_up_and_spit_it_out], :ignore_errors => false
86
+ s.should_not respond_to(:chew_it_up_and_spit_it_out)
87
+ end
88
+
89
+ it 'should be able to undefine all stuck methods' do
90
+ Spearmint.stick_to String
91
+ s = ''
92
+ s.should respond_to(:chew)
93
+ s.should respond_to(:chew_it_up_and_spit_it_out)
94
+ s.should respond_to(:open)
95
+
96
+ Spearmint.unstick_all_from String, false
97
+ s.should_not respond_to(:chew)
98
+ s.should_not respond_to(:chew_it_up_and_spit_it_out)
99
+ s.should_not respond_to(:open)
100
+ end
48
101
  end
data/sticky_blox.gemspec CHANGED
@@ -5,12 +5,12 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sticky_blox}
8
- s.version = "1.0.0"
8
+ s.version = "1.0.1"
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"]
12
12
  s.date = %q{2010-04-12}
13
- s.description = %q{see http://github.com/jacaetevha/sticky_blox for more description. I'm lazy}
13
+ s.description = %q{Late-binding traits for Ruby. See http://github.com/jacaetevha/sticky_blox for more description.}
14
14
  s.email = %q{jacaetevha@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 0
9
- version: 1.0.0
8
+ - 1
9
+ version: 1.0.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jason Rogers
@@ -31,7 +31,7 @@ dependencies:
31
31
  version: 1.3.0
32
32
  type: :development
33
33
  version_requirements: *id001
34
- description: see http://github.com/jacaetevha/sticky_blox for more description. I'm lazy
34
+ description: Late-binding traits for Ruby. See http://github.com/jacaetevha/sticky_blox for more description.
35
35
  email: jacaetevha@gmail.com
36
36
  executables: []
37
37