ruby_decorators 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -26,6 +26,12 @@ Or install it yourself as:
26
26
  ## Usage
27
27
 
28
28
  ```ruby
29
+ class Hi < RubyDecorator
30
+ def call(this, *args, &blk)
31
+ this.call(*args, &blk).sub('hello', 'hi')
32
+ end
33
+ end
34
+
29
35
  class Batman < RubyDecorator
30
36
  def call(this, *args, &blk)
31
37
  this.call(*args, &blk).sub('world', 'batman')
@@ -58,6 +64,7 @@ class World
58
64
  @greeting
59
65
  end
60
66
 
67
+ +Hi
61
68
  +Catwoman
62
69
  def hello_catwoman
63
70
  @greeting
@@ -73,7 +80,7 @@ world = World.new
73
80
 
74
81
  world.hello_world # => "hello world"
75
82
  world.hello_batman # => "hello batman"
76
- world.hello_catwoman # => "hello catwoman"
83
+ world.hello_catwoman # => "hi catwoman"
77
84
  world.hello_super_catwoman # => "hello super catwoman"
78
85
  ```
79
86
 
@@ -1,9 +1,9 @@
1
1
  class RubyDecorator
2
2
  def self.+@
3
- RubyDecorators::Stack.decorators << self
3
+ RubyDecorators::Stack.all << self
4
4
  end
5
5
 
6
6
  def +@
7
- RubyDecorators::Stack.decorators << self
7
+ RubyDecorators::Stack.all << self
8
8
  end
9
9
  end
@@ -1,43 +1,41 @@
1
1
  require "ruby_decorators/version"
2
2
  require "ruby_decorator"
3
- require "ruby_decorators/stack"
4
3
 
5
4
  module RubyDecorators
6
- def method_added(method_name)
7
- @__decorated_methods ||= []
5
+ class Stack
6
+ def self.all
7
+ @all ||= []
8
+ end
9
+ end
8
10
 
9
- return if RubyDecorators::Stack.decorators.empty? ||
10
- method_name.to_s =~ /__undecorated_/ ||
11
- @__decorated_methods.include?(method_name)
11
+ def method_added(method_name)
12
+ super
12
13
 
13
- current_decorator = RubyDecorators::Stack.decorators.pop
14
- method_visibility = detect_method_visibility(method_name)
14
+ @methods ||= {}
15
+ @decorators ||= {}
15
16
 
16
- class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
17
- alias_method :__undecorated_#{method_name}, :#{method_name}
17
+ return if RubyDecorators::Stack.all.empty?
18
18
 
19
- @__decorators ||= {}
20
- @__decorators["#{method_name}"] = current_decorator
19
+ @methods[method_name] = instance_method(method_name)
20
+ @decorators[method_name] = RubyDecorators::Stack.all.pop(42)
21
21
 
22
- #{method_visibility}
22
+ class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
23
+ #{method_visibility_for(method_name)}
23
24
  def #{method_name}(*args, &blk)
24
- decorator = #{current_decorator}.new
25
- decorator ||= self.class.instance_variable_get(:@__decorators)["#{method_name}"]
26
-
27
- if args.any?
28
- decorator.call(method(:__undecorated_#{method_name}), *args, &blk)
29
- else
30
- decorator.call(method(:__undecorated_#{method_name}), &blk)
31
- end
25
+ decorators = self.class.instance_variable_get(:@decorators)[:#{method_name}]
26
+ method = self.class.instance_variable_get(:@methods)[:#{method_name}]
27
+
28
+ decorators.inject(method.bind(self)) do |method, decorator|
29
+ decorator = decorator.new if decorator.respond_to?(:new)
30
+ lambda { |*a, &b| decorator.call(method, *a, &b) }
31
+ end.call(*args, &blk)
32
32
  end
33
33
  RUBY_EVAL
34
-
35
- @__decorated_methods << method_name
36
34
  end
37
35
 
38
36
  private
39
37
 
40
- def detect_method_visibility(method_name)
38
+ def method_visibility_for(method_name)
41
39
  if private_method_defined?(method_name)
42
40
  :private
43
41
  elsif protected_method_defined?(method_name)
@@ -1,3 +1,3 @@
1
1
  module RubyDecorators
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -7,6 +7,12 @@ describe RubyDecorators do
7
7
  end
8
8
  end
9
9
 
10
+ class Hi < RubyDecorator
11
+ def call(this, *args, &blk)
12
+ this.call(*args, &blk).sub('hello', 'hi')
13
+ end
14
+ end
15
+
10
16
  class Batman < RubyDecorator
11
17
  def call(this, *args, &blk)
12
18
  this.call(*args, &blk).sub('world', 'batman')
@@ -23,6 +29,15 @@ describe RubyDecorators do
23
29
  end
24
30
  end
25
31
 
32
+ class DummyClass2
33
+ extend RubyDecorators
34
+
35
+ +Hi
36
+ def hi
37
+ 'hello'
38
+ end
39
+ end
40
+
26
41
  class DummyClass
27
42
  extend RubyDecorators
28
43
 
@@ -68,6 +83,18 @@ describe RubyDecorators do
68
83
  @greeting
69
84
  end
70
85
 
86
+ +Hi
87
+ +Batman
88
+ def hi_batman
89
+ @greeting
90
+ end
91
+
92
+ +Hi
93
+ +Catwoman.new('super', 'catwoman')
94
+ def hi_super_catwoman(arg1)
95
+ "#{@greeting}#{arg1}"
96
+ end
97
+
71
98
  protected
72
99
 
73
100
  +Batman
@@ -86,6 +113,7 @@ describe RubyDecorators do
86
113
  subject { DummyClass.new }
87
114
 
88
115
  it "#hello_world" do
116
+ DummyClass2.new.hi.must_equal 'hi'
89
117
  subject.hello_world.must_equal 'hello world'
90
118
  end
91
119
 
@@ -126,4 +154,14 @@ describe RubyDecorators do
126
154
  subject.hello_super_catwoman.must_equal 'hello super catwoman'
127
155
  end
128
156
  end
157
+
158
+ describe "multiple decorators" do
159
+ it "decorates a simple method" do
160
+ subject.hi_batman.must_equal 'hi batman'
161
+ end
162
+
163
+ it "decorates a method with args" do
164
+ subject.hi_super_catwoman('!').must_equal 'hi super catwoman!'
165
+ end
166
+ end
129
167
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_decorators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -72,7 +72,6 @@ files:
72
72
  - Rakefile
73
73
  - lib/ruby_decorator.rb
74
74
  - lib/ruby_decorators.rb
75
- - lib/ruby_decorators/stack.rb
76
75
  - lib/ruby_decorators/version.rb
77
76
  - ruby_decorators.gemspec
78
77
  - spec/ruby_decorators_spec.rb
@@ -91,7 +90,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
91
90
  version: '0'
92
91
  segments:
93
92
  - 0
94
- hash: -142632259474802857
93
+ hash: -1045434834331850826
95
94
  required_rubygems_version: !ruby/object:Gem::Requirement
96
95
  none: false
97
96
  requirements:
@@ -100,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
99
  version: '0'
101
100
  segments:
102
101
  - 0
103
- hash: -142632259474802857
102
+ hash: -1045434834331850826
104
103
  requirements: []
105
104
  rubyforge_project:
106
105
  rubygems_version: 1.8.24
@@ -1,7 +0,0 @@
1
- module RubyDecorators
2
- class Stack
3
- def self.decorators
4
- @decorators ||= []
5
- end
6
- end
7
- end