split_date_time 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +46 -0
- data/Rakefile +7 -0
- data/lib/split_date_time.rb +22 -0
- data/lib/split_date_time/splitter.rb +79 -0
- data/lib/split_date_time/version.rb +3 -0
- data/spec/naming_spec.rb +27 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/splitter_spec.rb +76 -0
- data/split_date_time.gemspec +26 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1b98af975b2451703f230cfda4ff78ea51455d99
|
4
|
+
data.tar.gz: 7b15252e19e89b343ac1a5f09d977dd23939d6f5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 71acfe3c7a6c0a50a90b19f04887c3bf39482a9d87ca6d97d66a15eca4ee61d7b05ba5663e1aa1f307963a2146188af1e4051ea9b821ae9b9589e7a2294c109c
|
7
|
+
data.tar.gz: 5484e651b94831ff1a3db38c20db703ea6594cb66d498c8fb4b2cfa9ff89d60a192e1e42f5abd0fc59cc4a8ecbcbba150fd1193895baa4175fe172d93c0cb8ef
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Chuck Callebs
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# SplitDateTime
|
2
|
+
|
3
|
+
A simple gem that allows you to split a DateTime field into a Date and a Time field for separate processing.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'split_date_time'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install split_date_time
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
In your ActiveRecord model:
|
22
|
+
|
23
|
+
```
|
24
|
+
split_date_time field: 'created_at', prefix: 'created'
|
25
|
+
```
|
26
|
+
**field** (required): The table column you'd like to split.
|
27
|
+
|
28
|
+
**prefix** (optional): A custom prefix to use.
|
29
|
+
|
30
|
+
The above would generate the following methods:
|
31
|
+
|
32
|
+
- `created_date`
|
33
|
+
- `created_date=`
|
34
|
+
- `created_time`
|
35
|
+
- `created_time=`
|
36
|
+
|
37
|
+
You'll then be able to use these as you would any other ActiveRecord attribute. The fields will be concatenated and stored as a DateTime when you save the model.
|
38
|
+
|
39
|
+
|
40
|
+
## Contributing
|
41
|
+
|
42
|
+
1. Fork it
|
43
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
44
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
45
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
46
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'split_date_time/splitter'
|
2
|
+
|
3
|
+
module SplitDateTime
|
4
|
+
if defined? Rails::Railtie
|
5
|
+
require 'rails'
|
6
|
+
class Railtie < Rails::Railtie
|
7
|
+
initializer 'split_date_time.insert_into_active_record' do
|
8
|
+
ActiveSupport.on_load :active_record do
|
9
|
+
SplitDateTime::Railtie.insert
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Railtie
|
16
|
+
def self.insert
|
17
|
+
if defined?(ActiveRecord)
|
18
|
+
ActiveRecord::Base.send(:include, SplitDateTime::Splitter)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_support/all'
|
3
|
+
# require 'version'
|
4
|
+
|
5
|
+
module SplitDateTime
|
6
|
+
module Splitter
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
class Naming
|
10
|
+
def self.time_getter(field, prefix = nil)
|
11
|
+
"#{prefix_or_field(field, prefix)}_time"
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.time_setter(field, prefix = nil)
|
15
|
+
"#{prefix_or_field(field, prefix)}_time="
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.date_getter(field, prefix = nil)
|
19
|
+
"#{prefix_or_field(field, prefix)}_date"
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.date_setter(field, prefix = nil)
|
23
|
+
"#{prefix_or_field(field, prefix)}_date="
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.prefix_or_field(field, prefix = nil)
|
27
|
+
prefix.present? ? "#{prefix}" : "#{field}"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
included do
|
32
|
+
extend ClassMethods
|
33
|
+
end
|
34
|
+
|
35
|
+
module ClassMethods
|
36
|
+
def split_date_time(field, options = {})
|
37
|
+
define_split_accessors(field, options[:prefix])
|
38
|
+
define_concatenation_callback(field, options[:prefix])
|
39
|
+
end
|
40
|
+
|
41
|
+
def define_split_accessors(field, prefix = nil)
|
42
|
+
define_method Naming.time_getter(field, prefix) do
|
43
|
+
field_val = send(field)
|
44
|
+
field_val.strftime('%H:%M') if field_val.present?
|
45
|
+
end
|
46
|
+
define_method Naming.time_setter(field, prefix) do |val|
|
47
|
+
instance_variable_set :"@#{Naming.time_getter(field, prefix)}", val
|
48
|
+
end
|
49
|
+
define_method Naming.date_getter(field, prefix) do
|
50
|
+
field_val = send(field)
|
51
|
+
field_val.strftime('%m/%d/%Y') if field_val.present?
|
52
|
+
end
|
53
|
+
define_method Naming.date_setter(field, prefix) do |val|
|
54
|
+
instance_variable_set :"@#{Naming.date_getter(field, prefix)}", val
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def define_concatenation_callback(field, prefix)
|
59
|
+
if self.kind_of? ActiveRecord::Base
|
60
|
+
before_validation :"concatenate_#{field}", if: :"#{field}_modified?"
|
61
|
+
end
|
62
|
+
|
63
|
+
define_method :"concatenate_#{field}" do
|
64
|
+
time_variable = instance_variable_get(:"@#{Naming.time_getter(field, prefix)}")
|
65
|
+
time_variable ||= send("#{Naming.time_getter(field, prefix)}")
|
66
|
+
date_variable = instance_variable_get(:"@#{Naming.date_getter(field, prefix)}")
|
67
|
+
date_variable ||= send("#{Naming.date_getter(field, prefix)}")
|
68
|
+
self.send "#{field}=", DateTime.parse("#{date_variable} #{time_variable}")
|
69
|
+
end
|
70
|
+
|
71
|
+
define_method :"#{field}_modified?" do
|
72
|
+
time_variable = instance_variable_get(:"@#{Naming.time_getter(field, prefix)}")
|
73
|
+
date_variable = instance_variable_get(:"@#{Naming.date_getter(field, prefix)}")
|
74
|
+
time_variable.present? || date_variable.present?
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
data/spec/naming_spec.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SplitDateTime::Splitter::Naming do
|
4
|
+
it 'should use prefix if one is provided' do
|
5
|
+
SplitDateTime::Splitter::Naming.prefix_or_field(:test_field, :test).should eq 'test'
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should use field name if no prefix is provided' do
|
9
|
+
SplitDateTime::Splitter::Naming.prefix_or_field(:test_field).should eq 'test_field'
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should correctly format date getter' do
|
13
|
+
SplitDateTime::Splitter::Naming.date_getter(:test_field).should eq 'test_field_date'
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should correctly format time getter' do
|
17
|
+
SplitDateTime::Splitter::Naming.time_getter(:test_field).should eq 'test_field_time'
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should correctly format date setter' do
|
21
|
+
SplitDateTime::Splitter::Naming.date_setter(:test_field).should eq 'test_field_date='
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should correctly format time setter' do
|
25
|
+
SplitDateTime::Splitter::Naming.time_setter(:test_field).should eq 'test_field_time='
|
26
|
+
end
|
27
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require_relative '../lib/split_date_time/splitter'
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SplitDateTime::Splitter do
|
4
|
+
|
5
|
+
class TestClass
|
6
|
+
include SplitDateTime::Splitter
|
7
|
+
|
8
|
+
attr_accessor :test_field_1, :test_field_2
|
9
|
+
|
10
|
+
split_date_time :test_field_1
|
11
|
+
split_date_time :test_field_2, prefix: :other
|
12
|
+
end
|
13
|
+
|
14
|
+
context '#initialization' do
|
15
|
+
let(:subject) { TestClass.new }
|
16
|
+
|
17
|
+
it 'should create time accessors when initialized' do
|
18
|
+
subject.should respond_to(:test_field_1_time)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should create date accessors when initialized' do
|
22
|
+
subject.should respond_to(:test_field_1_date)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should create callback method when initialized' do
|
26
|
+
subject.should respond_to(:concatenate_test_field_1)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context '#accessors' do
|
31
|
+
let(:subject) { TestClass.new }
|
32
|
+
|
33
|
+
it 'should extract a DateTime date' do
|
34
|
+
subject.test_field_1 = DateTime.parse("03/03/2014 01:15:24 AM")
|
35
|
+
subject.test_field_1_date.should eq "03/03/2014"
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should extract a DateTime time' do
|
39
|
+
subject.test_field_1 = DateTime.parse("03/03/2014 01:15:24 AM")
|
40
|
+
subject.test_field_1_time.should eq "01:15"
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should add specific prefix when provided' do
|
44
|
+
subject.should respond_to(:other_date)
|
45
|
+
subject.should respond_to(:other_time)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context '#callbacks' do
|
50
|
+
let(:subject) { TestClass.new }
|
51
|
+
|
52
|
+
it 'should concatenate date/time together' do
|
53
|
+
subject.test_field_1_date = '03/03/2014'
|
54
|
+
subject.test_field_1_time = '01:15'
|
55
|
+
subject.concatenate_test_field_1.should eq "Mon, 03 Mar 2014 01:15:00 +0000"
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should concatenate date/time together when only one was touched' do
|
59
|
+
subject.test_field_1 = DateTime.parse("03/03/2014 01:15:24 AM")
|
60
|
+
subject.test_field_1_date = '05/05/2014'
|
61
|
+
subject.concatenate_test_field_1.should eq "Mon, 05 May 2014 01:15:00 +0000"
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'should show field as modified if both date and time are touched' do
|
65
|
+
subject.test_field_1_date = '03/03/2014'
|
66
|
+
subject.test_field_1_time = '01:15'
|
67
|
+
subject.test_field_1_modified?.should be_true
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should show field as modified if only date or time were touched' do
|
71
|
+
subject.test_field_1 = DateTime.parse("03/03/2014 01:15:24 AM")
|
72
|
+
subject.test_field_1_date = '05/05/2014'
|
73
|
+
subject.test_field_1_modified?.should be_true
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'split_date_time/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "split_date_time"
|
8
|
+
spec.version = SplitDateTime::VERSION
|
9
|
+
spec.authors = ["Chuck Callebs"]
|
10
|
+
spec.email = ["cacallebs@gmail.com"]
|
11
|
+
spec.description = %q{Allows DateTime fields to be split into a Date field and a Time field.}
|
12
|
+
spec.summary = %q{Allows DateTime fields to be split into a Date field and a Time field.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "activerecord"
|
25
|
+
spec.add_development_dependency "active_support"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: split_date_time
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chuck Callebs
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activerecord
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: active_support
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Allows DateTime fields to be split into a Date field and a Time field.
|
84
|
+
email:
|
85
|
+
- cacallebs@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- lib/split_date_time.rb
|
96
|
+
- lib/split_date_time/splitter.rb
|
97
|
+
- lib/split_date_time/version.rb
|
98
|
+
- spec/naming_spec.rb
|
99
|
+
- spec/spec_helper.rb
|
100
|
+
- spec/splitter_spec.rb
|
101
|
+
- split_date_time.gemspec
|
102
|
+
homepage: ''
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.2.1
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: Allows DateTime fields to be split into a Date field and a Time field.
|
126
|
+
test_files:
|
127
|
+
- spec/naming_spec.rb
|
128
|
+
- spec/spec_helper.rb
|
129
|
+
- spec/splitter_spec.rb
|