roqua-support 0.1.11 → 0.1.12

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c0d83dcfdd5a8666dbfa15efa42dc41bc1286944
4
- data.tar.gz: f4c169d8f8b96e4b578e4808c7212b0cb2ee1495
3
+ metadata.gz: 04157f293aa088264645c6571535bbff47532f96
4
+ data.tar.gz: a77b44d28bc6c0604aacbff2d9f3f6d983a0e03e
5
5
  SHA512:
6
- metadata.gz: a11b436cc2a870c201ec997a5e23f6d47148b54d9ce5978fc08b5ef248db4382de8e2d2c9b440085249b3b978123bb6bb21d7db3b41995f93d2a9777c0687069
7
- data.tar.gz: 5de99e2f313f6c21e70f938db3d79ad0533dc025006cc8f57d6e8d69adb18a4ffd1917bb2adbf97cd3fb53137ac188df88c8d01126ed81201ed3fa51196bdce4
6
+ metadata.gz: a4afb549cb7124a21623c25144917a4ddf8d1f771da44aa10bf61b1fb342205f1ee34663c9124191b164a9071d9293afb956fd10c5ca40168756f7f34105571c
7
+ data.tar.gz: 2316b715438c76b82f7a9e8820eb029122c16d749396c4545df2611705587de49f69e18476eab21d0a242d604112f089feefeff5c6ae58285da8e6bbbb0615ee
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.1.12 / 2014-11-10
2
+
3
+ * Added roqua/core_ext/active_interaction/filters/date_time_as_unix_extension that allows date_times attributes to be set by a unix timestamp.
4
+ * Added roqua/core_ext/active_interaction/filters/duration_filter that allows ActiveSupport::Duration attributes.
5
+
1
6
  ## 0.1.11 / 2014-11-06
2
7
 
