active_interaction 1.1.7 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -1
  3. data/README.md +3 -2
  4. data/lib/active_interaction.rb +7 -1
  5. data/lib/active_interaction/base.rb +61 -13
  6. data/lib/active_interaction/concerns/runnable.rb +2 -18
  7. data/lib/active_interaction/concerns/transactable.rb +72 -0
  8. data/lib/active_interaction/errors.rb +7 -0
  9. data/lib/active_interaction/filter.rb +23 -2
  10. data/lib/active_interaction/filter_column.rb +59 -0
  11. data/lib/active_interaction/filters/abstract_date_time_filter.rb +14 -0
  12. data/lib/active_interaction/filters/abstract_filter.rb +4 -7
  13. data/lib/active_interaction/filters/abstract_numeric_filter.rb +4 -0
  14. data/lib/active_interaction/filters/boolean_filter.rb +4 -0
  15. data/lib/active_interaction/filters/date_time_filter.rb +3 -0
  16. data/lib/active_interaction/filters/decimal_filter.rb +54 -0
  17. data/lib/active_interaction/filters/file_filter.rb +4 -0
  18. data/lib/active_interaction/filters/time_filter.rb +4 -0
  19. data/lib/active_interaction/grouped_input.rb +24 -0
  20. data/lib/active_interaction/locale/en.yml +1 -0
  21. data/lib/active_interaction/modules/input_processor.rb +40 -0
  22. data/lib/active_interaction/version.rb +1 -1
  23. data/spec/active_interaction/base_spec.rb +90 -29
  24. data/spec/active_interaction/concerns/runnable_spec.rb +0 -26
  25. data/spec/active_interaction/concerns/transactable_spec.rb +114 -0
  26. data/spec/active_interaction/filter_column_spec.rb +96 -0
  27. data/spec/active_interaction/filter_spec.rb +15 -11
  28. data/spec/active_interaction/filters/array_filter_spec.rb +13 -5
  29. data/spec/active_interaction/filters/boolean_filter_spec.rb +6 -0
  30. data/spec/active_interaction/filters/date_filter_spec.rb +76 -5
  31. data/spec/active_interaction/filters/date_time_filter_spec.rb +87 -5
  32. data/spec/active_interaction/filters/decimal_filter_spec.rb +70 -0
  33. data/spec/active_interaction/filters/file_filter_spec.rb +11 -3
  34. data/spec/active_interaction/filters/float_filter_spec.rb +12 -4
  35. data/spec/active_interaction/filters/hash_filter_spec.rb +16 -8
  36. data/spec/active_interaction/filters/integer_filter_spec.rb +12 -4
  37. data/spec/active_interaction/filters/model_filter_spec.rb +12 -5
  38. data/spec/active_interaction/filters/string_filter_spec.rb +11 -3
  39. data/spec/active_interaction/filters/symbol_filter_spec.rb +10 -2
  40. data/spec/active_interaction/filters/time_filter_spec.rb +87 -5
  41. data/spec/active_interaction/grouped_input_spec.rb +19 -0
  42. data/spec/active_interaction/modules/input_processor_spec.rb +75 -0
  43. data/spec/support/filters.rb +6 -0
  44. metadata +16 -1
@@ -0,0 +1,96 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe ActiveInteraction::FilterColumn do
6
+ let(:type) { :float }
7
+ subject(:column) { described_class.intern(type) }
8
+
9
+ describe '.intern(type)' do
10
+ it 'returns the same object for each type' do
11
+ expect(described_class.intern(type)).to equal column
12
+ end
13
+
14
+ it 'returns different objects for different types' do
15
+ expect(described_class.intern(:integer)).to_not equal column
16
+ end
17
+ end
18
+
19
+ describe '.new(type)' do
20
+ it 'is private' do
21
+ expect { described_class.new(type) }.to raise_error NoMethodError
22
+ end
23
+ end
24
+
25
+ describe '#limit' do
26
+ it 'returns nil' do
27
+ expect(column.limit).to be_nil
28
+ end
29
+ end
30
+
31
+ describe '#type' do
32
+ it 'returns the type' do
33
+ expect(column.type).to eql type
34
+ end
35
+ end
36
+
37
+ describe '#number?' do
38
+ let(:number?) { column.number? }
39
+
40
+ context 'type is' do
41
+ context ':integer' do
42
+ let(:type) { :integer }
43
+
44
+ it 'returns true' do
45
+ expect(number?).to be_true
46
+ end
47
+ end
48
+
49
+ context ':float' do
50
+ let(:type) { :float }
51
+
52
+ it 'returns true' do
53
+ expect(number?).to be_true
54
+ end
55
+ end
56
+
57
+ context 'anything else' do
58
+ let(:type) { :string }
59
+
60
+ it 'returns false' do
61
+ expect(number?).to be_false
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ describe '#text?' do
68
+ let(:text?) { column.text? }
69
+
70
+ context 'type is' do
71
+ context ':string' do
72
+ let(:type) { :string }
73
+
74
+ it 'returns true' do
75
+ expect(text?).to be_true
76
+ end
77
+ end
78
+
79
+ context 'anything else' do
80
+ let(:type) { :float }
81
+
82
+ it 'returns false' do
83
+ expect(text?).to be_false
84
+ end
85
+ end
86
+ end
87
+ end
88
+
89
+ describe '#type' do
90
+ let(:type) { :float }
91
+
92
+ it 'returns the type' do
93
+ expect(column.type).to eql type
94
+ end
95
+ end
96
+ end
@@ -3,29 +3,33 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  module ActiveInteraction
6
- class TestFilter < ActiveInteraction::Filter; end
6
+ class ATestFilter < ActiveInteraction::Filter; end
7
7
  end
