active_interaction 0.1.1 → 0.1.2
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.
- data/CHANGELOG.md +4 -0
- data/README.md +1 -1
- data/lib/active_interaction/base.rb +14 -1
- data/lib/active_interaction/filter.rb +1 -1
- data/lib/active_interaction/filters/array_filter.rb +1 -1
- data/lib/active_interaction/filters/file_filter.rb +1 -1
- data/lib/active_interaction/version.rb +1 -1
- data/spec/active_interaction/base_spec.rb +60 -25
- data/spec/active_interaction/filter_spec.rb +1 -1
- data/spec/active_interaction/filters/file_filter_spec.rb +2 -2
- metadata +3 -3
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -58,7 +58,7 @@ module ActiveInteraction
|
|
58
58
|
|
59
59
|
options.each do |attribute, value|
|
60
60
|
if respond_to?("#{attribute}=")
|
61
|
-
send("#{attribute}=", value)
|
61
|
+
send("_filter__#{attribute}=", value)
|
62
62
|
else
|
63
63
|
instance_variable_set("@#{attribute}", value)
|
64
64
|
end
|
@@ -118,6 +118,19 @@ module ActiveInteraction
|
|
118
118
|
|
119
119
|
# @private
|
120
120
|
def self.filter_attr_accessor(filter, attribute, options, &block)
|
121
|
+
filter_attr_writer = "_filter__#{attribute}="
|
122
|
+
define_method(filter_attr_writer) do |value|
|
123
|
+
filtered_value =
|
124
|
+
begin
|
125
|
+
filter.prepare(attribute, value, options, &block)
|
126
|
+
rescue InvalidValue, MissingValue
|
127
|
+
value
|
128
|
+
end
|
129
|
+
|
130
|
+
instance_variable_set("@#{attribute}", filtered_value)
|
131
|
+
end
|
132
|
+
private filter_attr_writer
|
133
|
+
|
121
134
|
attr_writer attribute
|
122
135
|
|
123
136
|
if options.has_key?(:default)
|
@@ -4,16 +4,6 @@ describe ActiveInteraction::Base do
|
|
4
4
|
let(:options) { {} }
|
5
5
|
subject(:interaction) { described_class.new(options) }
|
6
6
|
|
7
|
-
class InteractionWithAttribute < described_class
|
8
|
-
attr_reader :thing
|
9
|
-
|
10
|
-
validates :thing, presence: true
|
11
|
-
|
12
|
-
def execute
|
13
|
-
thing
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
7
|
class InteractionWithFilter < described_class
|
18
8
|
float :thing
|
19
9
|
|
@@ -22,12 +12,6 @@ describe ActiveInteraction::Base do
|
|
22
12
|
end
|
23
13
|
end
|
24
14
|
|
25
|
-
class InteractionWithFilters < described_class
|
26
|
-
float :thing1, :thing2
|
27
|
-
|
28
|
-
def execute; end
|
29
|
-
end
|
30
|
-
|
31
15
|
describe '.new(options = {})' do
|
32
16
|
it 'does not allow :result as an option' do
|
33
17
|
options.merge!(result: nil)
|
@@ -39,8 +23,22 @@ describe ActiveInteraction::Base do
|
|
39
23
|
expect { interaction }.to raise_error ArgumentError
|
40
24
|
end
|
41
25
|
|
42
|
-
|
43
|
-
let(:described_class)
|
26
|
+
context 'with an attribute' do
|
27
|
+
let(:described_class) do
|
28
|
+
Class.new(ActiveInteraction::Base) do
|
29
|
+
attr_reader :thing
|
30
|
+
|
31
|
+
validates :thing, presence: true
|
32
|
+
|
33
|
+
def self.model_name
|
34
|
+
ActiveModel::Name.new(self, nil, SecureRandom.hex)
|
35
|
+
end
|
36
|
+
|
37
|
+
def execute
|
38
|
+
thing
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
44
42
|
let(:thing) { SecureRandom.hex }
|
45
43
|
|
46
44
|
context 'failing validations' do
|
@@ -63,19 +61,50 @@ describe ActiveInteraction::Base do
|
|
63
61
|
end
|
64
62
|
end
|
65
63
|
end
|
64
|
+
|
65
|
+
describe InteractionWithFilter do
|
66
|
+
let(:described_class) { InteractionWithFilter }
|
67
|
+
|
68
|
+
context 'failing validations' do
|
69
|
+
before { options.merge!(thing: thing) }
|
70
|
+
|
71
|
+
context 'InvalidValue' do
|
72
|
+
let(:thing) { 'a' }
|
73
|
+
|
74
|
+
it 'sets the attribute to the filtered value' do
|
75
|
+
expect(interaction.thing).to equal thing
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context 'MissingValue' do
|
80
|
+
let(:thing) { nil }
|
81
|
+
|
82
|
+
it 'sets the attribute to the filtered value' do
|
83
|
+
expect(interaction.thing).to equal thing
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'passing validations' do
|
89
|
+
before { options.merge!(thing: 1) }
|
90
|
+
|
91
|
+
it 'sets the attribute to the filtered value' do
|
92
|
+
expect(interaction.thing).to eql 1.0
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
66
96
|
end
|
67
97
|
|
68
98
|
describe '.method_missing(filter_type, *args, &block)' do
|
69
|
-
it 'raises an error for invalid filter
|
99
|
+
it 'raises an error for an invalid filter type' do
|
70
100
|
expect {
|
71
|
-
|
101
|
+
Class.new(described_class) do
|
72
102
|
not_a_valid_filter_type :thing
|
73
|
-
def execute; end
|
74
103
|
end
|
75
104
|
}.to raise_error NoMethodError
|
76
105
|
end
|
77
106
|
|
78
|
-
|
107
|
+
context 'with a filter' do
|
79
108
|
let(:described_class) { InteractionWithFilter }
|
80
109
|
|
81
110
|
it 'adds an attr_reader' do
|
@@ -87,8 +116,14 @@ describe ActiveInteraction::Base do
|
|
87
116
|
end
|
88
117
|
end
|
89
118
|
|
90
|
-
|
91
|
-
let(:described_class)
|
119
|
+
context 'with multiple filters' do
|
120
|
+
let(:described_class) do
|
121
|
+
Class.new(ActiveInteraction::Base) do
|
122
|
+
float :thing1, :thing2
|
123
|
+
|
124
|
+
def execute; end
|
125
|
+
end
|
126
|
+
end
|
92
127
|
|
93
128
|
%w(thing1 thing2).each do |thing|
|
94
129
|
it "adds an attr_reader for #{thing}" do
|
@@ -102,7 +137,7 @@ describe ActiveInteraction::Base do
|
|
102
137
|
end
|
103
138
|
end
|
104
139
|
|
105
|
-
|
140
|
+
context 'with a filter' do
|
106
141
|
let(:described_class) { InteractionWithFilter }
|
107
142
|
let(:thing) { rand }
|
108
143
|
|
@@ -14,7 +14,7 @@ describe ActiveInteraction::FileFilter do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
context 'with a Tempfile' do
|
17
|
-
let(:value) { Tempfile.new(
|
17
|
+
let(:value) { Tempfile.new(SecureRandom.hex) }
|
18
18
|
|
19
19
|
it 'returns the Tempfile' do
|
20
20
|
expect(result).to equal value
|
@@ -22,7 +22,7 @@ describe ActiveInteraction::FileFilter do
|
|
22
22
|
end
|
23
23
|
|
24
24
|
context 'with a object that responds to `tempfile`' do
|
25
|
-
let(:value) { double(tempfile: Tempfile.new(
|
25
|
+
let(:value) { double(tempfile: Tempfile.new(SecureRandom.hex)) }
|
26
26
|
|
27
27
|
it 'returns the Tempfile' do
|
28
28
|
expect(result).to equal value.tempfile
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: active_interaction
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Aaron Lasseigne
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-07-
|
13
|
+
date: 2013-07-15 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activemodel
|
@@ -235,7 +235,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
235
235
|
- !ruby/object:Gem::Version
|
236
236
|
segments:
|
237
237
|
- 0
|
238
|
-
hash:
|
238
|
+
hash: 669983182544330814
|
239
239
|
version: '0'
|
240
240
|
none: false
|
241
241
|
requirements: []
|