active_interaction 3.7.1 → 4.0.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.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +190 -0
  3. data/CONTRIBUTING.md +1 -1
  4. data/README.md +96 -90
  5. data/lib/active_interaction.rb +1 -7
  6. data/lib/active_interaction/base.rb +48 -33
  7. data/lib/active_interaction/concerns/active_modelable.rb +1 -3
  8. data/lib/active_interaction/concerns/active_recordable.rb +1 -6
  9. data/lib/active_interaction/concerns/hashable.rb +0 -1
  10. data/lib/active_interaction/concerns/missable.rb +0 -1
  11. data/lib/active_interaction/concerns/runnable.rb +16 -28
  12. data/lib/active_interaction/errors.rb +8 -7
  13. data/lib/active_interaction/filter.rb +51 -37
  14. data/lib/active_interaction/filter_column.rb +0 -3
  15. data/lib/active_interaction/filters/abstract_date_time_filter.rb +34 -36
  16. data/lib/active_interaction/filters/abstract_numeric_filter.rb +27 -17
  17. data/lib/active_interaction/filters/array_filter.rb +57 -36
  18. data/lib/active_interaction/filters/boolean_filter.rb +26 -12
  19. data/lib/active_interaction/filters/date_filter.rb +1 -2
  20. data/lib/active_interaction/filters/date_time_filter.rb +1 -2
  21. data/lib/active_interaction/filters/decimal_filter.rb +10 -28
  22. data/lib/active_interaction/filters/file_filter.rb +6 -5
  23. data/lib/active_interaction/filters/float_filter.rb +1 -2
  24. data/lib/active_interaction/filters/hash_filter.rb +37 -27
  25. data/lib/active_interaction/filters/integer_filter.rb +7 -8
  26. data/lib/active_interaction/filters/interface_filter.rb +48 -14
  27. data/lib/active_interaction/filters/object_filter.rb +23 -50
  28. data/lib/active_interaction/filters/record_filter.rb +10 -35
  29. data/lib/active_interaction/filters/string_filter.rb +21 -12
  30. data/lib/active_interaction/filters/symbol_filter.rb +13 -7
  31. data/lib/active_interaction/filters/time_filter.rb +19 -22
  32. data/lib/active_interaction/grouped_input.rb +0 -3
  33. data/lib/active_interaction/inputs.rb +89 -0
  34. data/lib/active_interaction/locale/ja.yml +24 -0
  35. data/lib/active_interaction/modules/input_processor.rb +9 -6
  36. data/lib/active_interaction/modules/validation.rb +9 -12
  37. data/lib/active_interaction/version.rb +1 -3
  38. data/spec/active_interaction/base_spec.rb +95 -35
  39. data/spec/active_interaction/concerns/active_modelable_spec.rb +0 -2
  40. data/spec/active_interaction/concerns/active_recordable_spec.rb +0 -2
  41. data/spec/active_interaction/concerns/hashable_spec.rb +1 -3
  42. data/spec/active_interaction/concerns/missable_spec.rb +0 -2
  43. data/spec/active_interaction/concerns/runnable_spec.rb +32 -12
  44. data/spec/active_interaction/errors_spec.rb +49 -22
  45. data/spec/active_interaction/filter_column_spec.rb +0 -2
  46. data/spec/active_interaction/filter_spec.rb +0 -2
  47. data/spec/active_interaction/filters/abstract_date_time_filter_spec.rb +1 -3
  48. data/spec/active_interaction/filters/abstract_numeric_filter_spec.rb +1 -3
  49. data/spec/active_interaction/filters/array_filter_spec.rb +41 -15
  50. data/spec/active_interaction/filters/boolean_filter_spec.rb +58 -4
  51. data/spec/active_interaction/filters/date_filter_spec.rb +43 -3
  52. data/spec/active_interaction/filters/date_time_filter_spec.rb +43 -3
  53. data/spec/active_interaction/filters/decimal_filter_spec.rb +57 -3
  54. data/spec/active_interaction/filters/file_filter_spec.rb +1 -3
  55. data/spec/active_interaction/filters/float_filter_spec.rb +60 -4
  56. data/spec/active_interaction/filters/hash_filter_spec.rb +19 -9
  57. data/spec/active_interaction/filters/integer_filter_spec.rb +49 -7
  58. data/spec/active_interaction/filters/interface_filter_spec.rb +397 -24
  59. data/spec/active_interaction/filters/object_filter_spec.rb +23 -59
  60. data/spec/active_interaction/filters/record_filter_spec.rb +23 -49
  61. data/spec/active_interaction/filters/string_filter_spec.rb +15 -3
  62. data/spec/active_interaction/filters/symbol_filter_spec.rb +15 -3
  63. data/spec/active_interaction/filters/time_filter_spec.rb +65 -3
  64. data/spec/active_interaction/grouped_input_spec.rb +0 -2
  65. data/spec/active_interaction/i18n_spec.rb +3 -7
  66. data/spec/active_interaction/{modules/input_processor_spec.rb → inputs_spec.rb} +5 -5
  67. data/spec/active_interaction/integration/array_interaction_spec.rb +23 -12
  68. data/spec/active_interaction/integration/boolean_interaction_spec.rb +0 -2
  69. data/spec/active_interaction/integration/date_interaction_spec.rb +0 -2
  70. data/spec/active_interaction/integration/date_time_interaction_spec.rb +0 -2
  71. data/spec/active_interaction/integration/file_interaction_spec.rb +0 -2
  72. data/spec/active_interaction/integration/float_interaction_spec.rb +0 -2
  73. data/spec/active_interaction/integration/hash_interaction_spec.rb +0 -2
  74. data/spec/active_interaction/integration/integer_interaction_spec.rb +0 -2
  75. data/spec/active_interaction/integration/interface_interaction_spec.rb +1 -3
  76. data/spec/active_interaction/integration/object_interaction_spec.rb +0 -2
  77. data/spec/active_interaction/integration/string_interaction_spec.rb +0 -2
  78. data/spec/active_interaction/integration/symbol_interaction_spec.rb +0 -2
  79. data/spec/active_interaction/integration/time_interaction_spec.rb +14 -18
  80. data/spec/active_interaction/modules/validation_spec.rb +1 -3
  81. data/spec/spec_helper.rb +2 -6
  82. data/spec/support/concerns.rb +0 -2
  83. data/spec/support/filters.rb +13 -9
  84. data/spec/support/interactions.rb +22 -14
  85. metadata +106 -52
  86. data/lib/active_interaction/backports.rb +0 -59
  87. data/lib/active_interaction/filters/abstract_filter.rb +0 -19
  88. data/spec/active_interaction/filters/abstract_filter_spec.rb +0 -8
