time_splitter 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +7 -0
- data/Gemfile +3 -0
- data/LICENSE +20 -0
- data/README.md +53 -0
- data/Rakefile +2 -0
- data/lib/time_splitter/accessors.rb +66 -0
- data/lib/time_splitter/version.rb +3 -0
- data/lib/time_splitter.rb +5 -0
- data/spec/time_splitter/accessors_spec.rb +179 -0
- data/time_splitter.gemspec +22 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3ae7e334a287a8d946e911dba08ba928346391d9
|
4
|
+
data.tar.gz: 7639b9ea8eed95122d10ca476ae27115e132505e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4ccee3538dd7964342530d7e788b85bb9247d7fc6b9d5707528fb902487cc6cd3f77d0008ce552b88122dea458430288bf991f2e6ef053a1e292c3bf411278d7
|
7
|
+
data.tar.gz: 6ec58eb45efb7dbc021436cb7b02ac75cb6b02105934cdb5c99f7a9e4fd93faa8eea5e73e8139422260303ae3b757cc47ed384c34d3ac629fe21ff20be84ca7e
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Michi Huber
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
TimeSplitter
|
2
|
+
============
|
3
|
+
[![Build Status](https://travis-ci.org/shekibobo/time_splitter.png)](https://travis-ci.org/shekibobo/time_splitter)
|
4
|
+
|
5
|
+
Setting DateTimes can be a difficult or ugly thing, especially through a web form. Finding a good DatePicker or TimePicker is easy, but getting them to work on both can be difficult. TimeSplitter automatically generates accessors for `date`, `time`, `hour`, and `min` on any datetime or time attribute, making it trivial to use different form inputs to set different parts of a datetime field.
|
6
|
+
|
7
|
+
This gem is based on [SplitDatetime](https://github.com/michihuber/split_datetime) by [Michi Huber](https://github.com/michihuber). TimeSplitter improves on the gem, updating for Rails 4, adding `time` accessors, and providing a safer and more consistent default setting.
|
8
|
+
|
9
|
+
## Example Usage
|
10
|
+
In your `Gemfile`:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem "time_splitter"
|
14
|
+
```
|
15
|
+
|
16
|
+
After bundling, assuming you have an Event model with a starts_at attribute, add this to your model:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
class Event < ActiveRecord::Base
|
20
|
+
extend TimeSplitter::Accessors
|
21
|
+
split_accessor :starts_at
|
22
|
+
end
|
23
|
+
```
|
24
|
+
|
25
|
+
In your view:
|
26
|
+
|
27
|
+
```erb
|
28
|
+
<%= simple_form_for @event do |f| %>
|
29
|
+
<%= f.input :starts_at_date, as: :string, input_html: { class: 'datepicker' } %>
|
30
|
+
<%= f.input :starts_at_hour, collection: 0..24 %>
|
31
|
+
<%= f.input :starts_at_min, collection: [0, 15, 30, 45] %>
|
32
|
+
<%= f.input :starts_at_time, as: :time_select
|
33
|
+
<%= ... %>
|
34
|
+
<% end %>
|
35
|
+
```
|
36
|
+
|
37
|
+
Add your js datepicker and you're good to go. (Of course, this also works with standard Rails form helpers).
|
38
|
+
|
39
|
+
## Options
|
40
|
+
|
41
|
+
You can specify the date format for the view:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
split_accessor :starts_at, format: "%D"
|
45
|
+
```
|
46
|
+
|
47
|
+
See `Time#strftime` for formats. Default is `"%F"`.
|
48
|
+
|
49
|
+
You can specify multiple datetime fields to split:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
split_accessor :starts_at, :ends_at, :expires_at, format: "%D"
|
53
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
module TimeSplitter
|
2
|
+
module Accessors
|
3
|
+
def split_accessor(*attrs)
|
4
|
+
opts = { format: "%F" }.merge!(attrs.extract_options!)
|
5
|
+
|
6
|
+
attrs.each do |attr|
|
7
|
+
attr_accessible "#{attr}_date",
|
8
|
+
"#{attr}_time",
|
9
|
+
"#{attr}_hour",
|
10
|
+
"#{attr}_min" if defined?(ActiveModel::MassAssignmentSecurity)
|
11
|
+
|
12
|
+
# Maps the setter for #{attr}_time to accept multipart-parameters for Time
|
13
|
+
composed_of "#{attr}_time".to_sym, class_name: 'DateTime' if self.respond_to?(:composed_of)
|
14
|
+
|
15
|
+
# Default instance of the attribute, used if setting an element of the
|
16
|
+
# time attribute before the attribute was sent. Allows us to retrieve a
|
17
|
+
# default value for +#{attr}+ to modify without explicitely overriding
|
18
|
+
# the attr_reader. Defaults to a Time object with all fields set to 0.
|
19
|
+
define_method("#{attr}_or_new") do
|
20
|
+
self.send(attr) || Time.new(0)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Writers
|
24
|
+
|
25
|
+
define_method("#{attr}_date=") do |date|
|
26
|
+
return unless date.present?
|
27
|
+
date = Date.parse(date.to_s)
|
28
|
+
self.send("#{attr}=", self.send("#{attr}_or_new").change(year: date.year, month: date.month, day: date.day))
|
29
|
+
end
|
30
|
+
|
31
|
+
define_method("#{attr}_hour=") do |hour|
|
32
|
+
return unless hour.present?
|
33
|
+
self.send("#{attr}=", self.send("#{attr}_or_new").change(hour: hour, min: self.send("#{attr}_or_new").min))
|
34
|
+
end
|
35
|
+
|
36
|
+
define_method("#{attr}_min=") do |min|
|
37
|
+
return unless min.present?
|
38
|
+
self.send("#{attr}=", self.send("#{attr}_or_new").change(min: min))
|
39
|
+
end
|
40
|
+
|
41
|
+
define_method("#{attr}_time=") do |time|
|
42
|
+
return unless time.present?
|
43
|
+
time = Time.parse(time) unless time.is_a?(Date) || time.is_a?(Time)
|
44
|
+
self.send("#{attr}=", self.send("#{attr}_or_new").change(hour: time.hour, min: time.min))
|
45
|
+
end
|
46
|
+
|
47
|
+
# Readers
|
48
|
+
define_method("#{attr}_date") do
|
49
|
+
self.send(attr).try :strftime, opts[:format]
|
50
|
+
end
|
51
|
+
|
52
|
+
define_method("#{attr}_hour") do
|
53
|
+
self.send(attr).try :hour
|
54
|
+
end
|
55
|
+
|
56
|
+
define_method("#{attr}_min") do
|
57
|
+
self.send(attr).try :min
|
58
|
+
end
|
59
|
+
|
60
|
+
define_method("#{attr}_time") do
|
61
|
+
self.send(attr).try :to_time
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,179 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
require_relative "../../lib/time_splitter/accessors.rb"
|
3
|
+
|
4
|
+
describe TimeSplitter::Accessors do
|
5
|
+
let(:model) { Model.new }
|
6
|
+
before do
|
7
|
+
class ModelParent; attr_accessor :starts_at; end
|
8
|
+
class Model < ModelParent; attr_accessor :starts_at; end
|
9
|
+
Model.extend(TimeSplitter::Accessors)
|
10
|
+
Model.split_accessor(:starts_at)
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#starts_at" do
|
14
|
+
it 'does not override the default reader for the field' do
|
15
|
+
class Model; def starts_at; 5; end; end
|
16
|
+
expect(model.starts_at).to eq 5
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'correctly returns nil if not set' do
|
20
|
+
expect(model.starts_at).to eq nil
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "split datetime methods" do
|
25
|
+
context 'when #starts_at is nil' do
|
26
|
+
describe "#starts_at_date" do
|
27
|
+
it "returns nil" do
|
28
|
+
expect(model.starts_at_date).to be_nil
|
29
|
+
end
|
30
|
+
|
31
|
+
it "lets you modify the format" do
|
32
|
+
Model.split_accessor(:starts_at, format: "%D")
|
33
|
+
expect(model.starts_at_date).to be_nil
|
34
|
+
end
|
35
|
+
|
36
|
+
it "sets the appropiate parts of #starts_at" do
|
37
|
+
model.starts_at_date = Time.new(1111, 1, 1)
|
38
|
+
expect(model.starts_at).to eq Time.new(1111, 1, 1, 0, 0, 0)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "can set from a string" do
|
42
|
+
model.starts_at_date = "1111-01-01"
|
43
|
+
expect(model.starts_at).to eq Time.new(1111, 1, 1, 0, 0, 0)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "is nil if the string is empty" do
|
47
|
+
model.starts_at_date = ""
|
48
|
+
expect(model.starts_at).to be_nil
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "#starts_at_hour" do
|
53
|
+
it "returns nil" do
|
54
|
+
expect(model.starts_at_hour).to be_nil
|
55
|
+
end
|
56
|
+
|
57
|
+
it "sets the hour of starts_at" do
|
58
|
+
model.starts_at_hour = 11
|
59
|
+
expect(model.starts_at).to eq Time.new(0, 1, 1, 11, 0, 0)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "is nil if the string is empty" do
|
63
|
+
model.starts_at_hour = ""
|
64
|
+
expect(model.starts_at).to be_nil
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe "#starts_at_min" do
|
69
|
+
it "returns nil" do
|
70
|
+
expect(model.starts_at_min).to be_nil
|
71
|
+
end
|
72
|
+
|
73
|
+
it "sets the minute of #starts_at" do
|
74
|
+
model.starts_at_min = 55
|
75
|
+
expect(model.starts_at).to eq Time.new(0, 1, 1, 0, 55, 0)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "is nil if the string is empty" do
|
79
|
+
model.starts_at_min = ""
|
80
|
+
expect(model.starts_at).to be_nil
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe '#starts_at_time' do
|
85
|
+
it 'returns nil' do
|
86
|
+
expect(model.starts_at_time).to be_nil
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'sets the hour and minute of #starts_at' do
|
90
|
+
model.starts_at_time = '08:33'
|
91
|
+
expect(model.starts_at).to eq Time.new(0, 1, 1, 8, 33, 0)
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'is nil if the string is empty' do
|
95
|
+
model.starts_at_time = ''
|
96
|
+
expect(model.starts_at).to be_nil
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context 'when modifying #starts_at' do
|
102
|
+
before { model.starts_at = Time.new(2222, 12, 22, 13, 44, 0) }
|
103
|
+
|
104
|
+
describe "#starts_at_date" do
|
105
|
+
it "returns the model's starts_at date as string" do
|
106
|
+
expect(model.starts_at_date).to eq "2222-12-22"
|
107
|
+
end
|
108
|
+
|
109
|
+
it "lets you modify the format" do
|
110
|
+
Model.split_accessor(:starts_at, format: "%D")
|
111
|
+
expect(model.starts_at_date).to eq "12/22/22"
|
112
|
+
end
|
113
|
+
|
114
|
+
it "sets the appropiate parts of #starts_at" do
|
115
|
+
model.starts_at_date = Time.new(1111, 1, 1)
|
116
|
+
expect(model.starts_at).to eq Time.new(1111, 1, 1, 13, 44, 0)
|
117
|
+
end
|
118
|
+
|
119
|
+
it "can set from a string" do
|
120
|
+
model.starts_at_date = "1111-01-01"
|
121
|
+
expect(model.starts_at).to eq Time.new(1111, 1, 1, 13, 44, 0)
|
122
|
+
end
|
123
|
+
|
124
|
+
it "uses the default if the string is empty" do
|
125
|
+
model.starts_at_date = ""
|
126
|
+
expect(model.starts_at).to eq Time.new(2222, 12, 22, 13, 44, 0)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe "#starts_at_hour" do
|
131
|
+
it "returns the hour" do
|
132
|
+
expect(model.starts_at_hour).to eq 13
|
133
|
+
end
|
134
|
+
|
135
|
+
it "sets the hour of starts_at" do
|
136
|
+
model.starts_at_hour = 11
|
137
|
+
expect(model.starts_at).to eq Time.new(2222, 12, 22, 11, 44, 0)
|
138
|
+
end
|
139
|
+
|
140
|
+
it "uses the default if the string is empty" do
|
141
|
+
model.starts_at_hour = ""
|
142
|
+
expect(model.starts_at).to eq Time.new(2222, 12, 22, 13, 44, 0)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
describe "#starts_at_min" do
|
147
|
+
it "returns the min" do
|
148
|
+
expect(model.starts_at_min).to eq 44
|
149
|
+
end
|
150
|
+
|
151
|
+
it "sets the minute of #starts_at" do
|
152
|
+
model.starts_at_min = 55
|
153
|
+
expect(model.starts_at).to eq Time.new(2222, 12, 22, 13, 55, 0)
|
154
|
+
end
|
155
|
+
|
156
|
+
it "uses the default if the string is empty" do
|
157
|
+
model.starts_at_min = ""
|
158
|
+
expect(model.starts_at).to eq Time.new(2222, 12, 22, 13, 44, 0)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
describe '#starts_at_time' do
|
163
|
+
it 'returns the time' do
|
164
|
+
expect(model.starts_at_time).to eq Time.new(2222, 12, 22, 13, 44, 0)
|
165
|
+
end
|
166
|
+
|
167
|
+
it 'sets the hour and minute of #starts_at' do
|
168
|
+
model.starts_at_time = '08:33'
|
169
|
+
expect(model.starts_at).to eq Time.new(2222, 12, 22, 8, 33, 0)
|
170
|
+
end
|
171
|
+
|
172
|
+
it 'uses the default if the string is empty' do
|
173
|
+
model.starts_at_time = ''
|
174
|
+
expect(model.starts_at).to eq Time.new(2222, 12, 22, 13, 44, 0)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require 'time_splitter/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "time_splitter"
|
6
|
+
s.version = TimeSplitter::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["Michi Huber", "Joshua Kovach"]
|
9
|
+
s.email = ["michi.huber@gmail.com", "kovach.jc@gmail.com"]
|
10
|
+
s.homepage = "http://github.com/shekibbo/time_splitter"
|
11
|
+
s.description = %q{Add split accessors for Date/Time/DateTime objects to easily set the date, time, hour, or minute.}
|
12
|
+
s.summary = %q{Use `datetime_accessor :starts_at` to provide `starts_at_date`, `starts_at_time`, `starts_at_hour`, `starts_at_minute` accessors on the model.}
|
13
|
+
s.license = "MIT"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- spec/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_development_dependency 'activesupport'
|
21
|
+
s.add_development_dependency 'rspec', '~> 2.11'
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: time_splitter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michi Huber
|
8
|
+
- Joshua Kovach
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-09-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - '>='
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - '>='
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rspec
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '2.11'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ~>
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '2.11'
|
42
|
+
description: Add split accessors for Date/Time/DateTime objects to easily set the
|
43
|
+
date, time, hour, or minute.
|
44
|
+
email:
|
45
|
+
- michi.huber@gmail.com
|
46
|
+
- kovach.jc@gmail.com
|
47
|
+
executables: []
|
48
|
+
extensions: []
|
49
|
+
extra_rdoc_files: []
|
50
|
+
files:
|
51
|
+
- .travis.yml
|
52
|
+
- Gemfile
|
53
|
+
- LICENSE
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- lib/time_splitter.rb
|
57
|
+
- lib/time_splitter/accessors.rb
|
58
|
+
- lib/time_splitter/version.rb
|
59
|
+
- spec/time_splitter/accessors_spec.rb
|
60
|
+
- time_splitter.gemspec
|
61
|
+
homepage: http://github.com/shekibbo/time_splitter
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
metadata: {}
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 2.1.5
|
82
|
+
signing_key:
|
83
|
+
specification_version: 4
|
84
|
+
summary: Use `datetime_accessor :starts_at` to provide `starts_at_date`, `starts_at_time`,
|
85
|
+
`starts_at_hour`, `starts_at_minute` accessors on the model.
|
86
|
+
test_files:
|
87
|
+
- spec/time_splitter/accessors_spec.rb
|