time_splitter 0.3.1 → 0.5.0
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 +4 -4
- data/README.md +15 -0
- data/lib/time_splitter/accessors.rb +4 -4
- data/lib/time_splitter/version.rb +1 -1
- data/spec/time_splitter/accessors_spec.rb +44 -16
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83c4a402a05a74a22687e44114460cf340c73eb8
|
4
|
+
data.tar.gz: 2abde10a75986d39028fa19cc95f4ad6db88a273
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa52cdbe485b91bfd9f4224d25fb6fbecdd780ddd6cdf317234a03a552646a0e351b1b49897282f28f81e229156ed9ca6b0ecac79b1d2c936764debc836860a6
|
7
|
+
data.tar.gz: 3742fd82de7c316b7f5eaf792798376a92b03753a4ae2392230c2e442629b58dd5786f490e0675d4c3ec5cac72cbcc2419c91f2265c4e3d2b5deac4ce30fce66
|
data/README.md
CHANGED
@@ -62,3 +62,18 @@ You can specify multiple datetime fields to split:
|
|
62
62
|
```ruby
|
63
63
|
split_accessor :starts_at, :ends_at, :expires_at, format: "%D"
|
64
64
|
```
|
65
|
+
|
66
|
+
You can specify a default timey object to write. If `starts_at` is `nil`, which it would be at the time of a `new` or `create` call, TimeSplitter will use the default value as the basepoint for modification.
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
split_accessor :starts_at, default: -> { DateTime.current }
|
70
|
+
|
71
|
+
# model = Model.new(starts_at_time: '09:00')
|
72
|
+
# model.starts_at
|
73
|
+
# => Thu, 10 Oct 2013 09:00:00 -0400
|
74
|
+
```
|
75
|
+
|
76
|
+
The default time object is `Time.new(0, 1, 1, 0, 0, 0, '+00:00')`.
|
77
|
+
|
78
|
+
Note that TimeSplitter does not handle seconds at this time, and from testing it appears they are set to zero when modifying them.
|
79
|
+
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module TimeSplitter
|
2
2
|
module Accessors
|
3
3
|
def split_accessor(*attrs)
|
4
|
-
|
4
|
+
options = { format: "%F" }.merge!(attrs.extract_options!)
|
5
5
|
|
6
6
|
attrs.each do |attr|
|
7
7
|
# Maps the setter for #{attr}_time to accept multipart-parameters for Time
|
@@ -12,7 +12,7 @@ module TimeSplitter
|
|
12
12
|
# default value for +#{attr}+ to modify without explicitely overriding
|
13
13
|
# the attr_reader. Defaults to a Time object with all fields set to 0.
|
14
14
|
define_method("#{attr}_or_new") do
|
15
|
-
self.send(attr) || Time.new(0)
|
15
|
+
self.send(attr) || options.fetch(:default, ->{ Time.new(0, 1, 1, 0, 0, 0, "+00:00") }).call
|
16
16
|
end
|
17
17
|
|
18
18
|
# Writers
|
@@ -41,7 +41,7 @@ module TimeSplitter
|
|
41
41
|
|
42
42
|
# Readers
|
43
43
|
define_method("#{attr}_date") do
|
44
|
-
self.send(attr).try :strftime,
|
44
|
+
self.send(attr).try :strftime, options[:format]
|
45
45
|
end
|
46
46
|
|
47
47
|
define_method("#{attr}_hour") do
|
@@ -53,7 +53,7 @@ module TimeSplitter
|
|
53
53
|
end
|
54
54
|
|
55
55
|
define_method("#{attr}_time") do
|
56
|
-
self.send(attr)
|
56
|
+
self.send(attr)
|
57
57
|
end
|
58
58
|
end
|
59
59
|
end
|
@@ -23,6 +23,34 @@ describe TimeSplitter::Accessors do
|
|
23
23
|
|
24
24
|
describe "split datetime methods" do
|
25
25
|
context 'when #starts_at is nil' do
|
26
|
+
describe 'overriding the default time' do
|
27
|
+
before { Model.split_accessor :starts_at, default: ->{ DateTime.new(1111, 2, 3, 4, 5, 0, '+7') } }
|
28
|
+
|
29
|
+
it 'sets the date on the new default' do
|
30
|
+
model.starts_at_date = '2222-5-6'
|
31
|
+
expect(model.starts_at).to eq DateTime.new(2222, 5, 6, 4, 5, 0, '+7')
|
32
|
+
expect(model.starts_at_date).to eq '2222-05-06'
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'sets the hour on the new default' do
|
36
|
+
model.starts_at_hour = 9
|
37
|
+
expect(model.starts_at).to eq DateTime.new(1111, 2, 3, 9, 5, 0, '+7')
|
38
|
+
expect(model.starts_at_hour).to eq 9
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'sets the minute on the new default' do
|
42
|
+
model.starts_at_min = 20
|
43
|
+
expect(model.starts_at).to eq DateTime.new(1111, 2, 3, 4, 20, 0, '+7')
|
44
|
+
expect(model.starts_at_min).to eq 20
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'sets the time on the new default' do
|
48
|
+
model.starts_at_time = '09:22'
|
49
|
+
expect(model.starts_at).to eq DateTime.new(1111, 2, 3, 9, 22, 0, '+7')
|
50
|
+
expect(model.starts_at_time).to eq DateTime.new(1111, 2, 3, 9, 22, 0, '+7')
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
26
54
|
describe "#starts_at_date" do
|
27
55
|
it "returns nil" do
|
28
56
|
expect(model.starts_at_date).to be_nil
|
@@ -35,12 +63,12 @@ describe TimeSplitter::Accessors do
|
|
35
63
|
|
36
64
|
it "sets the appropiate parts of #starts_at" do
|
37
65
|
model.starts_at_date = Time.new(1111, 1, 1)
|
38
|
-
expect(model.starts_at).to eq Time.new(1111, 1, 1, 0, 0, 0)
|
66
|
+
expect(model.starts_at).to eq Time.new(1111, 1, 1, 0, 0, 0, '+00:00')
|
39
67
|
end
|
40
68
|
|
41
69
|
it "can set from a string" do
|
42
70
|
model.starts_at_date = "1111-01-01"
|
43
|
-
expect(model.starts_at).to eq Time.new(1111, 1, 1, 0, 0, 0)
|
71
|
+
expect(model.starts_at).to eq Time.new(1111, 1, 1, 0, 0, 0, '+00:00')
|
44
72
|
end
|
45
73
|
|
46
74
|
it "is nil if the string is empty" do
|
@@ -56,7 +84,7 @@ describe TimeSplitter::Accessors do
|
|
56
84
|
|
57
85
|
it "sets the hour of starts_at" do
|
58
86
|
model.starts_at_hour = 11
|
59
|
-
expect(model.starts_at).to eq Time.new(0, 1, 1, 11, 0, 0)
|
87
|
+
expect(model.starts_at).to eq Time.new(0, 1, 1, 11, 0, 0, '+00:00')
|
60
88
|
end
|
61
89
|
|
62
90
|
it "is nil if the string is empty" do
|
@@ -72,7 +100,7 @@ describe TimeSplitter::Accessors do
|
|
72
100
|
|
73
101
|
it "sets the minute of #starts_at" do
|
74
102
|
model.starts_at_min = 55
|
75
|
-
expect(model.starts_at).to eq Time.new(0, 1, 1, 0, 55, 0)
|
103
|
+
expect(model.starts_at).to eq Time.new(0, 1, 1, 0, 55, 0, '+00:00')
|
76
104
|
end
|
77
105
|
|
78
106
|
it "is nil if the string is empty" do
|
@@ -88,7 +116,7 @@ describe TimeSplitter::Accessors do
|
|
88
116
|
|
89
117
|
it 'sets the hour and minute of #starts_at' do
|
90
118
|
model.starts_at_time = '08:33'
|
91
|
-
expect(model.starts_at).to eq Time.new(0, 1, 1, 8, 33, 0)
|
119
|
+
expect(model.starts_at).to eq Time.new(0, 1, 1, 8, 33, 0, '+00:00')
|
92
120
|
end
|
93
121
|
|
94
122
|
it 'is nil if the string is empty' do
|
@@ -99,7 +127,7 @@ describe TimeSplitter::Accessors do
|
|
99
127
|
end
|
100
128
|
|
101
129
|
context 'when modifying #starts_at' do
|
102
|
-
before { model.starts_at = Time.new(2222, 12, 22, 13, 44, 0) }
|
130
|
+
before { model.starts_at = Time.new(2222, 12, 22, 13, 44, 0, '+00:00') }
|
103
131
|
|
104
132
|
describe "#starts_at_date" do
|
105
133
|
it "returns the model's starts_at date as string" do
|
@@ -113,17 +141,17 @@ describe TimeSplitter::Accessors do
|
|
113
141
|
|
114
142
|
it "sets the appropiate parts of #starts_at" do
|
115
143
|
model.starts_at_date = Time.new(1111, 1, 1)
|
116
|
-
expect(model.starts_at).to eq Time.new(1111, 1, 1, 13, 44, 0)
|
144
|
+
expect(model.starts_at).to eq Time.new(1111, 1, 1, 13, 44, 0, '+00:00')
|
117
145
|
end
|
118
146
|
|
119
147
|
it "can set from a string" do
|
120
148
|
model.starts_at_date = "1111-01-01"
|
121
|
-
expect(model.starts_at).to eq Time.new(1111, 1, 1, 13, 44, 0)
|
149
|
+
expect(model.starts_at).to eq Time.new(1111, 1, 1, 13, 44, 0, '+00:00')
|
122
150
|
end
|
123
151
|
|
124
152
|
it "uses the default if the string is empty" do
|
125
153
|
model.starts_at_date = ""
|
126
|
-
expect(model.starts_at).to eq Time.new(2222, 12, 22, 13, 44, 0)
|
154
|
+
expect(model.starts_at).to eq Time.new(2222, 12, 22, 13, 44, 0, '+00:00')
|
127
155
|
end
|
128
156
|
end
|
129
157
|
|
@@ -134,12 +162,12 @@ describe TimeSplitter::Accessors do
|
|
134
162
|
|
135
163
|
it "sets the hour of starts_at" do
|
136
164
|
model.starts_at_hour = 11
|
137
|
-
expect(model.starts_at).to eq Time.new(2222, 12, 22, 11, 44, 0)
|
165
|
+
expect(model.starts_at).to eq Time.new(2222, 12, 22, 11, 44, 0, '+00:00')
|
138
166
|
end
|
139
167
|
|
140
168
|
it "uses the default if the string is empty" do
|
141
169
|
model.starts_at_hour = ""
|
142
|
-
expect(model.starts_at).to eq Time.new(2222, 12, 22, 13, 44, 0)
|
170
|
+
expect(model.starts_at).to eq Time.new(2222, 12, 22, 13, 44, 0, '+00:00')
|
143
171
|
end
|
144
172
|
end
|
145
173
|
|
@@ -150,28 +178,28 @@ describe TimeSplitter::Accessors do
|
|
150
178
|
|
151
179
|
it "sets the minute of #starts_at" do
|
152
180
|
model.starts_at_min = 55
|
153
|
-
expect(model.starts_at).to eq Time.new(2222, 12, 22, 13, 55, 0)
|
181
|
+
expect(model.starts_at).to eq Time.new(2222, 12, 22, 13, 55, 0, '+00:00')
|
154
182
|
end
|
155
183
|
|
156
184
|
it "uses the default if the string is empty" do
|
157
185
|
model.starts_at_min = ""
|
158
|
-
expect(model.starts_at).to eq Time.new(2222, 12, 22, 13, 44, 0)
|
186
|
+
expect(model.starts_at).to eq Time.new(2222, 12, 22, 13, 44, 0, '+00:00')
|
159
187
|
end
|
160
188
|
end
|
161
189
|
|
162
190
|
describe '#starts_at_time' do
|
163
191
|
it 'returns the time' do
|
164
|
-
expect(model.starts_at_time).to eq Time.new(2222, 12, 22, 13, 44, 0)
|
192
|
+
expect(model.starts_at_time).to eq Time.new(2222, 12, 22, 13, 44, 0, '+00:00')
|
165
193
|
end
|
166
194
|
|
167
195
|
it 'sets the hour and minute of #starts_at' do
|
168
196
|
model.starts_at_time = '08:33'
|
169
|
-
expect(model.starts_at).to eq Time.new(2222, 12, 22, 8, 33, 0)
|
197
|
+
expect(model.starts_at).to eq Time.new(2222, 12, 22, 8, 33, 0, '+00:00')
|
170
198
|
end
|
171
199
|
|
172
200
|
it 'uses the default if the string is empty' do
|
173
201
|
model.starts_at_time = ''
|
174
|
-
expect(model.starts_at).to eq Time.new(2222, 12, 22, 13, 44, 0)
|
202
|
+
expect(model.starts_at).to eq Time.new(2222, 12, 22, 13, 44, 0, '+00:00')
|
175
203
|
end
|
176
204
|
end
|
177
205
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time_splitter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michi Huber
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-10-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -79,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
79
|
version: '0'
|
80
80
|
requirements: []
|
81
81
|
rubyforge_project:
|
82
|
-
rubygems_version: 2.1.
|
82
|
+
rubygems_version: 2.1.7
|
83
83
|
signing_key:
|
84
84
|
specification_version: 4
|
85
85
|
summary: Use `datetime_accessor :starts_at` to provide `starts_at_date`, `starts_at_time`,
|