@@ -1,5 +1,3 @@
1
- # coding: utf-8
2
-
3
1
  require 'spec_helper'
4
2
 
5
3
  describe ActiveInteraction::DateFilter, :filter do
@@ -15,7 +13,7 @@ describe ActiveInteraction::DateFilter, :filter do
15
13
  end
16
14
 
17
15
  describe '#cast' do
18
- let(:result) { filter.cast(value, nil) }
16
+ let(:result) { filter.send(:cast, value, nil) }
19
17
 
20
18
  context 'with a Date' do
21
19
  let(:value) { Date.new }
@@ -63,6 +61,48 @@ describe ActiveInteraction::DateFilter, :filter do
63
61
  end
64
62
  end
65
63
 
64
+ context 'with an implicit String' do
65
+ let(:value) do
66
+ Class.new do
67
+ def to_str
68
+ '2011-12-13'
69
+ end
70
+ end.new
71
+ end
72
+
73
+ it 'returns a Date' do
74
+ expect(result).to eql Date.parse(value)
75
+ end
76
+ end
77
+
78
+ context 'with a blank String' do
79
+ let(:value) do
80
+ Class.new do
81
+ def to_str
82
+ ' '
83
+ end
84
+ end.new
85
+ end
86
+
87
+ context 'optional' do
88
+ include_context 'optional'
89
+
90
+ it 'returns the default' do
91
+ expect(result).to eql options[:default]
92
+ end
93
+ end
94
+
95
+ context 'required' do
96
+ include_context 'required'
97
+
98
+ it 'raises an error' do
99
+ expect do
100
+ result
101
+ end.to raise_error ActiveInteraction::MissingValueError
102
+ end
103
+ end
104
+ end
105
+
66
106
  context 'with a GroupedInput' do
