business_flow 0.5.2 → 0.6.0

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: 20dd5eb0ff89e49ebf99e3a47ec7383e8db07320
4
- data.tar.gz: 23f9387a1ba8bea2d3d8d9afee4384bf002e0876
3
+ metadata.gz: b485a68eacf74616f5f9e0fe635e210e017c9249
4
+ data.tar.gz: a3f786a40733abc5299058190545226953178450
5
5
  SHA512:
6
- metadata.gz: 23423eeb84a68119631859eaef316e9d18532c1c65d58ff3ee2cfb480026ff01e076d8f393a98395870df67252b4f998819d74322275ad5eb3abfa46afae3fd8
7
- data.tar.gz: da02de0322b75a90946504689243862f4595e2a0adb7bff77919af7c309dd69723acebd5a0717d53729a123ad254f6087b47a526f140fe96bdb46f16b2b454e4
6
+ metadata.gz: f74896456557f2042328b355973aabbe77b311466f76470fff9483a6d13a732c4847da248848bc61d4609115be75ff3eed27927e0c717ddaee58de7784fdaf25
7
+ data.tar.gz: 327b5cda58ad5add151556a4ad164ec360f0a0421f66827f4ba431f8bc21b075131f92aabc113b4ff491c80029357c9188b01e9c194801a7c543beadfdfb9990
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- business_flow (0.5.2)
4
+ business_flow (0.6.0)
5
5
  activemodel (>= 3.0)
6
6
  activesupport (>= 3.0)
7
7
 
@@ -1,9 +1,5 @@
1
- # Magic!
2
1
  module BusinessFlow
3
- # More magic!
4
- # call! to raise errors
5
- # Hooks for cross cutting concerns
6
- # Figure out how much we can freeze
2
+ # include BusinessFlow::Base in any object to get a flow!
7
3
  # :reek:ModuleInitialize
8
4
  module Base
9
5
  def self.included(klass)
@@ -24,7 +24,7 @@ module BusinessFlow
24
24
  if @callable.is_a?(Proc)
25
25
  @cached_proc = proc_callable
26
26
  elsif @callable.respond_to?(:call)
27
- @cached_proc = proc { |_, inputs| @callable.call(inputs) }
27
+ @cached_proc = call_callable
28
28
  elsif !@callable.is_a?(Symbol)
29
29
  raise ArgumentError, 'callable must be a symbol or respond to #call'
30
30
  end
@@ -52,6 +52,19 @@ module BusinessFlow
52
52
  end
53
53
  end
54
54
 
55
+ def call_callable
56
+ proc do |instance, inputs|
57
+ case @callable.method(:call).arity
58
+ when 1, -1
59
+ @callable.call(inputs)
60
+ when 0
61
+ @callable.call
62
+ else
63
+ @callable.call(instance, inputs)
64
+ end
65
+ end
66
+ end
67
+
55
68
  def lookup_callable
56
69
  constant_name = @callable.to_s.camelcase
57
70
  @metaclass.parents.each do |parent|
@@ -88,6 +88,33 @@ module BusinessFlow
88
88
  # Keep our internal helpers in a different module to avoid polluting the
89
89
  # namespace of whoever includes us.
90
90
  module PrivateHelpers
91
+ # Handle some logic around conditions
92
+ class ConditionList
93
+ def initialize(if_stmts, unless_stmts, klass)
94
+ @klass = klass
95
+ @conditions = Array.wrap(if_stmts).map(&method(:to_if)) +
96
+ Array.wrap(unless_stmts).map(&method(:to_unless))
97
+ end
98
+
99
+ def call(instance, inputs)
100
+ @conditions.all? { |cond| cond.call(instance, inputs) }
101
+ end
102
+
103
+ private
104
+
105
+ def to_if(cond)
106
+ Callable.new(cond, @klass)
107
+ end
108
+
109
+ def to_unless(cond)
110
+ if_stmt = to_if(cond)
111
+ unless_stmt = proc do |instance, input|
112
+ !if_stmt.call(instance, input)
113
+ end
114
+ to_if(unless_stmt)
115
+ end
116
+ end
117
+
91
118
  def self.create_parameter_field(klass, field)
92
119
  klass.send(:define_method, field) do
93
120
  if parameter_object.is_a?(Hash) && parameter_object.key?(field)
@@ -96,11 +123,18 @@ module BusinessFlow
96
123
  parameter_object.public_send(field)
97
124
  end
98
125
  end
126
+ klass.send(:private, field)
99
127
  end
100
128
 
101
129
  def self.create_conditional_callable(klass, opts)
102
- condition = opts[:if]
103
- condition && Callable.new(condition, klass)
130
+ return unless opts[:if] || opts[:unless]
131
+ Callable.new(condition(klass, opts), klass)
132
+ end
133
+
134
+ def self.condition(klass, opts)
135
+ if_stmts = opts.fetch(:if, proc { true })
136
+ unless_stmts = opts.fetch(:unless, proc { false })
137
+ ConditionList.new(if_stmts, unless_stmts, klass)
104
138
  end
105
139
 
106
140
  def self.create_field(klass, field)
@@ -1,3 +1,3 @@
1
1
  module BusinessFlow
2
- VERSION = '0.5.2'.freeze
2
+ VERSION = '0.6.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: business_flow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Scarborough
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-03-28 00:00:00.000000000 Z
11
+ date: 2018-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel