atacama 0.1.3 → 0.1.4

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: e1dd85acd949dc1261176c76b76e38add8ab8772
4
- data.tar.gz: 3d0006d2e584122ca3534748f620e0a335acad5b
3
+ metadata.gz: 504860bd1cc4b868e1299d7e43ea02a29efdf385
4
+ data.tar.gz: 56b29c7f192e6513fb1b039397ea55fe3cd54d7c
5
5
  SHA512:
6
- metadata.gz: f3938b99da71d5b9994c8993b01b42847c9e09dff9523b7d9650a7abe977d91aff71a60169378d9207ad65e1a8eb1cd51c0846b33bda4d4724396d61815115b2
7
- data.tar.gz: 1bd1146d793105404d1ea9e5f27b7c310c695ac378babb683d1cae83dace8af5a0ad1b017fc743c0ba1fd4f3e3d2011c40f1bb9d0684d3f24d29fe2c4c146782
6
+ metadata.gz: 38ae0a44fda886f8ce080f0e6734863094c7d34ed5d3a54497741602328012b5f6949f0c1852c043009e0909216428fd0212fb8f4747250e8afdea33215b7fbc
7
+ data.tar.gz: 2116d76499411e0bcb3183ebd83338f37e05f48548bbbfc8545eb092df6746e9329e0f4334b6437f1d85839cd1f7c62632e4fa2a30b3da709363c0adc11a3834
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- atacama (0.1.3)
4
+ atacama (0.1.4)
5
5
  dry-types (~> 0.13.2)
6
6
 
7
7
  GEM
@@ -9,6 +9,23 @@ module Atacama
9
9
  module Types
10
10
  include Dry::Types.module
11
11
  Boolean = Types::True | Types::False
12
+
13
+ def self.Option(**map)
14
+ Instance(Values::Option).constructor do |options|
15
+ map.each do |key, type|
16
+ type[options.value[key]]
17
+ end
18
+
19
+ options
20
+ end
21
+ end
22
+
23
+ def self.Return(type)
24
+ Instance(Values::Return).constructor do |options|
25
+ type[options.value]
26
+ options
27
+ end
28
+ end
12
29
  end
13
30
 
14
31
  # This class enables a DSL for creating a contract for the initializer
@@ -26,13 +43,26 @@ module Atacama
26
43
  end
27
44
 
28
45
  def injected
29
- @injected || {}
46
+ # Silences the VM warning about accessing uninitalized ivar
47
+ defined?(@injected) ? @injected : {}
30
48
  end
31
49
 
32
50
  def options
33
51
  @options ||= {}
34
52
  end
35
53
 
54
+ def returns(type)
55
+ @returns = type
56
+ end
57
+
58
+ def return_type
59
+ defined?(@returns) && @returns
60
+ end
61
+
62
+ def validate_return(value)
63
+ return_type && return_type[value]
64
+ end
65
+
36
66
  # Define an initializer value.
37
67
  # @param [Symbol] name of the argument
38
68
  def option(name, **kwargs)
@@ -43,12 +73,14 @@ module Atacama
43
73
  end
44
74
 
45
75
  def call(context = {})
46
- new(context: context).call
76
+ new(context: context).call.tap do |result|
77
+ validate_return(result)
78
+ end
47
79
  end
48
80
 
49
81
  def inject(injected)
50
- Class.new(self) do
51
- self.injected = injected
82
+ clone.tap do |clone|
83
+ clone.injected = injected
52
84
  end
53
85
  end
54
86
  end
@@ -65,6 +97,16 @@ module Atacama
65
97
  Validator.call(options: self.class.options, context: @context)
66
98
  end
67
99
 
100
+ # Pretty pretty printing.
101
+ def inspect
102
+ "#<#{self.class}:0x%x %s>" % [
103
+ object_id,
104
+ self.class.options.keys.map do |option|
105
+ "#{option}: #{context.send(option).inspect}"
106
+ end.join(' ')
107
+ ]
108
+ end
109
+
68
110
  def call
69
111
  self
70
112
  end
@@ -15,6 +15,19 @@ module Atacama
15
15
  end
16
16
 
17
17
  class << self
18
+ attr_reader :return_option
19
+
20
+ def returns_option(key, type)
21
+ @return_option = key
22
+
23
+ returns(
24
+ Types.Instance(Result).constructor do |options|
25
+ type[options.value]
26
+ options
27
+ end
28
+ )
29
+ end
30
+
18
31
  # @returns [Array<Atacama::Transaction::Definition>]
19
32
  def steps
20
33
  @steps ||= []
@@ -32,26 +45,21 @@ module Atacama
32
45
  def initialize(context: {}, steps: {})
33
46
  super(context: context)
34
47
  @overrides = steps
48
+ @return_value = nil
35
49
  end
36
50
 
37
51
  def call
38
- value = begin
39
- execute(self.class.steps)
40
- rescue HaltExecution => exception
41
- exception.value
42
- end
43
-
44
- raise MissingReturn, "Return value from #{self.class} missing, received: #{value.inspect}" unless value.is_a? Values::Return
45
-
46
- Result.call(value: value.value, transaction: context)
52
+ execute(self.class.steps)
53
+ Result.call(value: return_value, transaction: context)
47
54
  end
48
55
 
49
56
  private
50
57
 
51
58
  def execute(steps)
52
59
  steps.each do |step|
60
+ break if @return_value
53
61
  evaluate(step).tap do |result|
54
- raise HaltExecution.new(result) if result.is_a? Values::Return
62
+ @return_value = result.value if result.is_a? Values::Return
55
63
  context.merge!(result.value) if result.is_a? Values::Option
56
64
  end
57
65
  end
@@ -90,9 +98,15 @@ module Atacama
90
98
  end
91
99
 
92
100
  def evaluate_instance(step)
93
- step.with.new(context: context).call do
94
- execute(step.yielding.steps)
95
- end
101
+ step.with.new(context: context) \
102
+ .call { execute(step.yielding.steps) }
103
+ .tap { |result| step.with.validate_return(result) }
104
+ end
105
+
106
+ def return_value
107
+ @return_value ||
108
+ (self.class.return_option && context[self.class.return_option]) ||
109
+ nil
96
110
  end
97
111
  end
98
112
  end
@@ -1,3 +1,3 @@
1
1
  module Atacama
2
- VERSION = '0.1.3'.freeze
2
+ VERSION = '0.1.4'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: atacama
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Johnston
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-02 00:00:00.000000000 Z
11
+ date: 2018-11-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-types