8
- class TestFilter < ActiveInteraction::Filter; end
8
+ class ATestFilter < ActiveInteraction::Filter; end
9
9
 
10
10
  describe ActiveInteraction::Filter, :filter do
11
11
  include_context 'filters'
12
12
 
13
- describe '.slug' do
14
- it 'raises an error' do
15
- expect do
16
- described_class.slug
17
- end.to raise_error ActiveInteraction::InvalidClassError
13
+ describe '#database_column_type' do
14
+ it 'returns `:string`' do
15
+ expect(subject.database_column_type).to eql :string
18
16
  end
19
17
  end
20
18
 
21
- context ActiveInteraction::TestFilter do
19
+ context ActiveInteraction::ATestFilter do
22
20
  it_behaves_like 'a filter'
23
21
 
24
- let(:described_class) { ActiveInteraction::TestFilter }
22
+ let(:described_class) { ActiveInteraction::ATestFilter }
23
+
24
+ describe '.slug' do
25
+ it 'returns a slug representing the class' do
26
+ expect(described_class.slug).to eql :a_test
27
+ end
28
+ end
25
29
  end
26
30
 
27
- context TestFilter do
28
- let(:described_class) { TestFilter }
31
+ context ATestFilter do
32
+ let(:described_class) { ATestFilter }
29
33
 
30
34
  describe '.factory' do
31
35
  it 'returns a Filter' do
@@ -36,11 +36,13 @@ describe ActiveInteraction::ArrayFilter, :filter do
36
36
  end
37
37
 
38
38
  describe '#cast' do
39
+ let(:result) { filter.cast(value) }
40
+
39
41
  context 'with an Array' do
40
42
  let(:value) { [] }
41
43
 
42
44
  it 'returns the Array' do
43
- expect(filter.cast(value)).to eql value
45
+ expect(result).to eql value
44
46
  end
45
47
  end
46
48
 
@@ -48,7 +50,7 @@ describe ActiveInteraction::ArrayFilter, :filter do
48
50
  let(:value) { [[], false, 0.0, {}, 0, '', :''] }
49
51
 
50
52
  it 'returns the Array' do
51
- expect(filter.cast(value)).to eql value
53
+ expect(result).to eql value
52
54
  end
53
55
  end
54
56
 
@@ -59,7 +61,7 @@ describe ActiveInteraction::ArrayFilter, :filter do
59
61
  let(:value) { [] }
60
62
 
61
63
  it 'returns the Array' do
62
- expect(filter.cast(value)).to eql value
64
+ expect(result).to eql value
63
65
  end
64
66
  end
65
67
 
@@ -67,7 +69,7 @@ describe ActiveInteraction::ArrayFilter, :filter do
67
69
  let(:value) { [[]] }
68
70
 
69
71
  it 'returns the Array' do
70
- expect(filter.cast(value)).to eql value
72
+ expect(result).to eql value
71
73
  end
72
74
  end
73
75
 
@@ -76,10 +78,16 @@ describe ActiveInteraction::ArrayFilter, :filter do
76
78
 
77
79
  it 'raises an error' do
78
80
  expect do
79
- filter.cast(value)
81
+ result
80
82
  end.to raise_error ActiveInteraction::InvalidValueError
81
83
  end
82
84
  end
83
85
  end
84
86
  end
87
+
88
+ describe '#database_column_type' do
89
+ it 'returns :string' do
90
+ expect(filter.database_column_type).to eql :string
91
+ end
92
+ end
85
93
  end
@@ -23,4 +23,10 @@ describe ActiveInteraction::BooleanFilter, :filter do
23
23
  end
24
24
  end
25
25
  end
26
+
27
+ describe '#database_column_type' do
28
+ it 'returns :boolean' do
29
+ expect(filter.database_column_type).to eql :boolean
30
+ end
31
+ end
26
32
  end
@@ -15,11 +15,13 @@ describe ActiveInteraction::DateFilter, :filter do
15
15
  end
16
16
 
17
17
  describe '#cast' do
18
+ let(:result) { filter.cast(value) }
19
+
18
20
  context 'with a Date' do
19
21
  let(:value) { Date.new }
20
22
 
21
23
  it 'returns the Date' do
22
- expect(filter.cast(value)).to eql value
24
+ expect(result).to eql value
23
25
  end
24
26
  end
25
27
 
@@ -27,7 +29,7 @@ describe ActiveInteraction::DateFilter, :filter do
27
29
  let(:value) { '2011-12-13' }
28
30
 
29
31
  it 'returns a Date' do
30
- expect(filter.cast(value)).to eql Date.parse(value)
32
+ expect(result).to eql Date.parse(value)
31
33
  end
32
34
 
33
35
  context 'with format' do
@@ -36,7 +38,7 @@ describe ActiveInteraction::DateFilter, :filter do
36
38
  let(:value) { '13/12/2011' }
37
39
 
38
40
  it 'returns a Date' do
39
- expect(filter.cast(value)).to eql Date.strptime(value, format)
41
+ expect(result).to eql Date.strptime(value, format)
40
42
  end
41
43
  end
42
44
  end
@@ -46,7 +48,7 @@ describe ActiveInteraction::DateFilter, :filter do
46
48
 
47
49
  it 'raises an error' do
48
50
  expect do
49
- filter.cast(value)
51
+ result
50
52
  end.to raise_error ActiveInteraction::InvalidValueError
51
53
  end
52
54
 
@@ -55,10 +57,79 @@ describe ActiveInteraction::DateFilter, :filter do
55
57
 
56
58
  it 'raises an error' do
57
59
  expect do
58
- filter.cast(value)
60
+ result
61
+ end.to raise_error ActiveInteraction::InvalidValueError
62
+ end
63
+ end
64
+ end
65
+
66
+ context 'with a GroupedInput' do
67
+ let(:year) { 2012 }
68
+ let(:month) { 1 }
69
+ let(:day) { 2 }
70
+ let(:value) do
71
+ ActiveInteraction::GroupedInput.new(
72
+ '1' => year.to_s,
73
+ '2' => month.to_s,
74
+ '3' => day.to_s
75
+ )
76
+ end
77
+
78
+ it 'returns a Date' do
79
+ expect(result).to eql Date.new(year, month, day)
80
+ end
81
+ end
82
+
83
+ context 'with an invalid GroupedInput' do
84
+ context 'empty' do
85
+ let(:value) { ActiveInteraction::GroupedInput.new }
86
+
87
+ it 'raises an error' do
88
+ expect do
89
+ result
90
+ end.to raise_error ActiveInteraction::InvalidValueError
91
+ end
92
+ end
93
+
94
+ context 'partial inputs' do
95
+ let(:value) do
96
+ ActiveInteraction::GroupedInput.new(
97
+ '2' => '1'
98
+ )
99
+ end
100
+
101
+ it 'raises an error' do
102
+ expect do
103
+ result
59
104
  end.to raise_error ActiveInteraction::InvalidValueError
60
105
  end
61
106
  end
62
107
  end
63
108
  end
109
+
110
+ describe '#database_column_type' do
111
+ it 'returns :date' do
112
+ expect(filter.database_column_type).to eql :date
113
+ end
114
+ end
115
+
116
+ describe '#default' do
117
+ context 'with a GroupedInput' do
118
+ before do
119
+ options.merge!(
120
+ default: ActiveInteraction::GroupedInput.new(
121
+ '1' => '2012',
122
+ '2' => '1',
123
+ '3' => '2'
124
+ )
125
+ )
126
+ end
127
+
128
+ it 'raises an error' do
129
+ expect do
130
+ filter.default
131
+ end.to raise_error ActiveInteraction::InvalidDefaultError
132
+ end
133
+ end
134
+ end
64
135
  end
