active_interaction 0.5.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +27 -3
- data/README.md +8 -6
- data/lib/active_interaction.rb +5 -3
- data/lib/active_interaction/active_model.rb +29 -0
- data/lib/active_interaction/base.rb +82 -116
- data/lib/active_interaction/errors.rb +79 -5
- data/lib/active_interaction/filter.rb +195 -21
- data/lib/active_interaction/filters.rb +26 -0
- data/lib/active_interaction/filters/array_filter.rb +22 -25
- data/lib/active_interaction/filters/boolean_filter.rb +12 -12
- data/lib/active_interaction/filters/date_filter.rb +32 -5
- data/lib/active_interaction/filters/date_time_filter.rb +34 -7
- data/lib/active_interaction/filters/file_filter.rb +12 -9
- data/lib/active_interaction/filters/float_filter.rb +13 -11
- data/lib/active_interaction/filters/hash_filter.rb +36 -17
- data/lib/active_interaction/filters/integer_filter.rb +13 -11
- data/lib/active_interaction/filters/model_filter.rb +15 -15
- data/lib/active_interaction/filters/string_filter.rb +19 -8
- data/lib/active_interaction/filters/symbol_filter.rb +29 -0
- data/lib/active_interaction/filters/time_filter.rb +38 -16
- data/lib/active_interaction/method_missing.rb +18 -0
- data/lib/active_interaction/overload_hash.rb +1 -0
- data/lib/active_interaction/validation.rb +19 -0
- data/lib/active_interaction/version.rb +1 -1
- data/spec/active_interaction/active_model_spec.rb +33 -0
- data/spec/active_interaction/base_spec.rb +54 -48
- data/spec/active_interaction/errors_spec.rb +99 -0
- data/spec/active_interaction/filter_spec.rb +12 -20
- data/spec/active_interaction/filters/array_filter_spec.rb +50 -28
- data/spec/active_interaction/filters/boolean_filter_spec.rb +15 -15
- data/spec/active_interaction/filters/date_filter_spec.rb +30 -18
- data/spec/active_interaction/filters/date_time_filter_spec.rb +31 -19
- data/spec/active_interaction/filters/file_filter_spec.rb +7 -7
- data/spec/active_interaction/filters/float_filter_spec.rb +13 -11
- data/spec/active_interaction/filters/hash_filter_spec.rb +38 -29
- data/spec/active_interaction/filters/integer_filter_spec.rb +18 -8
- data/spec/active_interaction/filters/model_filter_spec.rb +24 -20
- data/spec/active_interaction/filters/string_filter_spec.rb +14 -8
- data/spec/active_interaction/filters/symbol_filter_spec.rb +24 -0
- data/spec/active_interaction/filters/time_filter_spec.rb +33 -69
- data/spec/active_interaction/filters_spec.rb +21 -0
- data/spec/active_interaction/i18n_spec.rb +0 -15
- data/spec/active_interaction/integration/array_interaction_spec.rb +2 -22
- data/spec/active_interaction/integration/hash_interaction_spec.rb +5 -25
- data/spec/active_interaction/integration/symbol_interaction_spec.rb +5 -0
- data/spec/active_interaction/method_missing_spec.rb +69 -0
- data/spec/active_interaction/validation_spec.rb +55 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/support/filters.rb +168 -14
- data/spec/support/interactions.rb +11 -13
- metadata +31 -13
- data/lib/active_interaction/filter_method.rb +0 -13
- data/lib/active_interaction/filter_methods.rb +0 -26
- data/lib/active_interaction/filters/abstract_date_time_filter.rb +0 -25
- data/spec/active_interaction/filter_method_spec.rb +0 -43
- data/spec/active_interaction/filter_methods_spec.rb +0 -30
@@ -1,29 +1,21 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
TestFilter = Class.new(Filter)
|
5
|
-
end
|
6
|
-
|
7
|
-
describe ActiveInteraction::Filter do
|
8
|
-
it_behaves_like 'a filter'
|
9
|
-
|
10
|
-
describe '.factory(type)' do
|
11
|
-
let(:result) { described_class.factory(type) }
|
3
|
+
class ActiveInteraction::TestFilter < ActiveInteraction::Filter; end
|
12
4
|
|
13
|
-
|
14
|
-
|
5
|
+
describe ActiveInteraction::Filter, :filter do
|
6
|
+
include_context 'filters'
|
15
7
|
|
16
|
-
|
17
|
-
|
18
|
-
|
8
|
+
describe '.slug' do
|
9
|
+
it 'raises an error' do
|
10
|
+
expect {
|
11
|
+
described_class.slug
|
12
|
+
}.to raise_error ActiveInteraction::InvalidClassError
|
19
13
|
end
|
14
|
+
end
|
20
15
|
|
21
|
-
|
22
|
-
|
16
|
+
context ActiveInteraction::TestFilter do
|
17
|
+
it_behaves_like 'a filter'
|
23
18
|
|
24
|
-
|
25
|
-
expect { result }.to raise_error NoMethodError
|
26
|
-
end
|
27
|
-
end
|
19
|
+
let(:described_class) { ActiveInteraction::TestFilter }
|
28
20
|
end
|
29
21
|
end
|
@@ -1,55 +1,77 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ActiveInteraction::ArrayFilter do
|
3
|
+
describe ActiveInteraction::ArrayFilter, :filter do
|
4
4
|
include_context 'filters'
|
5
5
|
it_behaves_like 'a filter'
|
6
6
|
|
7
|
-
|
7
|
+
context 'with multiple nested filters' do
|
8
|
+
let(:block) { Proc.new { array; array } }
|
9
|
+
|
10
|
+
it 'raises an error' do
|
11
|
+
expect { filter }.to raise_error ActiveInteraction::InvalidFilterError
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'with a nested name' do
|
16
|
+
let(:block) { Proc.new { array :a } }
|
17
|
+
|
18
|
+
it 'raises an error' do
|
19
|
+
expect { filter }.to raise_error ActiveInteraction::InvalidFilterError
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'with a nested default' do
|
24
|
+
let(:block) { Proc.new { array default: nil } }
|
25
|
+
|
26
|
+
it 'raises an error' do
|
27
|
+
expect { filter }.to raise_error ActiveInteraction::InvalidDefaultError
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '#cast' do
|
8
32
|
context 'with an Array' do
|
9
33
|
let(:value) { [] }
|
10
34
|
|
11
35
|
it 'returns the Array' do
|
12
|
-
expect(
|
36
|
+
expect(filter.cast(value)).to eq value
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'with a heterogenous Array' do
|
41
|
+
let(:value) { [[], false, 0.0, {}, 0, '', :''] }
|
42
|
+
|
43
|
+
it 'returns the Array' do
|
44
|
+
expect(filter.cast(value)).to eq value
|
13
45
|
end
|
14
46
|
end
|
15
47
|
|
16
|
-
context 'with a
|
48
|
+
context 'with a nested filter' do
|
17
49
|
let(:block) { Proc.new { array } }
|
18
50
|
|
19
|
-
context 'with an Array
|
20
|
-
let(:value) { [
|
51
|
+
context 'with an Array' do
|
52
|
+
let(:value) { [] }
|
21
53
|
|
22
54
|
it 'returns the Array' do
|
23
|
-
expect(
|
55
|
+
expect(filter.cast(value)).to eq value
|
24
56
|
end
|
25
57
|
end
|
26
58
|
|
27
|
-
context 'with an Array of
|
28
|
-
let(:value) { [
|
59
|
+
context 'with an Array of Arrays' do
|
60
|
+
let(:value) { [[]] }
|
29
61
|
|
30
|
-
it '
|
31
|
-
expect
|
32
|
-
result
|
33
|
-
}.to raise_error ActiveInteraction::InvalidNestedValue
|
62
|
+
it 'returns the Array' do
|
63
|
+
expect(filter.cast(value)).to eq value
|
34
64
|
end
|
35
65
|
end
|
36
|
-
end
|
37
|
-
|
38
|
-
context 'with a nested block' do
|
39
|
-
let(:block) { Proc.new { array { array } } }
|
40
|
-
let(:value) { [[[]]] }
|
41
|
-
|
42
|
-
it 'returns the Array' do
|
43
|
-
expect(result).to eql value
|
44
|
-
end
|
45
|
-
end
|
46
66
|
|
47
|
-
|
48
|
-
|
49
|
-
let(:value) { [] }
|
67
|
+
context 'with a heterogenous Array' do
|
68
|
+
let(:value) { [[], false, 0.0, {}, 0, '', :''] }
|
50
69
|
|
51
|
-
|
52
|
-
|
70
|
+
it 'raises an error' do
|
71
|
+
expect{
|
72
|
+
filter.cast(value)
|
73
|
+
}.to raise_error ActiveInteraction::InvalidValueError
|
74
|
+
end
|
53
75
|
end
|
54
76
|
end
|
55
77
|
end
|
@@ -1,31 +1,23 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ActiveInteraction::BooleanFilter do
|
3
|
+
describe ActiveInteraction::BooleanFilter, :filter do
|
4
4
|
include_context 'filters'
|
5
5
|
it_behaves_like 'a filter'
|
6
6
|
|
7
|
-
describe '
|
8
|
-
context 'with true' do
|
9
|
-
let(:value) { true }
|
10
|
-
|
11
|
-
it 'returns true' do
|
12
|
-
expect(result).to eql true
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
7
|
+
describe '#cast' do
|
16
8
|
context 'with false' do
|
17
9
|
let(:value) { false }
|
18
10
|
|
19
11
|
it 'returns false' do
|
20
|
-
expect(
|
12
|
+
expect(filter.cast(value)).to be_false
|
21
13
|
end
|
22
14
|
end
|
23
15
|
|
24
|
-
context 'with
|
25
|
-
let(:value) {
|
16
|
+
context 'with true' do
|
17
|
+
let(:value) { true }
|
26
18
|
|
27
19
|
it 'returns true' do
|
28
|
-
expect(
|
20
|
+
expect(filter.cast(value)).to be_true
|
29
21
|
end
|
30
22
|
end
|
31
23
|
|
@@ -33,7 +25,15 @@ describe ActiveInteraction::BooleanFilter do
|
|
33
25
|
let(:value) { '0' }
|
34
26
|
|
35
27
|
it 'returns false' do
|
36
|
-
expect(
|
28
|
+
expect(filter.cast(value)).to be_false
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'with "1"' do
|
33
|
+
let(:value) { '1' }
|
34
|
+
|
35
|
+
it 'returns true' do
|
36
|
+
expect(filter.cast(value)).to be_true
|
37
37
|
end
|
38
38
|
end
|
39
39
|
end
|
@@ -1,48 +1,60 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ActiveInteraction::DateFilter do
|
3
|
+
describe ActiveInteraction::DateFilter, :filter do
|
4
4
|
include_context 'filters'
|
5
5
|
it_behaves_like 'a filter'
|
6
6
|
|
7
|
-
|
7
|
+
shared_context 'with format' do
|
8
|
+
let(:format) { '%d/%m/%Y' }
|
9
|
+
|
10
|
+
before do
|
11
|
+
options.merge!(format: format)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#cast' do
|
8
16
|
context 'with a Date' do
|
9
|
-
let(:value) { Date.
|
17
|
+
let(:value) { Date.new }
|
10
18
|
|
11
19
|
it 'returns the Date' do
|
12
|
-
expect(
|
20
|
+
expect(filter.cast(value)).to eq value
|
13
21
|
end
|
14
22
|
end
|
15
23
|
|
16
|
-
context 'with a
|
17
|
-
let(:value) { '
|
24
|
+
context 'with a String' do
|
25
|
+
let(:value) { '2011-12-13' }
|
18
26
|
|
19
|
-
it '
|
20
|
-
expect(
|
27
|
+
it 'returns a Date' do
|
28
|
+
expect(filter.cast(value)).to eq Date.parse(value)
|
21
29
|
end
|
22
30
|
|
23
|
-
context 'with
|
24
|
-
|
31
|
+
context 'with format' do
|
32
|
+
include_context 'with format'
|
25
33
|
|
26
|
-
|
34
|
+
let(:value) { '13/12/2011' }
|
27
35
|
|
28
|
-
it '
|
29
|
-
expect(
|
36
|
+
it 'returns a Date' do
|
37
|
+
expect(filter.cast(value)).to eq Date.strptime(value, format)
|
30
38
|
end
|
31
39
|
end
|
32
40
|
end
|
33
41
|
|
34
42
|
context 'with an invalid String' do
|
35
|
-
let(:value) { '
|
43
|
+
let(:value) { 'invalid' }
|
36
44
|
|
37
45
|
it 'raises an error' do
|
38
|
-
expect {
|
46
|
+
expect {
|
47
|
+
filter.cast(value)
|
48
|
+
}.to raise_error ActiveInteraction::InvalidValueError
|
39
49
|
end
|
40
50
|
|
41
|
-
context 'with
|
42
|
-
|
51
|
+
context 'with format' do
|
52
|
+
include_context 'with format'
|
43
53
|
|
44
54
|
it 'raises an error' do
|
45
|
-
expect {
|
55
|
+
expect {
|
56
|
+
filter.cast(value)
|
57
|
+
}.to raise_error ActiveInteraction::InvalidValueError
|
46
58
|
end
|
47
59
|
end
|
48
60
|
end
|
@@ -1,48 +1,60 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ActiveInteraction::DateTimeFilter do
|
3
|
+
describe ActiveInteraction::DateTimeFilter, :filter do
|
4
4
|
include_context 'filters'
|
5
5
|
it_behaves_like 'a filter'
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
shared_context 'with format' do
|
8
|
+
let(:format) { '%d/%m/%Y %H:%M:%S %:z' }
|
9
|
+
|
10
|
+
before do
|
11
|
+
options.merge!(format: format)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#cast' do
|
16
|
+
context 'with a Datetime' do
|
17
|
+
let(:value) { DateTime.new }
|
10
18
|
|
11
19
|
it 'returns the DateTime' do
|
12
|
-
expect(
|
20
|
+
expect(filter.cast(value)).to eq value
|
13
21
|
end
|
14
22
|
end
|
15
23
|
|
16
|
-
context 'with a
|
17
|
-
let(:value) { '
|
24
|
+
context 'with a String' do
|
25
|
+
let(:value) { '2011-12-13T14:15:16+17:18' }
|
18
26
|
|
19
|
-
it '
|
20
|
-
expect(
|
27
|
+
it 'returns a DateTime' do
|
28
|
+
expect(filter.cast(value)).to eq DateTime.parse(value)
|
21
29
|
end
|
22
30
|
|
23
|
-
context 'with
|
24
|
-
|
31
|
+
context 'with format' do
|
32
|
+
include_context 'with format'
|
25
33
|
|
26
|
-
|
34
|
+
let(:value) { '13/12/2011 14:15:16 +17:18' }
|
27
35
|
|
28
|
-
it '
|
29
|
-
expect(
|
36
|
+
it 'returns a DateTime' do
|
37
|
+
expect(filter.cast(value)).to eq DateTime.strptime(value, format)
|
30
38
|
end
|
31
39
|
end
|
32
40
|
end
|
33
41
|
|
34
42
|
context 'with an invalid String' do
|
35
|
-
let(:value) { '
|
43
|
+
let(:value) { 'invalid' }
|
36
44
|
|
37
45
|
it 'raises an error' do
|
38
|
-
expect {
|
46
|
+
expect {
|
47
|
+
filter.cast(value)
|
48
|
+
}.to raise_error ActiveInteraction::InvalidValueError
|
39
49
|
end
|
40
50
|
|
41
|
-
context 'with
|
42
|
-
|
51
|
+
context 'with format' do
|
52
|
+
include_context 'with format'
|
43
53
|
|
44
54
|
it 'raises an error' do
|
45
|
-
expect {
|
55
|
+
expect {
|
56
|
+
filter.cast(value)
|
57
|
+
}.to raise_error ActiveInteraction::InvalidValueError
|
46
58
|
end
|
47
59
|
end
|
48
60
|
end
|
@@ -1,15 +1,15 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ActiveInteraction::FileFilter do
|
3
|
+
describe ActiveInteraction::FileFilter, :filter do
|
4
4
|
include_context 'filters'
|
5
5
|
it_behaves_like 'a filter'
|
6
6
|
|
7
|
-
describe '
|
7
|
+
describe '#cast' do
|
8
8
|
context 'with a File' do
|
9
|
-
let(:value) { File.
|
9
|
+
let(:value) { File.new(__FILE__) }
|
10
10
|
|
11
11
|
it 'returns the File' do
|
12
|
-
expect(
|
12
|
+
expect(filter.cast(value)).to eq value
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
@@ -17,15 +17,15 @@ describe ActiveInteraction::FileFilter do
|
|
17
17
|
let(:value) { Tempfile.new(SecureRandom.hex) }
|
18
18
|
|
19
19
|
it 'returns the Tempfile' do
|
20
|
-
expect(
|
20
|
+
expect(filter.cast(value)).to eq value
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
context 'with
|
24
|
+
context 'with an object that responds to #tempfile' do
|
25
25
|
let(:value) { double(tempfile: Tempfile.new(SecureRandom.hex)) }
|
26
26
|
|
27
27
|
it 'returns the Tempfile' do
|
28
|
-
expect(
|
28
|
+
expect(filter.cast(value)).to eq value.tempfile
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
@@ -1,39 +1,41 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ActiveInteraction::FloatFilter do
|
3
|
+
describe ActiveInteraction::FloatFilter, :filter do
|
4
4
|
include_context 'filters'
|
5
5
|
it_behaves_like 'a filter'
|
6
6
|
|
7
|
-
describe '
|
7
|
+
describe '#cast' do
|
8
8
|
context 'with a Float' do
|
9
9
|
let(:value) { rand }
|
10
10
|
|
11
11
|
it 'returns the Float' do
|
12
|
-
expect(
|
12
|
+
expect(filter.cast(value)).to eq value
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
context 'with
|
16
|
+
context 'with a Numeric' do
|
17
17
|
let(:value) { rand(1 << 16) }
|
18
18
|
|
19
|
-
it '
|
20
|
-
expect(
|
19
|
+
it 'returns a Float' do
|
20
|
+
expect(filter.cast(value)).to eq value.to_f
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
context 'with a
|
24
|
+
context 'with a String' do
|
25
25
|
let(:value) { rand.to_s }
|
26
26
|
|
27
|
-
it '
|
28
|
-
expect(
|
27
|
+
it 'returns a Float' do
|
28
|
+
expect(filter.cast(value)).to eq Float(value)
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
32
|
context 'with an invalid String' do
|
33
|
-
let(:value) { '
|
33
|
+
let(:value) { 'invalid' }
|
34
34
|
|
35
35
|
it 'raises an error' do
|
36
|
-
expect {
|
36
|
+
expect {
|
37
|
+
filter.cast(value)
|
38
|
+
}.to raise_error ActiveInteraction::InvalidValueError
|
37
39
|
end
|
38
40
|
end
|
39
41
|
end
|