67
107
  let(:year) { 2012 }
68
108
  let(:month) { 1 }
@@ -1,5 +1,3 @@
1
- # coding: utf-8
2
-
3
1
  require 'spec_helper'
4
2
 
5
3
  describe ActiveInteraction::DateTimeFilter, :filter do
@@ -15,7 +13,7 @@ describe ActiveInteraction::DateTimeFilter, :filter do
15
13
  end
16
14
 
17
15
  describe '#cast' do
18
- let(:result) { filter.cast(value, nil) }
16
+ let(:result) { filter.send(:cast, value, nil) }
19
17
 
20
18
  context 'with a Datetime' do
21
19
  let(:value) { DateTime.new }
@@ -63,6 +61,48 @@ describe ActiveInteraction::DateTimeFilter, :filter do
63
61
  end
64
62
  end
65
63
 
64
+ context 'with an implicit String' do
65
+ let(:value) do
66
+ Class.new do
67
+ def to_str
68
+ '2011-12-13T14:15:16+17:18'
69
+ end
70
+ end.new
71
+ end
72
+
73
+ it 'returns a DateTime' do
74
+ expect(result).to eql DateTime.parse(value)
75
+ end
76
+ end
77
+
78
+ context 'with a blank String' do
79
+ let(:value) do
80
+ Class.new do
81
+ def to_str
82
+ ' '
83
+ end
84
+ end.new
85
+ end
86
+
87
+ context 'optional' do
88
+ include_context 'optional'
89
+
90
+ it 'returns the default' do
91
+ expect(result).to eql options[:default]
92
+ end
93
+ end
94
+
95
+ context 'required' do
96
+ include_context 'required'
97
+
98
+ it 'raises an error' do
99
+ expect do
100
+ result
101
+ end.to raise_error ActiveInteraction::MissingValueError
102
+ end
103
+ end
104
+ end
105
+
66
106
  context 'with a GroupedInput' do
67
107
  let(:year) { 2012 }
68
108
  let(:month) { 1 }
@@ -1,5 +1,3 @@
1
- # coding: utf-8
2
-
3
1
  require 'spec_helper'
4
2
 
5
3
  describe ActiveInteraction::DecimalFilter, :filter do
@@ -15,7 +13,7 @@ describe ActiveInteraction::DecimalFilter, :filter do
15
13
  end
16
14
 
17
15
  describe '#cast' do
18
- let(:result) { filter.cast(value, nil) }
16
+ let(:result) { filter.send(:cast, value, nil) }
19
17
 
20
18
  context 'with a Float' do
21
19
  let(:value) { rand }
@@ -35,6 +33,20 @@ describe ActiveInteraction::DecimalFilter, :filter do
35
33
  end
36
34
  end
37
35
 
36
+ context 'with an implicit Integer' do
37
+ let(:value) do
38
+ Class.new do
39
+ def to_int
40
+ @to_int ||= rand(1 << 16)
41
+ end
42
+ end.new
43
+ end
44
+
45
+ it 'returns a BigDecimal' do
46
+ expect(result).to eql BigDecimal(value.to_int)
47
+ end
48
+ end
49
+
38
50
  context 'with a Numeric' do
39
51
  let(:value) { rand(1 << 16) }
40
52
 
@@ -60,6 +72,48 @@ describe ActiveInteraction::DecimalFilter, :filter do
60
72
  end.to raise_error ActiveInteraction::InvalidValueError
61
73
  end
62
74
  end
75
+
76
+ context 'with an implicit String' do
77
+ let(:value) do
78
+ Class.new do
79
+ def to_str
80
+ '1.1'
81
+ end
82
+ end.new
83
+ end
84
+
85
+ it 'returns a BigDecimal' do
86
+ expect(result).to eql BigDecimal(value)
87
+ end
88
+ end
89
+
90
+ context 'with a blank String' do
91
+ let(:value) do
92
+ Class.new do
93
+ def to_str
94
+ ' '
95
+ end
96
+ end.new
97
+ end
98
+
99
+ context 'optional' do
100
+ include_context 'optional'
101
+
102
+ it 'returns the default' do
103
+ expect(result).to eql options[:default]
104
+ end
105
+ end
106
+
107
+ context 'required' do
108
+ include_context 'required'
109
+
110
+ it 'raises an error' do
111
+ expect do
112
+ result
113
+ end.to raise_error ActiveInteraction::MissingValueError
114
+ end
115
+ end
116
+ end
63
117
  end
