active_interaction 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 0.1.2
2
+
3
+ - `execute` will now have the filtered version of the values passed to `run` or `run!` as was intended.
4
+
1
5
  # 0.1.1
2
6
 
3
7
  - Correct gemspec dependencies on activemodel.
data/README.md CHANGED
@@ -21,7 +21,7 @@ This project uses [semantic versioning][].
21
21
  Add it to your Gemfile:
22
22
 
23
23
  ~~~ rb
24
- gem 'active_interaction', '~> 0.1.1'
24
+ gem 'active_interaction', '~> 0.1.2'
25
25
  ~~~
26
26
 
27
27
  And then execute:
@@ -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)
@@ -21,7 +21,7 @@ module ActiveInteraction
21
21
  return_nil(options[:allow_nil])
22
22
  else
23
23
  bad_value
24
- end
24
+ end
25
25
  end
26
26
 
27
27
  def self.return_nil(allow_nil)
@@ -48,7 +48,7 @@ module ActiveInteraction
48
48
  if filter_methods.count > 1
49
49
  raise ArgumentError, 'Array filter blocks can only contain one filter.'
50
50
  else
51
- filter_method = filter_methods.first
51
+ filter_methods.first
52
52
  end
53
53
  end
54
54
  private_class_method :get_filter_method
@@ -28,7 +28,7 @@ module ActiveInteraction
28
28
 
29
29
  def self.extract_file(value)
30
30
  if value.respond_to?(:tempfile)
31
- value = value.tempfile
31
+ value.tempfile
32
32
  else
33
33
  value
34
34
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveInteraction
2
- VERSION = Gem::Version.new('0.1.1')
2
+ VERSION = Gem::Version.new('0.1.2')
3
3
  end
@@ -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
- describe InteractionWithAttribute do
43
- let(:described_class) { InteractionWithAttribute }
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 types' do
99
+ it 'raises an error for an invalid filter type' do
70
100
  expect {
71
- class InteractionWithInvalidFilter < described_class
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
- describe InteractionWithFilter do
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
- describe InteractionWithFilters do
91
- let(:described_class) { InteractionWithFilters }
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
- describe InteractionWithFilter do
140
+ context 'with a filter' do
106
141
  let(:described_class) { InteractionWithFilter }
107
142
  let(:thing) { rand }
108
143
 
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  module ActiveInteraction
4
- class TestFilter < Filter; end
4
+ TestFilter = Class.new(Filter)
5
5
  end
6
6
 
7
7
  describe ActiveInteraction::Filter do
@@ -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('temp') }
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('temp')) }
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.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 00:00:00.000000000 Z
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: -3542562646665336521
238
+ hash: 669983182544330814
239
239
  version: '0'
240
240
  none: false
241
241
  requirements: []