carry_out 0.2.9 → 0.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 15539f051ef8fbb7bde23ab9027e1a309b318eb6
4
- data.tar.gz: 75df54ddbd00851be278a80faaa350e2483ad0c5
3
+ metadata.gz: 4b9327924b9bfe1b3e1bcfcf84d550d9b9128da1
4
+ data.tar.gz: 5f9be969be455b38060313cf57defaaea7e6b330
5
5
  SHA512:
6
- metadata.gz: c9df9545f0b391a1f31471ecbc1b85a4f5257fd9399144c75d55643cb787620ac7486dc46de7ee297877779a975b7b3a1817ac14ad62b31172895f9a851c581c
7
- data.tar.gz: 33bcc7bc1820b9c2dbd2bd8a084f6a8168d145e854d6ee50ad0fd0ae370ce8a8255ba0a361dadc97201b277353a5b6f1b1d283063d75935f7a3edac6702593dd
6
+ metadata.gz: 8f1cdd23f90dd6b0a540511597d5873b7b15ab9b395ff6c983add20694ee5b99eaa85da1dd54102d3d8bbad09aca8fd17f238da3c05dfb2b81b32e71fd912314
7
+ data.tar.gz: cb681e5628b28c3e9d0ba7979f8582379354fb4393113e50b5339803035c5509cdde3e6f34361606b2ef4211b4c98b6454adcc37265cd77e0d2d18e8948246fe
data/README.md CHANGED
@@ -241,7 +241,7 @@ CarryOut.defaults = {
241
241
  }
242
242
  ```
243
243
 
244
- #### Magic will\_, then\_, and within\_
244
+ #### will\_, then\_, and within\_ Directives
245
245
 
246
246
  The magic versions of `will`, `then`, and `within` will use the configured search strategy to convert the remaning portion of the directive into a class reference.
247
247
 
@@ -256,7 +256,7 @@ end
256
256
  plan = CarryOut.will_say_hello
257
257
  ```
258
258
 
259
- #### Magic returning\_as\_
259
+ #### returning\_as\_ Directive
260
260
 
261
261
  The magic `returning_as_` directive is an alternative to passing the `as:` option to a `will`/`then` directive. The remainder of the directive becomes the key symbol into which the unit's return value will be stored.
262
262
 
@@ -268,6 +268,45 @@ plan = CarryOut
268
268
  .message(CarryOut.get(:message))
269
269
  ```
270
270
 
271
+ #### result\_of\_ Directive
272
+
273
+ The magic `result_of_` directive is available within blocks passed to parameter methods. The remainder of the directive becomes the context key symbol from which the value will be retreived.
274
+
275
+ ```ruby
276
+ plan = CarryOut
277
+ .will_receive_message
278
+ .returning_as_message
279
+ .then_log
280
+ .message { result_of_message }
281
+ # instead of .message(CarryOut.get(:message)
282
+ # or .message { |refs| refs.message }
283
+ ```
284
+
285
+ #### Example using all available magic
286
+
287
+ While a contrived example, the following illustrates the improved readability of a plan when using the magic directives.
288
+
289
+ ```ruby
290
+ plan = CarryOut
291
+ .within_order_transaction
292
+ .will_order_bagel
293
+ .flavored('everything')
294
+ .toasted
295
+ .topped_with('butter')
296
+ .and('strawberry cream cheese')
297
+ .returning_as_bagel
298
+ .then_order_coffee
299
+ .with_cream
300
+ .and_sugar
301
+ .then_calculate_order_total
302
+ .for { result_of_bagel }
303
+ .and { result_of_coffee }
304
+ .then_swipe_credit_card
305
+ .returning_as_cc
306
+ .then_pay
307
+ .with_credit_card { result_of_cc }
308
+ ```
309
+
271
310
  ## Motivation
272
311
 
273
312
  I've been trying to keep my Rails controllers clean, but I prefer to avoid shoving inter-model business logic inside database models. The recommendation I most frequently run into is to move that kind of logic into something akin to service objects. I like that idea, but I want to keep my services small and composable, and I want to separate the "what" from the "how" of my logic.
@@ -0,0 +1,17 @@
1
+ module CarryOut
2
+ class Context
3
+ MATCH_RESULT_METHOD = /^result_of_(.+)/
4
+
5
+ def initialize(context)
6
+ @context = context
7
+ end
8
+
9
+ def method_missing(method, *args, &block)
10
+ if MATCH_RESULT_METHOD =~ method && args.empty? && block.nil?
11
+ @context[$1.to_sym]
12
+ else
13
+ super
14
+ end
15
+ end
16
+ end
17
+ end
@@ -15,10 +15,6 @@ module CarryOut
15
15
  end
16
16
  end
17
17
 
18
- def raises?(klass)
19
- @unitClass.raises?(klass)
20
- end
21
-
22
18
  def respond_to?(method)
23
19
  @unitClass.instance_methods.include?(method) || super
24
20
  end
@@ -27,7 +23,13 @@ module CarryOut
27
23
  unit = @unitClass.respond_to?(:execute) ? @unitClass : @unitClass.new
28
24
 
29
25
  @messages.each do |message|
30
- arg = message[:block] ? message[:block].call(context) : message[:argument]
26
+ arg =
27
+ if message[:block]
28
+ Context.new(context).instance_exec(context, &message[:block])
29
+ else
30
+ message[:argument]
31
+ end
32
+
31
33
  unit.send(message[:method], arg)
32
34
  end
33
35
 
@@ -44,13 +46,13 @@ module CarryOut
44
46
 
45
47
  private
46
48
  def append_message(method, *args, &block)
49
+ if !args.first.nil? && !block.nil?
50
+ raise ArgumentError.new("Arguments, references, and blocks are mutually exclusive")
51
+ end
52
+
47
53
  if @unitClass.instance_methods.include?(method)
48
54
  if args.first.kind_of?(Reference)
49
- if block.nil?
50
- @messages << { method: method, block: args.first }
51
- else
52
- raise ArgumentError.new("References and blocks are mutually exclusive")
53
- end
55
+ @messages << { method: method, block: -> (refs) { args.first.call(refs) } }
54
56
  else
55
57
  @messages << { method: method, argument: args.first || true, block: block }
56
58
  end
@@ -1,3 +1,3 @@
1
1
  module CarryOut
2
- VERSION = "0.2.9"
2
+ VERSION = "0.2.10"
3
3
  end
data/lib/carry_out.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "carry_out/version"
2
2
 
3
+ require "carry_out/context"
3
4
  require "carry_out/error"
4
5
  require "carry_out/plan"
5
6
  require "carry_out/plan_node"
@@ -58,12 +59,11 @@ module CarryOut
58
59
  default_carry_out.send(method, *args, &block)
59
60
  end
60
61
 
61
- private
62
- def self.default_options
63
- @default_options ||= Hash.new
64
- end
62
+ def self.default_options
63
+ @default_options ||= Hash.new
64
+ end
65
65
 
66
- def self.default_carry_out
67
- @default_carry_out ||= ConfiguredCarryOut.new(default_options)
68
- end
66
+ def self.default_carry_out
67
+ @default_carry_out ||= ConfiguredCarryOut.new(default_options)
68
+ end
69
69
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carry_out
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.9
4
+ version: 0.2.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Fields
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-26 00:00:00.000000000 Z
11
+ date: 2017-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -88,6 +88,7 @@ files:
88
88
  - bin/setup
89
89
  - carry_out.gemspec
90
90
  - lib/carry_out.rb
91
+ - lib/carry_out/context.rb
91
92
  - lib/carry_out/error.rb
92
93
  - lib/carry_out/plan.rb
93
94
  - lib/carry_out/plan_node.rb