64
118
 
65
119
  describe '#database_column_type' do
@@ -1,5 +1,3 @@
1
- # coding: utf-8
2
-
3
1
  require 'spec_helper'
4
2
 
5
3
  describe ActiveInteraction::FileFilter, :filter do
@@ -7,7 +5,7 @@ describe ActiveInteraction::FileFilter, :filter do
7
5
  it_behaves_like 'a filter'
8
6
 
9
7
  describe '#cast' do
10
- let(:result) { filter.cast(value, nil) }
8
+ let(:result) { filter.send(:cast, value, nil) }
11
9
 
12
10
  context 'with a File' do
13
11
  let(:value) { File.new(__FILE__) }
@@ -1,5 +1,4 @@
1
- # coding: utf-8
2
-
1
+ require 'bigdecimal'
3
2
  require 'spec_helper'
4
3
 
5
4
  describe ActiveInteraction::FloatFilter, :filter do
@@ -7,7 +6,7 @@ describe ActiveInteraction::FloatFilter, :filter do
7
6
  it_behaves_like 'a filter'
8
7
 
9
8
  describe '#cast' do
10
- let(:result) { filter.cast(value, nil) }
9
+ let(:result) { filter.send(:cast, value, nil) }
11
10
 
12
11
  context 'with a Float' do
13
12
  let(:value) { rand }
@@ -17,8 +16,22 @@ describe ActiveInteraction::FloatFilter, :filter do
17
16
  end
18
17
  end
19
18
 
19
+ context 'with an implicit Integer' do
20
+ let(:value) do
21
+ Class.new do
22
+ def to_int
23
+ @to_int ||= rand(1 << 16)
24
+ end
25
+ end.new
26
+ end
27
+
28
+ it 'returns a Float' do
29
+ expect(result).to eql value.to_int.to_f
30
+ end
31
+ end
32
+
20
33
  context 'with a Numeric' do
21
- let(:value) { rand(1 << 16) }
34
+ let(:value) { BigDecimal('1.2') }
22
35
 
23
36
  it 'returns a Float' do
24
37
  expect(result).to eql value.to_f
@@ -42,6 +55,49 @@ describe ActiveInteraction::FloatFilter, :filter do
42
55
  end.to raise_error ActiveInteraction::InvalidValueError
43
56
  end
44
57
  end
58
+
59
+ context 'with an implicit String' do
60
+ let(:value) do
61
+ Class.new do
62
+ def to_str
63
+ '1.1'
64
+ end
65
+ end.new
66
+ end
67
+
68
+ it 'returns a Float' do
69
+ # apparently `Float()` doesn't do this even though `Integer()` does
70
+ expect(result).to eql Float(value.to_str)
71
+ end
72
+ end
73
+
74
+ context 'with a blank String' do
75
+ let(:value) do
76
+ Class.new do
77
+ def to_str
78
+ ' '
79
+ end
80
+ end.new
81
+ end
82
+
83
+ context 'optional' do
84
+ include_context 'optional'
85
+
86
+ it 'returns the default' do
87
+ expect(result).to eql options[:default]
88
+ end
89
+ end
90
+
91
+ context 'required' do
92
+ include_context 'required'
93
+
94
+ it 'raises an error' do
95
+ expect do
96
+ result
97
+ end.to raise_error ActiveInteraction::MissingValueError
98
+ end
99
+ end
100
+ end
45
101
  end
46
102
 
47
103
  describe '#database_column_type' do
@@ -1,5 +1,3 @@
1
- # coding: utf-8
2
-
3
1
  require 'spec_helper'
4
2
 
5
3
  describe ActiveInteraction::HashFilter, :filter do
@@ -15,7 +13,7 @@ describe ActiveInteraction::HashFilter, :filter do
15
13
  end
16
14
 
