active_interaction 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d65a75d08fc8be7355ec025ebaed8b08873819c0
4
- data.tar.gz: 3d75fd27e50013507a73176b40ba466e949b937f
3
+ metadata.gz: eac224974be5bdaafca02a1266a62b5a7d18e835
4
+ data.tar.gz: f6224fbee6808c51696a42af86a4138d20d6bd56
5
5
  SHA512:
6
- metadata.gz: d807465b1015098c9c77c33c4429cd685fdf2eb268c83388ecd8b57110f12d1716309f1100c42fd2fda9b4bbefc9c612545c74905e6a17117fed45710ad5925c
7
- data.tar.gz: 10c78c1906541dc8b89060c16bc957a44a9cd9519988e0633382c9609d4b101fa82ce9420df5fbf8b83a6f7ae9db4f76549c7075f5656d41a9423a52becd89db
6
+ metadata.gz: b816a2a4b4c463e5cc208862950d5b789c4d20c70df9a2cb98c7a8013b78a734426709d5e08587d2daea14034860521fb6286893e45c05418fab08331e3076be
7
+ data.tar.gz: fa0ec07a406a4c4d8c0d22270abaf05e2eb17bb3bd64a1590112fcca26649817a08aa48ad25f6785fc7caefd1e835eed14e4b4a8d613587cc4bb9f841c173e95
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [2.1.0][] (2015-07-30)
2
+
3
+ ## Added
4
+
5
+ - [#295][]: Added `given?` predicate method to see if an input was passed to
6
+ `run`.
7
+
1
8
  # [2.0.1][] (2015-05-27)
2
9
 
3
10
  ## Fixed
@@ -423,6 +430,7 @@ For help upgrading to version 2, please read [the announcement post][].
423
430
 
424
431
  - Initial release.
425
432
 
433
+ [2.1.0]: https://github.com/orgsync/active_interaction/compare/v2.0.1...v2.1.0
426
434
  [2.0.1]: https://github.com/orgsync/active_interaction/compare/v2.0.0...v2.0.1
427
435
  [2.0.0]: https://github.com/orgsync/active_interaction/compare/v1.6.0...v2.0.0
428
436
  [1.6.0]: https://github.com/orgsync/active_interaction/compare/v1.5.1...v1.6.0
@@ -556,5 +564,6 @@ For help upgrading to version 2, please read [the announcement post][].
556
564
  [#269]: https://github.com/orgsync/active_interaction/issues/269
557
565
  [#286]: https://github.com/orgsync/active_interaction/issues/286
558
566
  [#289]: https://github.com/orgsync/active_interaction/issues/289
567
+ [#295]: https://github.com/orgsync/active_interaction/issues/295
559
568
 
560
569
  [the announcement post]: http://devblog.orgsync.com/2015/05/06/announcing-active-interaction-2/
data/README.md CHANGED
@@ -1,7 +1,3 @@
1
- <p align="center">
2
- <img alt="" src="https://a.pomf.se/auvctt.svg" width="250">
3
- </p>
4
-
5
1
  <h1 align="center">
6
2
  <a href="https://github.com/orgsync/active_interaction">
7
3
  ActiveInteraction
@@ -66,6 +62,7 @@ Read more on [the project page][] or check out [the full documentation][].
66
62
  - [Descriptions](#descriptions)
67
63
  - [Errors](#errors)
68
64
  - [Forms](#forms)
65
+ - [Optional inputs](#optional-inputs)
69
66
  - [Predicates](#predicates)
70
67
  - [Translations](#translations)
71
68
  - [Credits](#credits)
@@ -75,13 +72,13 @@ Read more on [the project page][] or check out [the full documentation][].
75
72
  Add it to your Gemfile:
76
73
 
77
74
  ``` rb
78
- gem 'active_interaction', '~> 2.0'
75
+ gem 'active_interaction', '~> 2.1'
79
76
  ```
80
77
 
81
78
  Or install it manually:
82
79
 
83
80
  ``` sh
84
- $ gem install active_interaction --version '~> 2.0'
81
+ $ gem install active_interaction --version '~> 2.1'
85
82
  ```
86
83
 
87
84
  This project uses [Semantic Versioning][]. Check out [the change log][] for a
@@ -257,8 +254,14 @@ array :birthdays do
257
254
  end
258
255
  ```
259
256
 
260
- Note that filters inside an array block don't have names. Also you can only
261
- have one filter inside an array block.
257
+ Note that you can only have one filter inside an array block, and it must not
258
+ have a name.
259
+
260
+ ``` rb
261
+ array :cows do
262
+ object class: Cow
263
+ end
264
+ ```
262
265
 
263
266
  ### Boolean
264
267
 
@@ -1161,6 +1164,44 @@ used to define the inputs on your interaction will relay type information to
1161
1164
  these gems. As a result, form fields will automatically use the appropriate
1162
1165
  input type.
1163
1166
 
1167
+ ### Optional inputs
1168
+
1169
+ Optional inputs can be defined by using the `:default` option as described in
1170
+ [the Filters section][]. Within the interaction, provided and default values
1171
+ are merged to create `inputs`. There are times where it is useful to know
1172
+ whether a value was passed to `run` or the result of a filter default. In
1173
+ particular, it is useful when `nil` is an acceptable value. For example, you
1174
+ may optionally track your users' birthdays. You can use the `given?` predicate
1175
+ to see if an input was even passed to `run`.
1176
+
1177
+ ``` rb
1178
+ class UpdateUser < ActiveInteraction::Base
1179
+ object :user
1180
+ date :birthday,
1181
+ default: nil
1182
+
1183
+ def execute
1184
+ user.birthday = birthday if given?(:birthday)
1185
+ errors.merge!(user) unless user.save
1186
+ user
1187
+ end
1188
+ end
1189
+ ```
1190
+
1191
+ Now you have a few options. If you don't want to update their birthday, leave
1192
+ it out of the hash. If you want to remove their birthday, set `birthday: nil`.
1193
+ And if you want to update it, pass in the new value as usual.
1194
+
1195
+ ``` rb
1196
+ user = User.find(...)
1197
+ # Don't update their birthday.
1198
+ UpdateUser.run!(user: user)
1199
+ # Remove their birthday.
1200
+ UpdateUser.run!(user: user, birthday: nil)
1201
+ # Update their birthday.
1202
+ UpdateUser.run!(user: user, birthday: Date.new(2000, 1, 2))
1203
+ ```
1204
+
1164
1205
  ### Predicates
1165
1206
 
1166
1207
  ActiveInteraction creates a predicate method for every input defined by a filter. So if you have an input called `foo`, there will be a predicate method called `#foo?`. That method will tell you if the input was given (that is, if it was not `nil`).
@@ -1265,4 +1306,5 @@ Logo design by [Tyler Lee][].
1265
1306
  [the mit license]: LICENSE.md
1266
1307
  [formtastic]: https://rubygems.org/gems/formtastic
1267
1308
  [simple_form]: https://rubygems.org/gems/simple_form
1309
+ [the filters section]: #filters
1268
1310
  [tyler lee]: https://github.com/tylerlee
@@ -1,6 +1,7 @@
1
1
  # coding: utf-8
2
2
 
3
3
  require 'active_support/core_ext/hash/indifferent_access'
4
+ require 'set'
4
5
 
5
6
  module ActiveInteraction
6
7
  # @abstract Subclass and override {#execute} to implement a custom
@@ -194,6 +195,28 @@ module ActiveInteraction
194
195
  end
195
196
  end
196
197
 
198
+ # Returns `true` if the given key was in the hash passed to {.run}.
199
+ # Otherwise returns `false`. Use this to figure out if an input was given,
200
+ # even if it was `nil`.
201
+ #
202
+ # @example
203
+ # class Example < ActiveInteraction::Base
204
+ # integer :x, default: nil
205
+ # def execute; given?(:x) end
206
+ # end
207
+ # Example.run!() # => false
208
+ # Example.run!(x: nil) # => true
209
+ # Example.run!(x: rand) # => true
210
+ #
211
+ # @param input [#to_sym]
212
+ #
213
+ # @return [Boolean]
214
+ #
215
+ # @since 2.1.0
216
+ def given?(input)
217
+ @_interaction_keys.include?(input.to_sym)
218
+ end
219
+
197
220
  protected
198
221
 
199
222
  def run_validations!
@@ -206,6 +229,8 @@ module ActiveInteraction
206
229
 
207
230
  # @param inputs [Hash{Symbol => Object}]
208
231
  def process_inputs(inputs)
232
+ @_interaction_keys = inputs.keys.to_set & self.class.filters.keys
233
+
209
234
  inputs.each do |key, value|
210
235
  fail InvalidValueError, key.inspect if InputProcessor.reserved?(key)
211
236
 
@@ -5,5 +5,5 @@ module ActiveInteraction
5
5
  # The version number.
6
6
  #
7
7
  # @return [Gem::Version]
8
- VERSION = Gem::Version.new('2.0.1')
8
+ VERSION = Gem::Version.new('2.1.0')
9
9
  end
@@ -9,7 +9,7 @@ require 'active_model'
9
9
  #
10
10
  # @since 1.0.0
11
11
  #
12
- # @version 2.0.1
12
+ # @version 2.1.0
13
13
  module ActiveInteraction
14
14
  end
15
15
 
@@ -365,6 +365,55 @@ describe ActiveInteraction::Base do
365
365
  end
366
366
  end
367
367
 
368
+ describe '#given?' do
369
+ let(:described_class) do
370
+ Class.new(TestInteraction) do
371
+ float :x,
372
+ default: nil
373
+
374
+ def execute
375
+ given?(:x)
376
+ end
377
+ end
378
+ end
379
+
380
+ it 'is false when the input is not given' do
381
+ expect(result).to be false
382
+ end
383
+
384
+ it 'is true when the input is nil' do
385
+ inputs[:x] = nil
386
+ expect(result).to be true
387
+ end
388
+
389
+ it 'is true when the input is given' do
390
+ inputs[:x] = rand
391
+ expect(result).to be true
392
+ end
393
+
394
+ it 'symbolizes its argument' do
395
+ described_class.class_exec do
396
+ def execute
397
+ given?('x')
398
+ end
399
+ end
400
+
401
+ inputs[:x] = rand
402
+ expect(result).to be true
403
+ end
404
+
405
+ it 'only tracks inputs with filters' do
406
+ described_class.class_exec do
407
+ def execute
408
+ given?(:y)
409
+ end
410
+ end
411
+
412
+ inputs[:y] = rand
413
+ expect(result).to be false
414
+ end
415
+ end
416
+
368
417
  context 'inheritance' do
369
418
  context 'filters' do
370
419
  let(:described_class) { InteractionWithFilter }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_interaction
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Lasseigne
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-05-27 00:00:00.000000000 Z
12
+ date: 2015-07-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -57,14 +57,14 @@ dependencies:
57
57
  requirements:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
- version: '1.9'
60
+ version: '1.10'
61
61
  type: :development
62
62
  prerelease: false
63
63
  version_requirements: !ruby/object:Gem::Requirement
64
64
  requirements:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
- version: '1.9'
67
+ version: '1.10'
68
68
  - !ruby/object:Gem::Dependency
69
69
  name: coveralls
70
70
  requirement: !ruby/object:Gem::Requirement
@@ -85,14 +85,14 @@ dependencies:
85
85
  requirements:
86
86
  - - "~>"
87
87
  - !ruby/object:Gem::Version
88
- version: '4.5'
88
+ version: '4.6'
89
89
  type: :development
90
90
  prerelease: false
91
91
  version_requirements: !ruby/object:Gem::Requirement
92
92
  requirements:
93
93
  - - "~>"
94
94
  - !ruby/object:Gem::Version
95
- version: '4.5'
95
+ version: '4.6'
96
96
  - !ruby/object:Gem::Dependency
97
97
  name: guard-rubocop
98
98
  requirement: !ruby/object:Gem::Requirement
@@ -113,14 +113,14 @@ dependencies:
113
113
  requirements:
114
114
  - - "~>"
115
115
  - !ruby/object:Gem::Version
116
- version: '1.7'
116
+ version: '1.8'
117
117
  type: :development
118
118
  prerelease: false
119
119
  version_requirements: !ruby/object:Gem::Requirement
120
120
  requirements:
121
121
  - - "~>"
122
122
  - !ruby/object:Gem::Version
123
- version: '1.7'
123
+ version: '1.8'
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: rake
126
126
  requirement: !ruby/object:Gem::Requirement
@@ -141,28 +141,28 @@ dependencies:
141
141
  requirements:
142
142
  - - "~>"
143
143
  - !ruby/object:Gem::Version
144
- version: '3.2'
144
+ version: '3.3'
145
145
  type: :development
146
146
  prerelease: false
147
147
  version_requirements: !ruby/object:Gem::Requirement
148
148
  requirements:
149
149
  - - "~>"
150
150
  - !ruby/object:Gem::Version
151
- version: '3.2'
151
+ version: '3.3'
152
152
  - !ruby/object:Gem::Dependency
153
153
  name: rubocop
154
154
  requirement: !ruby/object:Gem::Requirement
155
155
  requirements:
156
156
  - - "~>"
157
157
  - !ruby/object:Gem::Version
158
- version: '0.30'
158
+ version: '0.32'
159
159
  type: :development
160
160
  prerelease: false
161
161
  version_requirements: !ruby/object:Gem::Requirement
162
162
  requirements:
163
163
  - - "~>"
164
164
  - !ruby/object:Gem::Version
165
- version: '0.30'
165
+ version: '0.32'
166
166
  - !ruby/object:Gem::Dependency
167
167
  name: yard
168
168
  requirement: !ruby/object:Gem::Requirement
@@ -291,7 +291,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
291
291
  version: '0'
292
292
  requirements: []
293
293
  rubyforge_project:
294
- rubygems_version: 2.4.6
294
+ rubygems_version: 2.4.5
295
295
  signing_key:
296
296
  specification_version: 4
297
297
  summary: Manage application specific business logic.