ruby_decorators 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  #### Ruby method decorators inspired by Python.
4
4
 
5
+ I wrote this as a small practice for some ruby meta-programming fun. The implementation is relatively simple, and is thread safe.
6
+
7
+ There are also these other two implementations:
8
+
9
+ - Yehuda Katz's [Ruby Decorators](https://github.com/wycats/ruby_decorators)
10
+ - Michael Fairley's [Method Decorators](https://github.com/michaelfairley/method_decorators)
11
+
5
12
  ## Installation
6
13
 
7
14
  Add this line to your application's Gemfile:
@@ -20,8 +27,8 @@ Or install it yourself as:
20
27
 
21
28
  ```ruby
22
29
  class Batman < RubyDecorator
23
- def call(this)
24
- this.sub('world', 'batman')
30
+ def call(this, *args, &blk)
31
+ this.call(*args, &blk).sub('world', 'batman')
25
32
  end
26
33
  end
27
34
 
@@ -30,12 +37,12 @@ class Catwoman < RubyDecorator
30
37
  @args = args.any? ? args : ['catwoman']
31
38
  end
32
39
 
33
- def call(this)
34
- this.sub('world', @args.join(' '))
40
+ def call(this, *args, &blk)
41
+ this.call(*args, &blk).sub('world', @args.join(' '))
35
42
  end
36
43
  end
37
44
 
38
- class DummyClass
45
+ class World
39
46
  extend RubyDecorators
40
47
 
41
48
  def initialize
@@ -62,12 +69,12 @@ class DummyClass
62
69
  end
63
70
  end
64
71
 
65
- dummy = DummyClass.new
72
+ world = World.new
66
73
 
67
- dummy.hello_world # => "hello world"
68
- dummy.hello_batman # => "hello batman"
69
- dummy.hello_catwoman # => "hello catwoman"
70
- dummy.hello_super_catwoman # => "hello super catwoman"
74
+ world.hello_world # => "hello world"
75
+ world.hello_batman # => "hello batman"
76
+ world.hello_catwoman # => "hello catwoman"
77
+ world.hello_super_catwoman # => "hello super catwoman"
71
78
  ```
72
79
 
73
80
  ## License
@@ -4,11 +4,11 @@ require "ruby_decorators/stack"
4
4
 
5
5
  module RubyDecorators
6
6
  def method_added(method_name)
7
- @decorated_methods ||= []
7
+ @__decorated_methods ||= []
8
8
 
9
9
  return if RubyDecorators::Stack.decorators.empty? ||
10
10
  method_name.to_s =~ /__undecorated_/ ||
11
- @decorated_methods.include?(method_name)
11
+ @__decorated_methods.include?(method_name)
12
12
 
13
13
  current_decorator = RubyDecorators::Stack.decorators.pop
14
14
  method_visibility = detect_method_visibility(method_name)
@@ -25,14 +25,14 @@ module RubyDecorators
25
25
  decorator ||= self.class.instance_variable_get(:@__decorators)["#{method_name}"]
26
26
 
27
27
  if args.any?
28
- decorator.call(self.send :__undecorated_#{method_name}, *args, &blk)
28
+ decorator.call(method(:__undecorated_#{method_name}), *args, &blk)
29
29
  else
30
- decorator.call(self.send :__undecorated_#{method_name}, &blk)
30
+ decorator.call(method(:__undecorated_#{method_name}), &blk)
31
31
  end
32
32
  end
33
33
  RUBY_EVAL
34
34
 
35
- @decorated_methods << method_name
35
+ @__decorated_methods << method_name
36
36
  end
37
37
 
38
38
  private
@@ -1,3 +1,3 @@
1
1
  module RubyDecorators
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -8,8 +8,8 @@ describe RubyDecorators do
8
8
  end
9
9
 
10
10
  class Batman < RubyDecorator
11
- def call(this)
12
- this.sub('world', 'batman')
11
+ def call(this, *args, &blk)
12
+ this.call(*args, &blk).sub('world', 'batman')
13
13
  end
14
14
  end
15
15
 
@@ -18,8 +18,8 @@ describe RubyDecorators do
18
18
  @args = args.any? ? args : ['catwoman']
19
19
  end
20
20
 
21
- def call(this)
22
- this.sub('world', @args.join(' '))
21
+ def call(this, *args, &blk)
22
+ this.call(*args, &blk).sub('world', @args.join(' '))
23
23
  end
24
24
  end
25
25
 
@@ -44,6 +44,10 @@ describe RubyDecorators do
44
44
  @greeting
45
45
  end
46
46
 
47
+ def hello_untouched
48
+ @greeting
49
+ end
50
+
47
51
  +Batman
48
52
  def hello_with_args(arg1, arg2)
49
53
  "#{@greeting} #{arg1} #{arg2}"
@@ -107,6 +111,10 @@ describe RubyDecorators do
107
111
  it "decorates a method with a block" do
108
112
  subject.hello_with_block('how are', 'you') { 'man?' }.must_equal 'hello batman how are you man?'
109
113
  end
114
+
115
+ it "ignores undecorated methods" do
116
+ subject.hello_untouched.must_equal 'hello world'
117
+ end
110
118
  end
111
119
 
112
120
  describe "a decorator with args" do
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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-11 00:00:00.000000000 Z
12
+ date: 2012-09-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -91,7 +91,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
91
91
  version: '0'
92
92
  segments:
93
93
  - 0
94
- hash: -334699568952873457
94
+ hash: -142632259474802857
95
95
  required_rubygems_version: !ruby/object:Gem::Requirement
96
96
  none: false
97
97
  requirements:
@@ -100,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
100
  version: '0'
101
101
  segments:
102
102
  - 0
103
- hash: -334699568952873457
103
+ hash: -142632259474802857
104
104
  requirements: []
105
105
  rubyforge_project:
106
106
  rubygems_version: 1.8.24