define_method_handler 0.0.2 → 0.0.3

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/CHANGELOG CHANGED
@@ -0,0 +1,5 @@
1
+ 0.0.3 Fixed bug on naming for anonymous methods defined on the fly
2
+
3
+ 0.0.2 Added missing lib dir to gem
4
+
5
+ 0.0.1 First release, broken :(
data/Rakefile CHANGED
@@ -7,7 +7,7 @@ require "rspec/core/rake_task"
7
7
 
8
8
  spec = Gem::Specification.new do |s|
9
9
  s.name = 'define_method_handler'
10
- s.version = '0.0.2'
10
+ s.version = '0.0.3'
11
11
  s.author = 'Dario Seminara'
12
12
  s.email = 'robertodarioseminara@gmail.com'
13
13
  s.platform = Gem::Platform::RUBY
@@ -97,7 +97,7 @@ class Class
97
97
  define_method(mname) do |*x, &callblk|
98
98
  self.class.method_handlers.reject{|mhh| (@disabled_handler_groups||[]).include? mhh.group }.reverse_each do |mhh|
99
99
  if mhh.execute?(*x)
100
- tmp_method = 'tmpmethod#{rand(10000000000000)}'
100
+ tmp_method = "tmpmethod#{rand(1000000)}#{Time.now.to_i}"
101
101
 
102
102
  begin
103
103
  self.class.class_eval do
@@ -117,4 +117,4 @@ class Class
117
117
 
118
118
  mh
119
119
  end
120
- end
120
+ end
@@ -0,0 +1,120 @@
1
+ =begin
2
+
3
+ This file is part of the define_method_handler project, http://github.com/tario/define_method_handler
4
+
5
+ Copyright (c) 2011 Roberto Dario Seminara <robertodarioseminara@gmail.com>
6
+
7
+ define_method_handler is free software: you can redistribute it and/or modify
8
+ it under the terms of the gnu general public license as published by
9
+ the free software foundation, either version 3 of the license, or
10
+ (at your option) any later version.
11
+
12
+ define_method_handler is distributed in the hope that it will be useful,
13
+ but without any warranty; without even the implied warranty of
14
+ merchantability or fitness for a particular purpose. see the
15
+ gnu general public license for more details.
16
+
17
+ you should have received a copy of the gnu general public license
18
+ along with define_method_handler. if not, see <http://www.gnu.org/licenses/>.
19
+
20
+ =end
21
+ require "set"
22
+ class Class
23
+ class MethodHandler
24
+ attr_reader :processor
25
+ attr_reader :priority
26
+ attr_reader :second_priority
27
+ attr_reader :group
28
+
29
+ def initialize(processor, sprior, group, prior = 0)
30
+ @processor = processor
31
+ @priority = prior
32
+ @second_priority = sprior
33
+ @group = group
34
+ end
35
+
36
+ def execute?(*args)
37
+ @condition ? @condition.call(*args) : true
38
+ end
39
+
40
+ def condition(&blk)
41
+ @condition = blk
42
+ self
43
+ end
44
+ end
45
+
46
+ module ChainMethods
47
+ def enable_handler_group(groupname)
48
+ @disabled_handler_groups ||= Set.new
49
+ group_included = @disabled_handler_groups.include? groupname
50
+ @disabled_handler_groups.delete(groupname)
51
+ yield
52
+ ensure
53
+ @disabled_handler_groups << groupname if group_included
54
+ end
55
+
56
+ def disable_handler_group(groupname)
57
+ @disabled_handler_groups ||= Set.new
58
+ @disabled_handler_groups << groupname
59
+ yield
60
+ ensure
61
+ @disabled_handler_groups.delete groupname
62
+ end
63
+ end
64
+
65
+ def method_handlers
66
+ @method_handlers
67
+ end
68
+
69
+ def handler_scope(options)
70
+ old_options = @method_handler_options
71
+ @method_handler_options = (@method_handler_options || {}).merge(options)
72
+ yield
73
+ ensure
74
+ @method_handler_options = old_options
75
+ end
76
+
77
+ def define_method_handler(mname, *options, &blk)
78
+ options = options.inject(&:merge) || {}
79
+ options.merge!(@method_handler_options) if @method_handler_options
80
+
81
+ @method_handlers ||= Array.new
82
+ @next_priority = (@next_priority || 0) + 1
83
+
84
+ mh = MethodHandler.new(blk, @next_priority, options[:group] || :default, options[:priority] || 0)
85
+ @method_handlers << mh
86
+
87
+ include ChainMethods
88
+
89
+ @method_handlers.sort!{|x,y|
90
+ if x.priority == y.priority
91
+ x.second_priority <=> y.second_priority
92
+ else
93
+ x.priority <=> y.priority
94
+ end
95
+ }
96
+
97
+ define_method(mname) do |*x, &callblk|
98
+ self.class.method_handlers.reject{|mhh| (@disabled_handler_groups||[]).include? mhh.group }.reverse_each do |mhh|
99
+ if mhh.execute?(*x)
100
+ tmp_method = 'tmpmethod#{rand(10000000000000)}'
101
+
102
+ begin
103
+ self.class.class_eval do
104
+ define_method(tmp_method, &mhh.processor)
105
+ end
106
+ return method(tmp_method).call(*x, &callblk)
107
+ ensure
108
+ self.class.class_eval do
109
+ remove_method(tmp_method)
110
+ end
111
+ end
112
+ end
113
+ end
114
+
115
+ nil
116
+ end
117
+
118
+ mh
119
+ end
120
+ end
data/spec/base_spec.rb CHANGED
@@ -38,5 +38,16 @@ describe "define_method_handler" do
38
38
  CHAIN3.new.foo(3).should be == 1
39
39
  CHAIN3.new.foo(4).should be == 2
40
40
  end
41
+
42
+ it "should accept recursive methods" do
43
+ class CHAIN4
44
+ define_method_handler(:fact) do |n|
45
+ n>1 ? fact(n-1)*n : 1
46
+ end
47
+ end
48
+
49
+ chain4 = CHAIN4.new
50
+ chain4.fact(5).should be == 120
51
+ end
41
52
 
42
- end
53
+ end
@@ -0,0 +1,42 @@
1
+ require "define_method_handler"
2
+
3
+
4
+ describe "define_method_handler" do
5
+
6
+ it "single method handler without condition should act as a method" do
7
+ class CHAIN1
8
+ define_method_handler(:foo) do
9
+ 100
10
+ end
11
+ end
12
+
13
+ CHAIN1.new.foo.should be == 100
14
+ end
15
+
16
+
17
+ it "single method handler without condition should act as a method and this method should accept blocks" do
18
+ class CHAIN2
19
+ define_method_handler(:foo) do |&blk|
20
+ blk.call + 1
21
+ end
22
+ end
23
+
24
+ CHAIN2.new.foo{99}.should be == 100
25
+ end
26
+
27
+ it "two method handlers with the same name should respond depending on condition" do
28
+ class CHAIN3
29
+ define_method_handler(:foo) {|x|
30
+ 1
31
+ }.condition{|x| x==3}
32
+
33
+ define_method_handler(:foo) {|x|
34
+ 2
35
+ }.condition{|x| x==4}
36
+ end
37
+
38
+ CHAIN3.new.foo(3).should be == 1
39
+ CHAIN3.new.foo(4).should be == 2
40
+ end
41
+
42
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: define_method_handler
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:
@@ -20,7 +20,9 @@ extra_rdoc_files:
20
20
  - README
21
21
  files:
22
22
  - lib/define_method_handler.rb
23
+ - lib/define_method_handler.rb~
23
24
  - spec/priority_spec.rb
25
+ - spec/base_spec.rb~
24
26
  - spec/base_spec.rb
25
27
  - spec/handler_scope_spec.rb
26
28
  - spec/group_spec.rb