17
15
  describe '#cast' do
18
- let(:result) { filter.cast(value, nil) }
16
+ let(:result) { filter.send(:cast, value, nil) }
19
17
 
20
18
  context 'with a Hash' do
21
19
  let(:value) { {} }
@@ -25,6 +23,20 @@ describe ActiveInteraction::HashFilter, :filter do
25
23
  end
26
24
  end
27
25
 
26
+ context 'with an implicit Hash' do
27
+ let(:value) do
28
+ Class.new do
29
+ def to_hash
30
+ {}
31
+ end
32
+ end.new
33
+ end
34
+
35
+ it 'returns the Hash' do
36
+ expect(result).to eql value.to_hash
37
+ end
38
+ end
39
+
28
40
  context 'with a non-empty Hash' do
29
41
  let(:value) { { a: {} } }
30
42
 
@@ -66,12 +78,10 @@ describe ActiveInteraction::HashFilter, :filter do
66
78
  end
67
79
 
68
80
  it 'populates the error' do
69
- begin
70
- result
71
- rescue ActiveInteraction::InvalidNestedValueError => e
72
- expect(e.filter_name).to eql k
73
- expect(e.input_value).to eql v
74
- end
81
+ result
82
+ rescue ActiveInteraction::InvalidNestedValueError => e
83
+ expect(e.filter_name).to eql k
84
+ expect(e.input_value).to eql v
75
85
  end
76
86
  end
77
87
  end
@@ -1,5 +1,3 @@
1
- # coding: utf-8
2
-
3
1
  require 'spec_helper'
4
2
 
5
3
  describe ActiveInteraction::IntegerFilter, :filter do
@@ -7,7 +5,7 @@ describe ActiveInteraction::IntegerFilter, :filter do
7
5
  it_behaves_like 'a filter'
8
6
 
9
7
  describe '#cast' do
10
- let(:result) { filter.cast(value, nil) }
8
+ let(:result) { filter.send(:cast, value, nil) }
11
9
 
12
10
  context 'with an Integer' do
13
11
  let(:value) { rand(1 << 16) }
@@ -29,7 +27,7 @@ describe ActiveInteraction::IntegerFilter, :filter do
29
27
  let(:value) { rand(1 << 16).to_s }
30
28
 
31
29
  it 'returns an Integer' do
32
- expect(result).to eql Integer(value)
30
+ expect(result).to eql Integer(value, 10)
33
31
  end
34
32
  end
35
33
 
@@ -43,12 +41,56 @@ describe ActiveInteraction::IntegerFilter, :filter do
43
41
  end
44
42
  end
45
43
 
44
+ context 'with an implicit String' do
45
+ let(:value) do
46
+ Class.new do
47
+ def to_str
48
+ '1'
49
+ end
50
+ end.new
51
+ end
52
+
53
+ it 'returns an Integer' do
54
+ # jRuby freezes on the implicit string value
55
+ expect(result).to eql Integer(value.to_str, 10)
56
+ end
57
+ end
58
+
59
+ context 'with a blank String' do
60
+ let(:value) do
61
+ Class.new do
62
+ def to_str
63
+ ' '
64
+ end
65
+ end.new
66
+ end
67
+
68
+ context 'optional' do
69
+ include_context 'optional'
70
+
71
+ it 'returns the default' do
72
+ expect(result).to eql options[:default]
73
+ end
74
+ end
75
+
76
+ context 'required' do
77
+ include_context 'required'
78
+
79
+ it 'raises an error' do
80
+ expect do
81
+ result
82
+ end.to raise_error ActiveInteraction::MissingValueError
83
+ end
84
+ end
85
+ end
86
+
46
87
  it 'supports different bases' do
47
- expect(filter.cast('07', nil)).to eql 7
88
+ expect(
89
+ described_class.new(name, base: 8).send(:cast, '071', nil)
90
+ ).to eql 57
48
91
  expect do
49
- filter.cast('08', nil)
92
+ described_class.new(name, base: 8).send(:cast, '081', nil)
50
93
  end.to raise_error ActiveInteraction::InvalidValueError
51
- expect(described_class.new(name, base: 10).cast('08', nil)).to eql 8
52
94
  end
53
95
  end
54
96