define_method_handler 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/AUTHORS ADDED
@@ -0,0 +1,2 @@
1
+ fastruby de tario <darios3@gmail.com>
2
+ - Dario Seminara
data/CHANGELOG ADDED
File without changes
data/README ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+ require 'rake/gempackagetask'
6
+ require "rspec/core/rake_task"
7
+
8
+ spec = Gem::Specification.new do |s|
9
+ s.name = 'define_method_handler'
10
+ s.version = '0.0.1'
11
+ s.author = 'Dario Seminara'
12
+ s.email = 'robertodarioseminara@gmail.com'
13
+ s.platform = Gem::Platform::RUBY
14
+ s.summary = 'Chain of responsability implementation in a ruby fashion way'
15
+ s.description = 'Chain of responsability implementation in a ruby fashion way'
16
+ s.homepage = "http://github.com/tario/define_method_handler"
17
+ s.has_rdoc = true
18
+ s.extra_rdoc_files = [ 'README' ]
19
+ s.extensions = FileList["ext/**/extconf.rb"].to_a
20
+ # s.rdoc_options << '--main' << 'README'
21
+ s.files = Dir.glob("{lib,spec}/**/*") +
22
+ [ 'AUTHORS', 'README', 'Rakefile', 'TODO', 'CHANGELOG' ]
23
+ end
24
+
25
+ desc 'Run tests'
26
+
27
+ RSpec::Core::RakeTask.new("test:units") do |t|
28
+ t.pattern= 'spec/**/*.rb'
29
+ end
30
+
31
+ desc 'Generate RDoc'
32
+ Rake::RDocTask.new :rdoc do |rd|
33
+ rd.rdoc_dir = 'doc'
34
+ rd.rdoc_files.add 'lib', 'README'
35
+ rd.main = 'README'
36
+ end
37
+
38
+ desc 'Build Gem'
39
+ Rake::GemPackageTask.new spec do |pkg|
40
+ pkg.need_tar = true
41
+ end
42
+
43
+ desc 'Clean up'
44
+ task :clean => [ :clobber_rdoc, :clobber_package ]
45
+
46
+ desc 'Clean up'
47
+ task :clobber => [ :clean ]
data/TODO ADDED
File without changes
data/spec/base_spec.rb ADDED
@@ -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
@@ -0,0 +1,91 @@
1
+ require "define_method_handler"
2
+
3
+
4
+ describe "define_method_handler" do
5
+
6
+ it "one method handler defined with group only should run when that group is enabled" do
7
+ class CHAIN2_1
8
+ define_method_handler(:foo, :group => :testgroup) {
9
+ 10
10
+ }
11
+ end
12
+
13
+ CHAIN2_1.new.foo.should be == 10
14
+ end
15
+
16
+ it "one method handler defined with group only should not run when that group is disabled" do
17
+ class CHAIN2_1
18
+ define_method_handler(:foo, :group => :testgroup) {
19
+ 10
20
+ }
21
+ end
22
+
23
+ chain = CHAIN2_1.new
24
+ chain.disable_handler_group(:testgroup) do
25
+ chain.foo.should be == nil
26
+ end
27
+ end
28
+
29
+ it "one method handler defined with group only should run after a group disable block" do
30
+ class CHAIN2_1
31
+ define_method_handler(:foo, :group => :testgroup) {
32
+ 10
33
+ }
34
+ end
35
+
36
+ chain = CHAIN2_1.new
37
+ chain.disable_handler_group(:testgroup) do
38
+ end
39
+
40
+ chain.foo.should be == 10
41
+ end
42
+
43
+ it "one method handler defined with group only should run inside enable_handler_group" do
44
+ class CHAIN2_1
45
+ define_method_handler(:foo, :group => :testgroup) {
46
+ 10
47
+ }
48
+ end
49
+
50
+ chain = CHAIN2_1.new
51
+ chain.disable_handler_group(:testgroup) do
52
+ chain.enable_handler_group(:testgroup) do
53
+ chain.foo.should be == 10
54
+ end
55
+ end
56
+ end
57
+
58
+ it "one method handler defined with group only should NOT run inside disable_handler_group block after enable_handler_group block" do
59
+ class CHAIN2_1
60
+ define_method_handler(:foo, :group => :testgroup) {
61
+ 10
62
+ }
63
+ end
64
+
65
+ chain = CHAIN2_1.new
66
+ chain.disable_handler_group(:testgroup) do
67
+ chain.enable_handler_group(:testgroup) do
68
+ end
69
+ chain.foo.should be == nil
70
+ end
71
+ end
72
+
73
+ it "one method handler defined with group only should run inside enable_handler_group, should run" do
74
+ class CHAIN2_1
75
+ define_method_handler(:foo, :group => :testgroup) {
76
+ 10
77
+ }
78
+ end
79
+
80
+ ret = nil
81
+ chain = CHAIN2_1.new
82
+ chain.disable_handler_group(:testgroup) do
83
+ chain.enable_handler_group(:testgroup) do
84
+ ret = chain.foo
85
+ end
86
+ end
87
+
88
+ ret.should be == 10
89
+ end
90
+
91
+ end
@@ -0,0 +1,82 @@
1
+ require "define_method_handler"
2
+
3
+
4
+ describe "define_method_handler" do
5
+
6
+ it "should allow use of scopes to define groups" do
7
+ class CHAIN4_1
8
+ handler_scope(:group => :testgroup) do
9
+ define_method_handler(:foo) do
10
+ 100
11
+ end
12
+ end
13
+ end
14
+
15
+ chain = CHAIN4_1.new
16
+ chain.disable_handler_group(:testgroup) do
17
+ chain.foo.should be == nil
18
+ end
19
+ end
20
+
21
+
22
+ it "method handler defined after handler_scope block should not be affected" do
23
+ class CHAIN4_2
24
+ handler_scope(:group => :testgroup) do
25
+ define_method_handler(:foo) do
26
+ 100
27
+ end
28
+ end
29
+
30
+ define_method_handler(:bar) do
31
+ 100
32
+ end
33
+ end
34
+
35
+ chain = CHAIN4_2.new
36
+ chain.disable_handler_group(:testgroup) do
37
+ chain.bar.should be == 100
38
+ end
39
+ end
40
+
41
+ it "nested handler_scopes should merge options" do
42
+ class CHAIN4_3
43
+ handler_scope(:group => :testgroup) do
44
+ handler_scope(:priority => 100) do
45
+ define_method_handler(:foo) do
46
+ 100
47
+ end
48
+ end
49
+ end
50
+
51
+ define_method_handler(:foo) do
52
+ 200
53
+ end
54
+ end
55
+
56
+ chain = CHAIN4_3.new
57
+ chain.foo.should be == 100
58
+
59
+ chain.disable_handler_group(:testgroup) do
60
+ chain.foo.should be == 200
61
+ end
62
+ end
63
+
64
+ it "nested handler_scopes with repeated options should prioritize the more nested" do
65
+ class CHAIN4_4
66
+ handler_scope(:priority => -100) do
67
+ handler_scope(:priority => 100) do
68
+ define_method_handler(:foo) do
69
+ 100
70
+ end
71
+ end
72
+ end
73
+
74
+ define_method_handler(:foo) do
75
+ 200
76
+ end
77
+ end
78
+
79
+ chain = CHAIN4_4.new
80
+ chain.foo.should be == 100
81
+ end
82
+ end
@@ -0,0 +1,33 @@
1
+ require "define_method_handler"
2
+
3
+
4
+ describe "define_method_handler" do
5
+
6
+ it "two methods with same name and with no conditions should execute the last implementation" do
7
+ class CHAIN1_1
8
+ define_method_handler(:foo) {
9
+ 1
10
+ }
11
+
12
+ define_method_handler(:foo) {
13
+ 2
14
+ }
15
+ end
16
+
17
+ CHAIN1_1.new.foo.should be == 2
18
+ end
19
+
20
+ it "two methods with same name and with no conditions should execute the implementation with higher priority" do
21
+ class CHAIN1_2
22
+ define_method_handler(:foo, :priority => 100) {
23
+ 1
24
+ }
25
+
26
+ define_method_handler(:foo, :priority => 1) {
27
+ 2
28
+ }
29
+ end
30
+
31
+ CHAIN1_2.new.foo.should be == 1
32
+ end
33
+ end
@@ -0,0 +1,44 @@
1
+ require "define_method_handler"
2
+
3
+
4
+ describe "define_method_handler" do
5
+
6
+ it "self inside define_method_handler should be the chain object" do
7
+ class CHAIN3_1
8
+ define_method_handler(:foo) do
9
+ self
10
+ end
11
+ end
12
+ chain = CHAIN3_1.new
13
+ chain.foo.should be == chain
14
+ end
15
+
16
+ it "should allow calling instance methods from method handler" do
17
+ class CHAIN3_2
18
+ def helper
19
+ 92
20
+ end
21
+
22
+ define_method_handler(:foo) do
23
+ helper
24
+ end
25
+ end
26
+ chain = CHAIN3_2.new
27
+ chain.foo.should be == 92
28
+ end
29
+
30
+ it "should allow calling private instance methods from method handler" do
31
+ class CHAIN3_3
32
+ define_method_handler(:foo) do
33
+ helper
34
+ end
35
+
36
+ private
37
+ def helper
38
+ 92
39
+ end
40
+ end
41
+ chain = CHAIN3_3.new
42
+ chain.foo.should be == 92
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: define_method_handler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dario Seminara
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-25 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Chain of responsability implementation in a ruby fashion way
15
+ email: robertodarioseminara@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - README
20
+ files:
21
+ - spec/priority_spec.rb
22
+ - spec/base_spec.rb
23
+ - spec/handler_scope_spec.rb
24
+ - spec/group_spec.rb
25
+ - spec/scope_spec.rb
26
+ - AUTHORS
27
+ - README
28
+ - Rakefile
29
+ - TODO
30
+ - CHANGELOG
31
+ homepage: http://github.com/tario/define_method_handler
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.10
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Chain of responsability implementation in a ruby fashion way
55
+ test_files: []