cases 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -4
- data/lib/cases/case.rb +4 -4
- data/lib/cases/caseable.rb +19 -20
- data/lib/cases/version.rb +1 -1
- data/lib/cases.rb +16 -20
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: adf9c289386b87ace3d0f17b7eebc933cbccfbf1
|
4
|
+
data.tar.gz: cbcc803fb733d19c427d7d2d988ad03bb6dbf762
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 77a44291c3382c283e21771ef13b960c88e3af55b3a45d68081b947b65f8648a112973d4681e3066c9c3516f1df5e00e3f8d25d3c4f7961b19f394f2272b41ed
|
7
|
+
data.tar.gz: 3df9092cde054df3786bc60db2d2f3b473759da2b979829806245fe0c38b6582ff5f199d0a7e654c5b3ed5477beef35e4e92d1cb355532f753945d5cc9da4855
|
data/README.md
CHANGED
@@ -88,8 +88,8 @@ end
|
|
88
88
|
> test_case.test_without_block
|
89
89
|
=> "success message from cased object"
|
90
90
|
> test_case.test_caseable do |on|
|
91
|
-
on.success? { |response| "#{response.success_message} through caseable"
|
92
|
-
on.failed? { |response| "#{response.failed_message} through caseable"
|
91
|
+
on.success? { |response| "#{response.success_message} through caseable" }
|
92
|
+
on.failed? { |response| "#{response.failed_message} through caseable" }
|
93
93
|
end
|
94
94
|
=> "success message from test response through caseable"
|
95
95
|
>
|
@@ -101,8 +101,8 @@ end
|
|
101
101
|
> test_case.test_without_block
|
102
102
|
=> "failed message from cased object"
|
103
103
|
> test_case.test_caseable do |on|
|
104
|
-
on.success? { |response| "#{response.success_message} through caseable"
|
105
|
-
on.failed? { |response| "#{response.failed_message} through caseable"
|
104
|
+
on.success? { |response| "#{response.success_message} through caseable" }
|
105
|
+
on.failed? { |response| "#{response.failed_message} through caseable" }
|
106
106
|
end
|
107
107
|
=> "failed message from test response through caseable"
|
108
108
|
```
|
data/lib/cases/case.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
module Cases
|
2
2
|
class Case
|
3
3
|
attr_accessor :result
|
4
|
-
attr_reader :method, :event, :options, :
|
4
|
+
attr_reader :method, :event, :options, :action_block
|
5
5
|
|
6
6
|
def self.all
|
7
7
|
@_all ||= {}
|
8
8
|
end
|
9
9
|
|
10
|
-
def initialize(method, event, options = {}, &
|
10
|
+
def initialize(method, event, options = {}, &action_block)
|
11
11
|
@method = method
|
12
12
|
@event = event
|
13
|
-
@block = block
|
14
13
|
@options = options
|
14
|
+
@action_block = action_block
|
15
15
|
|
16
16
|
register
|
17
17
|
end
|
@@ -24,7 +24,7 @@ module Cases
|
|
24
24
|
def execute(object, result)
|
25
25
|
return if !result.send(event)
|
26
26
|
|
27
|
-
self.result =
|
27
|
+
self.result = action_block.call(options[:on_self] ? object : result)
|
28
28
|
end
|
29
29
|
|
30
30
|
private
|
data/lib/cases/caseable.rb
CHANGED
@@ -1,27 +1,26 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module Cases
|
2
|
+
class Caseable
|
3
|
+
attr_reader :object, :runtime_block
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
def initialize(object, &runtime_block)
|
6
|
+
@object = object
|
7
|
+
@runtime_block = runtime_block
|
8
|
+
end
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
|
10
|
+
def execute
|
11
|
+
runtime_block.call(self)
|
12
|
+
case_blocks.reduce(object) { |result, case_block| case_block.call(result) }
|
13
|
+
end
|
11
14
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
def method_missing(method, &block)
|
18
|
-
case_blocks << block if object.send(method)
|
19
|
-
case_blocks
|
20
|
-
end
|
15
|
+
def method_missing(method, &case_block)
|
16
|
+
case_blocks << case_block if object.send(method)
|
17
|
+
case_blocks
|
18
|
+
end
|
21
19
|
|
22
|
-
|
20
|
+
private
|
23
21
|
|
24
|
-
|
25
|
-
|
22
|
+
def case_blocks
|
23
|
+
@_case_blocks ||= []
|
24
|
+
end
|
26
25
|
end
|
27
26
|
end
|
data/lib/cases/version.rb
CHANGED
data/lib/cases.rb
CHANGED
@@ -10,38 +10,34 @@ module Cases
|
|
10
10
|
end
|
11
11
|
|
12
12
|
module ClassMethods
|
13
|
-
def define_case(method,
|
14
|
-
|
15
|
-
define_case_with_block(method, cases, &Proc.new)
|
16
|
-
else
|
17
|
-
cases.each_pair do |event, action|
|
18
|
-
Case.new(method, event, { on_self: true }, &Proc.new { |object| object.send(action) })
|
19
|
-
end
|
20
|
-
end
|
13
|
+
def define_case(method, kase)
|
14
|
+
block_given? ? __cases_define_with_block(method, kase, &Proc.new) : __cases_define_without_block(method, kase)
|
21
15
|
|
22
|
-
|
16
|
+
__cases_define_execute_cases(method)
|
23
17
|
end
|
24
18
|
|
25
19
|
def define_caseable(method)
|
26
|
-
proxy_result(method) { |result, &
|
27
|
-
end
|
28
|
-
|
29
|
-
def cases
|
30
|
-
Case.all
|
20
|
+
proxy_result(method) { |result, &runtime_block| Cases::Caseable.new(result, &runtime_block).execute }
|
31
21
|
end
|
32
22
|
|
33
23
|
private
|
34
24
|
|
35
|
-
def
|
36
|
-
Case.new(method, event, &
|
25
|
+
def __cases_define_with_block(method, *events, &action_block)
|
26
|
+
events.each { |event| Cases::Case.new(method, event, &action_block) }
|
27
|
+
end
|
28
|
+
|
29
|
+
def __cases_define_without_block(method, kase)
|
30
|
+
kase.each_pair do |event, action|
|
31
|
+
Cases::Case.new(method, event, { on_self: true }, &Proc.new { |object| object.send(action) })
|
32
|
+
end
|
37
33
|
end
|
38
34
|
|
39
|
-
def
|
40
|
-
define_method "
|
41
|
-
|
35
|
+
def __cases_define_execute_cases(method)
|
36
|
+
define_method "__cases_execute_#{method}_cases" do |&block|
|
37
|
+
Cases::Case.all[method].find { |kase| kase.execute(self, block.call) }.result
|
42
38
|
end
|
43
39
|
|
44
|
-
around_method(method, "
|
40
|
+
around_method(method, "__cases_execute_#{method}_cases")
|
45
41
|
end
|
46
42
|
end
|
47
43
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cases
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Morgan Showman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
version_requirements: !ruby/object:Gem::Requirement
|