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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d1ff1b176f95aee6273a5c6430aeb8526556d86d
4
- data.tar.gz: d000af86cf1971ae3eab07daa4eabc1d4efc9d47
3
+ metadata.gz: adf9c289386b87ace3d0f17b7eebc933cbccfbf1
4
+ data.tar.gz: cbcc803fb733d19c427d7d2d988ad03bb6dbf762
5
5
  SHA512:
6
- metadata.gz: 012aa1a1164f252d9acb9005f3de8fb76cdf2f00cb019216ea3fa09f5ac561fcfaece33eb3efe3fbda85335a8eb8bbf23313ab138790a9292e236e5b4df49205
7
- data.tar.gz: 3c3d4398e97b29311a030d305cb15a7c24924919e2e9670febdaf1d52743dbf346c06bf2b874c733ee9d59f142aed8cf944d95c556611b7019446fe02a7a7f1e
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, :block
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 = {}, &block)
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 = block.call(options[:on_self] ? object : result)
27
+ self.result = action_block.call(options[:on_self] ? object : result)
28
28
  end
29
29
 
30
30
  private
@@ -1,27 +1,26 @@
1
- class Caseable
2
- attr_reader :object
1
+ module Cases
2
+ class Caseable
3
+ attr_reader :object, :runtime_block
3
4
 
4
- def self.execute(result, &block)
5
- new(result).execute(result, &block)
6
- end
5
+ def initialize(object, &runtime_block)
6
+ @object = object
7
+ @runtime_block = runtime_block
8
+ end
7
9
 
8
- def initialize(object)
9
- @object = object
10
- end
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
- def execute(original_result, &block)
13
- block.call(self)
14
- case_blocks.reduce(original_result) { |result, case_block| case_block.call(result) }
15
- end
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
- private
20
+ private
23
21
 
24
- def case_blocks
25
- @_case_blocks ||= []
22
+ def case_blocks
23
+ @_case_blocks ||= []
24
+ end
26
25
  end
27
26
  end
data/lib/cases/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Cases
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
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, cases)
14
- if block_given?
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
- define_execute_cases(method)
16
+ __cases_define_execute_cases(method)
23
17
  end
24
18
 
25
19
  def define_caseable(method)
26
- proxy_result(method) { |result, &block| Caseable.execute(result, &block) }
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 define_case_with_block(method, event, &block)
36
- Case.new(method, event, &block)
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 define_execute_cases(method)
40
- define_method "execute_#{method}_cases" do |&block|
41
- self.class.cases[method].find { |kase| kase.execute(self, block.call) }.result
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, "execute_#{method}_cases")
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.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-12 00:00:00.000000000 Z
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