time_splitter 0.5.0 → 1.0.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 +16 -5
- data/lib/time_splitter/accessors.rb +5 -3
- data/lib/time_splitter/version.rb +1 -1
- data/spec/time_splitter/accessors_spec.rb +15 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e95cb957e2b377227f2267ded2a018b24ec88aa9
|
4
|
+
data.tar.gz: 8a56421b51019ea0a86a494f071b47e4d953c8f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2846d29799635f3d33faaa61678c6e8fb04034a7b0e7f0cddaa896da92cbf6f4ddf9e79bbed2f7200ac7ae6f87dffb4736974439c5e8152096f778df8f001dd9
|
7
|
+
data.tar.gz: 12ebd84275f87d033f08c1c78d36b64d6e92938db113deeb2e6bc01499f718445b22b9b3239123587d46572af101a23105218c21b3e466e3b747f3a8ae471ba1
|
data/README.md
CHANGED
@@ -38,7 +38,7 @@ In your view:
|
|
38
38
|
<%= f.input :starts_at_date, as: :string, input_html: { class: 'datepicker' } %>
|
39
39
|
<%= f.input :starts_at_hour, collection: 0..24 %>
|
40
40
|
<%= f.input :starts_at_min, collection: [0, 15, 30, 45] %>
|
41
|
-
<%= f.input :starts_at_time, as: :
|
41
|
+
<%= f.input :starts_at_time, as: :time
|
42
42
|
<%= ... %>
|
43
43
|
<% end %>
|
44
44
|
```
|
@@ -49,13 +49,24 @@ If you are using Rails < 4.0 and/or are not using StrongParameters, you must add
|
|
49
49
|
|
50
50
|
## Options
|
51
51
|
|
52
|
-
|
52
|
+
By default, the read accessors provided by TimeSplitter are as follows:
|
53
|
+
```ruby
|
54
|
+
starts_at_date #=> Date
|
55
|
+
starts_at_time #=> Time or Timey class used in :default option
|
56
|
+
starts_at_hour #=> Fixnum
|
57
|
+
starts_at_min #=> Fixnum
|
58
|
+
```
|
59
|
+
|
60
|
+
You can override the default read format for date and time if you so choose, though doing so may not work well with certain form input types.
|
53
61
|
|
54
62
|
```ruby
|
55
|
-
split_accessor :starts_at,
|
63
|
+
split_accessor :starts_at, date_format: "%D", time_format: "%I:%M%p"
|
64
|
+
|
65
|
+
starts_at_date #=> String "2013-10-13"
|
66
|
+
starts_at_time #=> String "01:44PM"
|
56
67
|
```
|
57
68
|
|
58
|
-
See `Time#strftime` for formats.
|
69
|
+
See `Time#strftime` for formats.
|
59
70
|
|
60
71
|
You can specify multiple datetime fields to split:
|
61
72
|
|
@@ -70,7 +81,7 @@ split_accessor :starts_at, default: -> { DateTime.current }
|
|
70
81
|
|
71
82
|
# model = Model.new(starts_at_time: '09:00')
|
72
83
|
# model.starts_at
|
73
|
-
# => Thu, 10 Oct 2013 09:00:00 -0400
|
84
|
+
# => Thu, 10 Oct 2013 09:00:00 -0400 # DateTime
|
74
85
|
```
|
75
86
|
|
76
87
|
The default time object is `Time.new(0, 1, 1, 0, 0, 0, '+00:00')`.
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module TimeSplitter
|
2
2
|
module Accessors
|
3
3
|
def split_accessor(*attrs)
|
4
|
-
options =
|
4
|
+
options = 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
|
@@ -41,7 +41,8 @@ module TimeSplitter
|
|
41
41
|
|
42
42
|
# Readers
|
43
43
|
define_method("#{attr}_date") do
|
44
|
-
self.send(attr).try :
|
44
|
+
date = self.send(attr).try :to_date
|
45
|
+
date && options[:date_format] ? date.strftime(options[:date_format]) : date
|
45
46
|
end
|
46
47
|
|
47
48
|
define_method("#{attr}_hour") do
|
@@ -53,7 +54,8 @@ module TimeSplitter
|
|
53
54
|
end
|
54
55
|
|
55
56
|
define_method("#{attr}_time") do
|
56
|
-
self.send(attr)
|
57
|
+
time = self.send(attr)
|
58
|
+
time && options[:time_format] ? time.strftime(options[:time_format]) : time
|
57
59
|
end
|
58
60
|
end
|
59
61
|
end
|
@@ -29,7 +29,7 @@ describe TimeSplitter::Accessors do
|
|
29
29
|
it 'sets the date on the new default' do
|
30
30
|
model.starts_at_date = '2222-5-6'
|
31
31
|
expect(model.starts_at).to eq DateTime.new(2222, 5, 6, 4, 5, 0, '+7')
|
32
|
-
expect(model.starts_at_date).to eq
|
32
|
+
expect(model.starts_at_date).to eq Date.new(2222, 5, 6)
|
33
33
|
end
|
34
34
|
|
35
35
|
it 'sets the hour on the new default' do
|
@@ -57,7 +57,7 @@ describe TimeSplitter::Accessors do
|
|
57
57
|
end
|
58
58
|
|
59
59
|
it "lets you modify the format" do
|
60
|
-
Model.split_accessor(:starts_at,
|
60
|
+
Model.split_accessor(:starts_at, date_format: "%D")
|
61
61
|
expect(model.starts_at_date).to be_nil
|
62
62
|
end
|
63
63
|
|
@@ -114,6 +114,11 @@ describe TimeSplitter::Accessors do
|
|
114
114
|
expect(model.starts_at_time).to be_nil
|
115
115
|
end
|
116
116
|
|
117
|
+
it "lets you modify the time format" do
|
118
|
+
Model.split_accessor(:starts_at, time_format: "%I:%M%p")
|
119
|
+
expect(model.starts_at_time).to be_nil
|
120
|
+
end
|
121
|
+
|
117
122
|
it 'sets the hour and minute of #starts_at' do
|
118
123
|
model.starts_at_time = '08:33'
|
119
124
|
expect(model.starts_at).to eq Time.new(0, 1, 1, 8, 33, 0, '+00:00')
|
@@ -130,12 +135,12 @@ describe TimeSplitter::Accessors do
|
|
130
135
|
before { model.starts_at = Time.new(2222, 12, 22, 13, 44, 0, '+00:00') }
|
131
136
|
|
132
137
|
describe "#starts_at_date" do
|
133
|
-
it "returns the model's starts_at date
|
134
|
-
expect(model.starts_at_date).to eq
|
138
|
+
it "returns the model's starts_at date" do
|
139
|
+
expect(model.starts_at_date).to eq Date.new(2222, 12, 22)
|
135
140
|
end
|
136
141
|
|
137
142
|
it "lets you modify the format" do
|
138
|
-
Model.split_accessor(:starts_at,
|
143
|
+
Model.split_accessor(:starts_at, date_format: "%D")
|
139
144
|
expect(model.starts_at_date).to eq "12/22/22"
|
140
145
|
end
|
141
146
|
|
@@ -192,6 +197,11 @@ describe TimeSplitter::Accessors do
|
|
192
197
|
expect(model.starts_at_time).to eq Time.new(2222, 12, 22, 13, 44, 0, '+00:00')
|
193
198
|
end
|
194
199
|
|
200
|
+
it "lets you modify the time format" do
|
201
|
+
Model.split_accessor(:starts_at, time_format: "%I:%M%p")
|
202
|
+
expect(model.starts_at_time).to eq "01:44PM"
|
203
|
+
end
|
204
|
+
|
195
205
|
it 'sets the hour and minute of #starts_at' do
|
196
206
|
model.starts_at_time = '08:33'
|
197
207
|
expect(model.starts_at).to eq Time.new(2222, 12, 22, 8, 33, 0, '+00:00')
|
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: 1.0.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-10-
|
12
|
+
date: 2013-10-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|