active_interaction 0.2.2 → 0.3.0

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: fe823793d930b947cf93fb8c5e9653b7bcd63e49
4
- data.tar.gz: 9bfec14c2454c235e20e1ab347c927680ddb7101
3
+ metadata.gz: 58a3ed3559d4e08664cc19fb81100a947bb597e8
4
+ data.tar.gz: d7d1152399851b34028504b90b05a5cfda76e14f
5
5
  SHA512:
6
- metadata.gz: f469a6ed3d1efa8afab46b59a2c1c910ce4fa0a089524d892f04da1c9e7a17a8450a49d3c445f16c19a9df228606271fde3a33ab0cdf4049cc00eca2612e544c
7
- data.tar.gz: 1abe93f04ed2ff447e0a36189b41386e98bfbf9abeb4f19d5a849aae461a0becf5c141be7464d88511031a225010a9cf96facb9d0145002ce5abd654d26fe6dc
6
+ metadata.gz: 96954a543eb586113be298cef976bbbdc5bb1eeed62de460d5b42ac3ad3c2c56896483729ad1c4a012ef08757b1e92a47848b9b7db7c903ebea3f3455967cfba
7
+ data.tar.gz: 046393507e5bb5acfea4db4bd0b1e95222d8dfafd156bb5289668c0139153ce1c9e9a643d1b3fc7c41d5bcdd444df5abc47c300301a9721a8757471eea2976e7
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # [Master][]
2
2
 
3
+ # [0.3.0][] (2013-08-07)
4
+
5
+ - Give better error messages for nested attributes.
6
+ - Use default value when given an explicit `nil`.
7
+ - Allow nested default values.
8
+
3
9
  # [0.2.2][] (2013-08-07)
4
10
 
5
11
  - Fix support for `ActiveSupport::TimeWithZone`.
@@ -33,7 +39,8 @@
33
39
 
34
40
  - Initial release.
35
41
 
36
- [master]: https://github.com/orgsync/active_interaction/compare/v0.2.2...master
42
+ [master]: https://github.com/orgsync/active_interaction/compare/v0.3.0...master
43
+ [0.3.0]: https://github.com/orgsync/active_interaction/compare/v0.2.2...v0.3.0
37
44
  [0.2.2]: https://github.com/orgsync/active_interaction/compare/v0.2.1...v0.2.2
38
45
  [0.2.1]: https://github.com/orgsync/active_interaction/compare/v0.2.0...v0.2.1
39
46
  [0.2.0]: https://github.com/orgsync/active_interaction/compare/v0.1.3...v0.2.0
data/README.md CHANGED
@@ -22,7 +22,7 @@ This project uses [semantic versioning][].
22
22
  Add it to your Gemfile:
23
23
 
24
24
  ```ruby
25
- gem 'active_interaction', '~> 0.2.2'
25
+ gem 'active_interaction', '~> 0.3.0'
26
26
  ```
27
27
 
28
28
  And then execute:
@@ -121,7 +121,9 @@ module ActiveInteraction
121
121
  # @return The return value of {#execute}.
122
122
  def self.run!(options = {})
123
123
  outcome = run(options)
124
- raise InteractionInvalid if outcome.invalid?
124
+ if outcome.invalid?
125
+ raise InteractionInvalid, outcome.errors.full_messages.join(', ')
126
+ end
125
127
  outcome.result
126
128
  end
127
129
 
@@ -144,7 +146,7 @@ module ActiveInteraction
144
146
  begin
145
147
  default = filter.
146
148
  prepare(attribute, options[:default], options, &block)
147
- rescue InvalidValue
149
+ rescue InvalidNestedValue, InvalidValue
148
150
  raise InvalidDefaultValue
149
151
  end
150
152
  end
@@ -170,7 +172,7 @@ module ActiveInteraction
170
172
  value =
171
173
  begin
172
174
  filter.prepare(attribute, value, options, &block)
173
- rescue InvalidValue, MissingValue
175
+ rescue InvalidNestedValue, InvalidValue, MissingValue
174
176
  value
175
177
  end
176
178
  instance_variable_set("@#{attribute}", value)
@@ -188,11 +190,13 @@ module ActiveInteraction
188
190
  define_method(validator) do
189
191
  begin
190
192
  filter.prepare(attribute, send(attribute), options, &block)
191
- rescue MissingValue
192
- errors.add(attribute, 'is required')
193
+ rescue InvalidNestedValue
194
+ errors.add(attribute, 'is invalid')
193
195
  rescue InvalidValue
194
196
  errors.add(attribute,
195
197
  "is not a valid #{type.to_s.humanize.downcase}")
198
+ rescue MissingValue
199
+ errors.add(attribute, 'is required')
196
200
  end
197
201
  end
198
202
  private validator
@@ -1,6 +1,7 @@
1
1
  module ActiveInteraction
2
2
  class InteractionInvalid < ::StandardError; end
3
3
  class InvalidDefaultValue < ::StandardError; end
4
+ class InvalidNestedValue < ::StandardError; end
4
5
  class InvalidValue < ::StandardError; end
5
6
  class MissingValue < ::StandardError; end
6
7
  end
@@ -20,6 +20,8 @@ module ActiveInteraction
20
20
  when NilClass
21
21
  if options[:allow_nil]
22
22
  nil
23
+ elsif options.has_key?(:default)
24
+ options[:default]
23
25
  else
24
26
  raise MissingValue
25
27
  end