@@ -15,11 +15,13 @@ describe ActiveInteraction::DateTimeFilter, :filter do
15
15
  end
16
16
 
17
17
  describe '#cast' do
18
+ let(:result) { filter.cast(value) }
19
+
18
20
  context 'with a Datetime' do
19
21
  let(:value) { DateTime.new }
20
22
 
21
23
  it 'returns the DateTime' do
22
- expect(filter.cast(value)).to eql value
24
+ expect(result).to eql value
23
25
  end
24
26
  end
25
27
 
@@ -27,7 +29,7 @@ describe ActiveInteraction::DateTimeFilter, :filter do
27
29
  let(:value) { '2011-12-13T14:15:16+17:18' }
28
30
 
29
31
  it 'returns a DateTime' do
30
- expect(filter.cast(value)).to eql DateTime.parse(value)
32
+ expect(result).to eql DateTime.parse(value)
31
33
  end
32
34
 
33
35
  context 'with format' do
@@ -36,7 +38,7 @@ describe ActiveInteraction::DateTimeFilter, :filter do
36
38
  let(:value) { '13/12/2011 14:15:16 +17:18' }
37
39
 
38
40
  it 'returns a DateTime' do
39
- expect(filter.cast(value)).to eql DateTime.strptime(value, format)
41
+ expect(result).to eql DateTime.strptime(value, format)
40
42
  end
41
43
  end
42
44
  end
@@ -46,7 +48,7 @@ describe ActiveInteraction::DateTimeFilter, :filter do
46
48
 
47
49
  it 'raises an error' do
48
50
  expect do
49
- filter.cast(value)
51
+ result
50
52
  end.to raise_error ActiveInteraction::InvalidValueError
51
53
  end
52
54
 
@@ -55,10 +57,90 @@ describe ActiveInteraction::DateTimeFilter, :filter do
55
57
 
56
58
  it 'raises an error' do
57
59
  expect do
58
- filter.cast(value)
60
+ result
61
+ end.to raise_error ActiveInteraction::InvalidValueError
62
+ end
63
+ end
64
+ end
65
+
66
+ context 'with a GroupedInput' do
67
+ let(:year) { 2012 }
68
+ let(:month) { 1 }
69
+ let(:day) { 2 }
70
+ let(:hour) { 3 }
71
+ let(:min) { 4 }
72
+ let(:sec) { 5 }
73
+ let(:value) do
74
+ ActiveInteraction::GroupedInput.new(
75
+ '1' => year.to_s,
76
+ '2' => month.to_s,
77
+ '3' => day.to_s,
78
+ '4' => hour.to_s,
79
+ '5' => min.to_s,
80
+ '6' => sec.to_s
81
+ )
82
+ end
83
+
84
+ it 'returns a DateTime' do
85
+ expect(
86
+ result
87
+ ).to eql DateTime.new(year, month, day, hour, min, sec)
88
+ end
89
+ end
90
+
91
+ context 'with an invalid GroupedInput' do
92
+ context 'empty' do
93
+ let(:value) { ActiveInteraction::GroupedInput.new }
94
+
95
+ it 'raises an error' do
96
+ expect do
97
+ result
98
+ end.to raise_error ActiveInteraction::InvalidValueError
99
+ end
100
+ end
101
+
102
+ context 'partial inputs' do
103
+ let(:value) do
104
+ ActiveInteraction::GroupedInput.new(
105
+ '2' => '1'
106
+ )
107
+ end
108
+
109
+ it 'raises an error' do
110
+ expect do
111
+ result
59
112
  end.to raise_error ActiveInteraction::InvalidValueError
60
113
  end
61
114
  end
62
115
  end
63
116
  end
117
+
118
+ describe '#database_column_type' do
119
+ it 'returns :datetime' do
120
+ expect(filter.database_column_type).to eql :datetime
121
+ end
122
+ end
123
+
124
+ describe '#default' do
125
+ context 'with a GroupedInput' do
126
+ before do
127
+ options.merge!(
128
+ default: ActiveInteraction::GroupedInput.new(
129
+ '1' => '2012',
130
+ '2' => '1',
131
+ '3' => '2',
132
+ '4' => '3',
133
+ '5' => '4',
134
+ '6' => '5'
135
+ )
136
+ )
137
+ end
138
+
139
+ it 'raises an error' do
140
+ expect do
141
+ filter.default
142
+ end.to raise_error ActiveInteraction::InvalidDefaultError
143
+ end
144
+ end
145
+ end
64
146
  end