define_method_handler 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README +49 -0
  2. data/Rakefile +2 -2
  3. data/lib/define_method_handler.rb +120 -0
  4. metadata +5 -3
data/README CHANGED
@@ -0,0 +1,49 @@
1
+
2
+ ................................................................................
3
+ ................................................................................
4
+ ................................................................................
5
+ ................................................................................
6
+ ................................................................................
7
+ ........................,DMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM..................
8
+ ..................~MMMMMMM8:........+NMMZ:....$MZ............NMM................
9
+ ...............MMM~.......................,........,M..........MM+..............
10
+ ............NMM:..,~..=8MM87?7OMMMMD............+MZ...O7........MMO.............
11
+ ..........MMM...............................,?DMM,...M...M.......MM?............
12
+ .........,M,.......M.........8......................+8.~O..7......MM............
13
+ .........NM.................M..........$...............M...........MM...........
14
+ .........MM..................O.........,......78D7......O..:.......+M:..........
15
+ ........MM...................O...........MMMM$MMMMMMMM?.............OMD.........
16
+ ......IMM.........NMMMMMMMMM,..........MM?....MMMMMM$.MM.............,MM........
17
+ .....MM7.NM?......8MMMMMMMMMMM,MM.....MM...MMMMMMNNMMMMMM.:M,........N~=MM......
18
+ ....MM.8..,.................MMM$.......MMMM.....NM..........ZMMMMMMO....+.MN....
19
+ ...8M87.D...MMM=.............M...................MMM.....,MMMM.....NMD.....MM...
20
+ ...MM.,.M.MMN..+MMM.OM.......M.....................?MMMMMM+....M.....MM..M.:M...
21
+ ...ZM...M.....M..MMMM......MMM...............................DMMM.....M..D..MM..
22
+ ....M==.M.....M..........MM8...........MMMMMM............,MMMN..MMM,..M,.M..MM..
23
+ ....NMM...$..MMI.......MMMM..........,....MM..........DMMMM....MM?OM$NM..+..MZ..
24
+ .....MM.8M..MMMM....D:..O.MM......+MMMMM.8MN......MMMMMM......8MM....M.....MM...
25
+ ......MM....MMMMMM,.........MM+M~............NMMMM8....M?...NMMM?......?..MM....
26
+ .......MD...MMM,MMMMMM.......,M.......=MMMMMM,........MMMMMMMMM.........MMM.....
27
+ .......MM...MMM.MM..NMMMMMMMMMMMMMMMMMMM,...MM......MMMMMD..MM........7MM.......
28
+ .......DM...MMM.MM..MM......MN....+M........MM.MMMMMMMNM...MM........IMM........
29
+ .......:M...MMMMMMM.MM......M.....+M......DMMMMMMMM...DM..MM.........MN.........
30
+ ........M...MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM........MMMN.........MM..........
31
+ .......,M...MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM~..MM.......8MM,.........:M...........
32
+ .......$M...ZM.MMMMMMMMMMMMMMMMMMMMMMO.......~MZ....MMMO..........=M8...........
33
+ .......MM....MM:M.$M...M....MM......MN........NM.~MMM,...........MM,............
34
+ .......MM.....MM7..MM..MM....M......MM........MMMMD.....Z...O..MMM..............
35
+ .......M7......8MMMMMMO,MM...MM.....MM87NMMMMMM,....M=..=M?..MMM................
36
+ .......M...........IMMMMMMMMMMMMMMMMMMMMMMZ......M$...MD...MMM..................
37
+ ......:M...I...:.............................,M...MM...,MMM.....................
38
+ ......ZM...+.....M:......................IM,..MN....OMMM........................
39
+ ......NM....=.........?7$I?~::,.........?M?......MMMM...........................
40
+ ......DM......=MO................+MO..........MMMN..............................
41
+ .......MN..................................MMM?.................................
42
+ ........MM..........................MMMMMMM~....................................
43
+ .........MMM...................NMMMMMMM,........................................
44
+ ...........NMMMMMNO7I$DMMMMMMMMD................................................
45
+ ................+DMMMNZ~........................................................
46
+ ................................................................................
47
+ ................................................................................
48
+ ................................................................................
49
+
data/Rakefile CHANGED
@@ -7,12 +7,12 @@ 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.1'
10
+ s.version = '0.0.2'
11
11
  s.author = 'Dario Seminara'
12
12
  s.email = 'robertodarioseminara@gmail.com'
13
13
  s.platform = Gem::Platform::RUBY
14
14
  s.summary = 'Chain of responsability implementation in a ruby fashion way'
15
- s.description = 'Chain of responsability implementation in a ruby fashion way'
15
+ s.description = 'Chain of responsability implementation in a ruby fashion way. See README for examples'
16
16
  s.homepage = "http://github.com/tario/define_method_handler"
17
17
  s.has_rdoc = true
18
18
  s.extra_rdoc_files = [ 'README' ]
@@ -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
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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,15 +9,17 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-25 00:00:00.000000000Z
12
+ date: 2012-01-26 00:00:00.000000000Z
13
13
  dependencies: []
14
- description: Chain of responsability implementation in a ruby fashion way
14
+ description: Chain of responsability implementation in a ruby fashion way. See README
15
+ for examples
15
16
  email: robertodarioseminara@gmail.com
16
17
  executables: []
17
18
  extensions: []
18
19
  extra_rdoc_files:
19
20
  - README
20
21
  files:
22
+ - lib/define_method_handler.rb
21
23
  - spec/priority_spec.rb
22
24
  - spec/base_spec.rb
23
25
  - spec/handler_scope_spec.rb