@@ -8,10 +8,6 @@ module ActiveInteraction
8
8
 
9
9
  @attribute = args.shift if args.first.is_a?(Symbol)
10
10
  @options = (args.first || {}).dup
11
-
12
- if @options.include?(:default)
13
- raise ArgumentError, ':default is not supported inside filter blocks'
14
- end
15
11
  end
16
12
  end
17
13
  end
@@ -41,6 +41,8 @@ module ActiveInteraction
41
41
  Filter.factory(method.method_name).
42
42
  prepare(method.attribute, value, method.options, &method.block)
43
43
  end
44
+ rescue InvalidValue, MissingValue
45
+ raise InvalidNestedValue
44
46
  end
45
47
  private_class_method :convert_values
46
48
 
@@ -25,7 +25,7 @@ module ActiveInteraction
25
25
  def self.prepare(key, value, options = {}, &block)
26
26
  case value
27
27
  when Hash
28
- convert_values(value.dup, &block)
28
+ convert_values(value.merge(options[:default] || {}), &block)
29
29
  else
30
30
  super
31
31
  end
@@ -41,6 +41,8 @@ module ActiveInteraction
41
41
  end
42
42
 
43
43
  hash
44
+ rescue InvalidValue, MissingValue
45
+ raise InvalidNestedValue
44
46
  end
45
47
  private_class_method :convert_values
46
48
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveInteraction
2
- VERSION = Gem::Version.new('0.2.2')
2
+ VERSION = Gem::Version.new('0.3.0')
3
3
  end
@@ -32,11 +32,6 @@ describe ActiveInteraction::FilterMethod do
32
32
  before { args << options }
33
33
 
34
34
  include_examples 'instance variable assignment'
35
-
36
- it 'does not allow :default' do
37
- options.merge!(default: nil)
38
- expect { filter_method }.to raise_error ArgumentError
39
- end
40
35
  end
41
36
 
42
37
  context 'with a block' do
@@ -28,7 +28,9 @@ describe ActiveInteraction::ArrayFilter do
28
28
  let(:value) { [Object.new] }
29
29
 
30
30
  it 'raises an error' do
31
- expect { result }.to raise_error ActiveInteraction::InvalidValue
31
+ expect {
32
+ result
33
+ }.to raise_error ActiveInteraction::InvalidNestedValue
32
34
  end
33
35
  end
34
36
  end
@@ -28,7 +28,9 @@ describe ActiveInteraction::HashFilter do
28
28
  let(:value) { { a: Object.new } }
29
29
 
30
30
  it 'raises an error' do
31
- expect { result }.to raise_error ActiveInteraction::InvalidValue
31
+ expect {
32
+ result
33
+ }.to raise_error ActiveInteraction::InvalidNestedValue
32
34
  end
33
35
  end
34
36
  end
@@ -53,17 +53,23 @@ describe ArrayInteraction do
53
53
  end
54
54
  end
55
55
 
56
- context 'with an invalidly nested default' do
57
- it 'raises an error' do
58
- expect {
59
- klass = Class.new(ActiveInteraction::Base) do
60
- array :a do
61
- array default: []
62
- end
56
+ context 'with a validly nested default' do
57
+ let(:described_class) do
58
+ Class.new(ActiveInteraction::Base) do
59
+ array :a do
60
+ array default: [rand]
63
61
  end
64
- # TODO: Fail when defining class, not running it.
65
- klass.run(a: [])
66
- }.to raise_error ArgumentError
62
+ def execute; a end
63
+ end
64
+ end
65
+ let(:options) { { a: [] } }
66
+
67
+ it 'does not raise an error' do
68
+ expect { described_class.run(options) }.to_not raise_error
69
+ end
70
+
71
+ it 'ignores the nested default value' do
72
+ expect(described_class.run!(options)).to eq options[:a]
67
73
  end
68
74
  end
69
75
  end
@@ -53,17 +53,23 @@ describe HashInteraction do
53
53
  end
54
54
  end
55
55
 
56
- context 'with an invalidly nested default' do
57
- it 'raises an error' do
58
- expect {
59
- klass = Class.new(ActiveInteraction::Base) do
60
- hash :a do
61
- hash :x, default: {}
62
- end
56
+ context 'with a validly nested default' do
57
+ let(:described_class) do
58
+ Class.new(ActiveInteraction::Base) do
59
+ hash :a do
60
+ hash :x, default: { y: rand }
63
61
  end
64
- # TODO: Fail when defining class, not running it.
65
- klass.run(a: {})
66
- }.to raise_error ArgumentError
62
+ def execute; a end
63
+ end
64
+ end
65
+ let(:options) { { a: { x: {} } } }
66
+
67
+ it 'does not raise an error' do
68
+ expect { described_class.run(options) }.to_not raise_error
69
+ end
70
+
71
+ it 'merges the nested default value' do
72
+ expect(described_class.run!(options)[:x]).to have_key(:y)
67
73
  end
68
74
  end
69
75
  end
@@ -57,6 +57,11 @@ shared_examples_for 'an interaction' do |type, generator, filter_options = {}|
57
57
  expect(result[:default]).to_not be_nil
58
58
  end
59
59
 
60
+ it 'does not return nil for :default when given nil' do
61
+ options.merge!(default: nil)
62
+ expect(result[:default]).to_not be_nil
63
+ end
64
+
60
65
  it 'returns nil for :nil_default' do
61
66
  expect(result[:nil_default]).to be_nil
62
67
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_interaction
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Lasseigne