3
8
  * Don't catch Mysql2 errors
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- roqua-support (0.1.11)
4
+ roqua-support (0.1.12)
5
5
  activesupport (>= 3.2, < 5.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -76,6 +76,30 @@ class ApiAreaController < ApplicationController
76
76
  ...
77
77
  ```
78
78
 
79
+ ### ActiveInteraction extensions
80
+
81
+ ```
82
+ require 'roqua/core_ext/active_interaction/filters/date_time_as_unix_extension'
83
+ ```
84
+
85
+ Allows a date or date_time attribute to be set by a unix time e.g. 1415608242 or '1415608242'.
86
+
87
+
88
+ ```
89
+ require 'roqua/core_ext/active_interaction/filters/duration_filter'
90
+
91
+ class DurationFilterOperation < ActiveInteraction::Base
92
+ duration :duration
93
+ duration :foo, strip: true, default: nil # value is nil if duration is 0.
94
+ end
95
+
96
+ DurationFilterOperation.run(duration: 1.week)
97
+ DurationFilterOperation.run(duration: {value: 1, unit: 'weeks'})
98
+ ```
99
+
100
+ Allows you to specify an ActiveSupport::Duration attribute.
101
+
102
+
79
103
  ## Contributing
80
104
 
81
105
  1. Fork it
@@ -1,5 +1,5 @@
1
1
  module Roqua
2
2
  module Support
3
- VERSION = '0.1.11'
3
+ VERSION = '0.1.12'
4
4
  end
5
5
  end
@@ -0,0 +1,29 @@
1
+ # allow datetimes to be given as unix times for activeinteractions
2
+ module RoquaDateTimeAsUnixFilterExtension
3
+ def cast(value)
4
+ case value
5
+ when Numeric, /^[0-9]+$/
6
+ Time.at(value.to_i).to_datetime
7
+ when ''
8
+ super(nil)
9
+ else
10
+ super
11
+ end
12
+ end
13
+ end
14
+ ActiveInteraction::DateTimeFilter.include RoquaDateTimeAsUnixFilterExtension
15
+
16
+ # allow datetimes to be given as unix times as string
17
+ module RoquaTimeAsUnixFilterExtension
18
+ def cast(value)
19
+ case value
20
+ when /^[0-9]+$/
21
+ Time.at(value.to_i)
22
+ when ''
23
+ super(nil)
24
+ else
25
+ super
26
+ end
27
+ end
28
+ end
29
+ ActiveInteraction::TimeFilter.include RoquaTimeAsUnixFilterExtension
@@ -0,0 +1,46 @@
1
+ I18n.backend.store_translations :en, active_interaction: {
2
+ types: { duration: 'Duration' }
3
+ }
4
+
5
+ module ActiveInteraction
6
+ class Base
7
+ # @!method self.duration(*attributes, options = {})
8
+ # Creates accessors for the attributes and ensures that the value passed to
9
+ # the attributes is a ActiveSupport::Duration.
10
+ # Value can be a hash with a value and unit key
11
+ #
12
+ # @!macro filter_method_params
13
+ # @option options [Boolean] :strip (false) Make nil if value is 0.
14
+ #
15
+ # @example
16
+ # duration :first_name
17
+ # @example
18
+ # duration :first_name, strip: true
19
+ end
20
+
21
+ # @private
22
+ class DurationFilter < Filter
23
+ register :duration
24
+
25
+ def cast(value)
26
+ case value
27
+ when ActiveSupport::Duration
28
+ (value == 0 && strip?) ? super(nil) : value
29
+ when Hash
30
+ if value[:value].present? && (value[:value].to_i != 0 || !strip?)
31
+ value[:value].to_i.send(value[:unit])
32
+ else
33
+ super(nil)
34
+ end
35
+ else
36
+ super
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def strip?
43
+ options.fetch(:strip, false)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,51 @@
1
+ require 'active_interaction'
2
+ require 'active_support/all'
3
+ require 'roqua/core_ext/active_interaction/filters/date_time_as_unix_extension'
4
+
5
+ class DateTimeFilterOperation < ActiveInteraction::Base
6
+ date_time :date_time, default: nil
7
+
8
+ def execute
9
+ date_time
10
+ end
11
+ end
12
+
13
+ describe RoquaDateTimeAsUnixFilterExtension do
14
+ let(:time) { Time.now.change(:usec => 0) }
15
+
16
+ it 'unix integer time translates correctly to datetime' do
17
+ expect(DateTimeFilterOperation.run! date_time: time.to_i).to eq time
18
+ end
19
+
20
+ it 'unix integer time as string translates correctly to datetime' do
21
+ expect(DateTimeFilterOperation.run! date_time: time.to_i.to_s).to eq time
22
+ end
23
+
24
+ it 'transations and empty string to nil' do
25
+ expect(DateTimeFilterOperation.run! date_time: '').to eq nil
26
+ end
27
+ end
28
+
29
+ class TimeFilterOperation < ActiveInteraction::Base
30
+ time :time, default: nil
31
+
32
+ def execute
33
+ time
34
+ end
35
+ end
36
+
37
+ describe RoquaDateTimeAsUnixFilterExtension do
38
+ let(:time) { Time.now.change(:usec => 0) }
39
+
40
+ it 'unix integer time translates correctly to time' do
41
+ expect(TimeFilterOperation.run! time: time.to_i).to eq time
42
+ end
43
+
44
+ it 'unix integer time as string translates correctly to datetime' do
45
+ expect(TimeFilterOperation.run! time: time.to_i.to_s).to eq time
46
+ end
47
+
48
+ it 'transations and empty string to nil' do
49
+ expect(TimeFilterOperation.run! time: '').to eq nil
50
+ end
51
+ end
@@ -0,0 +1,90 @@
1
+ require 'active_interaction'
2
+ require 'active_support/all'
3
+ require 'roqua/core_ext/active_interaction/filters/duration_filter'
4
+
5
+ class DurationFilterOperation < ActiveInteraction::Base
6
+ duration :duration
7
+ duration :stripped_duration, strip: true, default: nil
8
+
9
+ def execute
10
+ {duration: duration, stripped_duration: stripped_duration}
11
+ end
12
+ end
13
+
14
+ describe ActiveInteraction::DurationFilter do
15
+ let(:duration) { 1.week }
16
+ let(:stripped_duration) { 1.week }
17
+
18
+
19
+ subject { DurationFilterOperation.run!(duration: duration, stripped_duration: stripped_duration) }
20
+
21
+ describe 'when given a duration object' do
22
+ it 'receives the object correctly' do
23
+ expect(subject[:duration]).to be_a(ActiveSupport::Duration)
24
+ expect(subject[:duration]).to eq 1.week
25
+ end
26
+
27
+ it 'does not strip non-0 values' do
28
+ expect(subject[:stripped_duration]).to be_a(ActiveSupport::Duration)
29
+ expect(subject[:stripped_duration]).to eq 1.week
30
+ end
31
+ end
32
+
33
+ describe 'when given a 0 duration object' do
34
+ let(:duration) { 0.weeks }
35
+ let(:stripped_duration) { 0.weeks }
36
+
37
+ it 'receives the object correctly' do
38
+ expect(subject[:duration]).to be_a(ActiveSupport::Duration)
39
+ expect(subject[:duration]).to eq 0.weeks
40
+ end
41
+
42
+ it 'strips 0 values' do
43
+ expect(subject[:stripped_duration]).to eq nil
44
+ end
45
+ end
46
+
47
+ describe 'when given a hash object' do
48
+ let(:duration) { {value: '1', unit: 'weeks'} }
49
+ let(:stripped_duration) { {value: '1', unit: 'weeks'} }
50
+
51
+ it 'receives the object correctly' do
52
+ expect(subject[:duration]).to be_a(ActiveSupport::Duration)
53
+ expect(subject[:duration]).to eq 1.week
54
+ end
55
+
56
+ it 'does not strip non-0 values' do
57
+ expect(subject[:stripped_duration]).to be_a(ActiveSupport::Duration)
58
+ expect(subject[:stripped_duration]).to eq 1.week
59
+ end
60
+ end
61
+
62
+ describe 'when given a hash {value: "0"} object' do
63
+ let(:duration) { {value: '0', unit: 'weeks'} }
64
+ let(:stripped_duration) { {value: '0', unit: 'weeks'} }
65
+
66
+ it 'receives the object correctly' do
67
+ expect(subject[:duration]).to be_a(ActiveSupport::Duration)
68
+ expect(subject[:duration]).to eq 0.weeks
69
+ end
70
+
71
+ it 'strips 0 values' do
72
+ expect(subject[:stripped_duration]).to eq nil
73
+ end
74
+ end
75
+
76
+ describe 'when given a hash {value: ""} object' do
77
+ let(:duration) { {value: '', unit: 'weeks'} }
78
+
79
+ it 'throws a required error when not stripped' do
80
+ expect { subject }.to raise_error ActiveInteraction::InvalidInteractionError, 'Duration is required'
81
+ end
82
+ end
83
+
84
+ describe 'when given a hash {value: ""} object' do
85
+ let(:stripped_duration) { {value: '', unit: 'weeks'} }
86
+ it 'strips 0 values' do
87
+ expect(subject[:stripped_duration]).to eq nil
88
+ end
89
+ end
90
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roqua-support
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marten Veldthuis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-06 00:00:00.000000000 Z
11
+ date: 2014-11-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -97,6 +97,8 @@ files:
97
97
  - circle.yml
98
98
  - lib/roqua-support.rb
99
99
  - lib/roqua-support/version.rb
100
+ - lib/roqua/core_ext/active_interaction/filters/date_time_as_unix_extension.rb
101
+ - lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
100
102
  - lib/roqua/core_ext/activerecord/uniq_find_or_create.rb
101
103
  - lib/roqua/core_ext/array/stable_sort_by.rb
102
104
  - lib/roqua/core_ext/enumerable/sort_by_alphanum.rb
@@ -111,6 +113,8 @@ files:
111
113
  - lib/roqua/support/logging.rb
112
114
  - lib/roqua/support/request_logger.rb
113
115
  - roqua-support.gemspec
116
+ - spec/roqua/core_ext/active_interaction/date_time_as_unix_extension_spec.rb
117
+ - spec/roqua/core_ext/active_interaction/duration_filter_spec.rb
114
118
  - spec/roqua/core_ext/activerecord/uniq_find_or_create_spec.rb
115
119
  - spec/roqua/core_ext/array/stable_sort_by_spec.rb
116
120
  - spec/roqua/core_ext/enumerable/sort_by_alphanum_spec.rb
@@ -143,11 +147,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
147
  version: '0'
144
148
  requirements: []
145
149
  rubyforge_project:
146
- rubygems_version: 2.2.2
150
+ rubygems_version: 2.3.0
147
151
  signing_key:
148
152
  specification_version: 4
149
153
  summary: Helper objects and proxies used by a lot of RoQua applications
150
154
  test_files:
155
+ - spec/roqua/core_ext/active_interaction/date_time_as_unix_extension_spec.rb
156
+ - spec/roqua/core_ext/active_interaction/duration_filter_spec.rb
151
157
  - spec/roqua/core_ext/activerecord/uniq_find_or_create_spec.rb
152
158
  - spec/roqua/core_ext/array/stable_sort_by_spec.rb
153
159
  - spec/roqua/core_ext/enumerable/sort_by_alphanum_spec.rb
@@ -160,3 +166,4 @@ test_files:
160
166
  - spec/roqua/support/request_logger_spec.rb
161
167
  - spec/roqua/support_spec.rb
162
168
  - spec/spec_helper.rb
169
+ has_rdoc: