date_time_attribute 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.gitignore +1 -0
- data/Gemfile +4 -3
- data/README.md +1 -1
- data/date_time_attribute.gemspec +2 -1
- data/lib/date_time_attribute.rb +7 -4
- data/spec/active_record_spec.rb +103 -0
- data/spec/date_time_attribute/container_spec.rb +7 -7
- data/spec/date_time_attribute_spec.rb +16 -16
- data/spec/spec_helper.rb +16 -5
- metadata +16 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MDJjOGJhZWE1ODQ2ODZiNGU3NjNjODAzYzE0YzllNjU5Zjc5Y2U5Mw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MTBkZTY3NDE2NDkzNzBiMDc3YWExNGU0ZjQ4NDRjMDA2YmY5NWYxMg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YzMwMWM5NWI3MmY4YzA3NmM3Y2EwNTRkMGZiZDc1ZjY1OTVlZDUzODE3ZGIx
|
10
|
+
ZjdiZmNjOWQ4MzBhYWNjNGFmMTgyZjk3NDkxY2YyMzU2ODE5OTc3ZjBiZDRm
|
11
|
+
NjE0ZjdhOTdiMGNhODc5NzcwYWI3M2MxODFjOGEzOWFiNGI2MWM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
M2Q1NDNmZGE5Y2RjNzc2NmQ3YjI4Nzg4ODc2MDg5MzIwZjcyNGU5YTExYmI4
|
14
|
+
MDg5MjNjYzIwYmJjZDk1NWMwNThjYjQwODQwZGM1M2I5OTgyZjZmNmUyYzdm
|
15
|
+
NjFlMTIxOGJiNmE2YzliMDU1NjNkNzg3YTVlYWYxZDYzZGUxZDY=
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -114,7 +114,7 @@ my_date_time = DateTimeAttribute::Container.new
|
|
114
114
|
my_date_time.date_time # => nil
|
115
115
|
my_date_time.date = '2001-02-03'
|
116
116
|
my_date_time.date_time # => 2001-02-03 00:00:00 +0700
|
117
|
-
my_date_time.
|
117
|
+
my_date_time.time = '10:00pm'
|
118
118
|
my_date_time.date_time # => 2001-02-03 22:00:00 +0700
|
119
119
|
|
120
120
|
# ...same as described above
|
data/date_time_attribute.gemspec
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{date_time_attribute}
|
5
|
-
s.version = "0.0.
|
5
|
+
s.version = "0.0.6"
|
6
6
|
|
7
7
|
s.date = %q{2013-11-22}
|
8
8
|
s.authors = ["Sergei Zinin"]
|
@@ -20,4 +20,5 @@ Gem::Specification.new do |s|
|
|
20
20
|
|
21
21
|
s.add_runtime_dependency 'activesupport', ">= 3.0.0"
|
22
22
|
s.add_development_dependency 'bundler'
|
23
|
+
s.add_development_dependency 'activerecord', ">= 4.0.2"
|
23
24
|
end
|
data/lib/date_time_attribute.rb
CHANGED
@@ -4,7 +4,7 @@ require 'active_support/duration'
|
|
4
4
|
require 'date_time_attribute/container'
|
5
5
|
|
6
6
|
module DateTimeAttribute
|
7
|
-
VERSION = '0.0.
|
7
|
+
VERSION = '0.0.6'
|
8
8
|
|
9
9
|
extend ActiveSupport::Concern
|
10
10
|
|
@@ -53,12 +53,15 @@ module DateTimeAttribute
|
|
53
53
|
|
54
54
|
attributes.each do |attribute|
|
55
55
|
attribute = attribute.to_sym
|
56
|
+
is_active_record_attribute = respond_to?(:attribute_method?) && attribute_method?(attribute)
|
56
57
|
|
57
|
-
# ActiveRecord issue: https://
|
58
|
-
if
|
59
|
-
|
58
|
+
# ActiveRecord lazy initialization issue: https://github.com/einzige/date_time_attribute/issues/2
|
59
|
+
if is_active_record_attribute && !attribute_methods_generated?
|
60
|
+
define_attribute_methods
|
60
61
|
end
|
61
62
|
|
63
|
+
attr_accessor attribute unless method_defined?(attribute)
|
64
|
+
|
62
65
|
define_method("#{attribute}_date") do
|
63
66
|
in_time_zone(time_zone) do |time_zone|
|
64
67
|
date_time_container(attribute).in_time_zone(time_zone).date
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DateTimeAttribute, ActiveRecord::Base, use_active_record: true do
|
4
|
+
before do
|
5
|
+
Time.zone = 'Pacific Time (US & Canada)'
|
6
|
+
end
|
7
|
+
|
8
|
+
context 'persisted record' do
|
9
|
+
subject(:target) { Model.create! }
|
10
|
+
|
11
|
+
it { should respond_to :created_at }
|
12
|
+
it { should respond_to :created_at_time }
|
13
|
+
it { should respond_to :created_at_date }
|
14
|
+
|
15
|
+
describe 'values' do
|
16
|
+
let(:created_at) { nil }
|
17
|
+
let(:date) { nil }
|
18
|
+
let(:time) { nil }
|
19
|
+
let(:time_zone) { nil }
|
20
|
+
subject(:date_time) { target.created_at }
|
21
|
+
|
22
|
+
before do
|
23
|
+
target.created_at = created_at
|
24
|
+
target.created_at_date = date
|
25
|
+
target.created_at_time = time
|
26
|
+
target.created_at_time_zone = time_zone
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'nothing set' do
|
30
|
+
let(:created_at) { nil }
|
31
|
+
let(:date) { nil }
|
32
|
+
let(:time) { nil }
|
33
|
+
|
34
|
+
it { should be_nil }
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'date_time set' do
|
38
|
+
let(:created_at) { Time.zone.now }
|
39
|
+
it { should_not be_nil }
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'date_time reset to nil' do
|
43
|
+
let(:created_at) { nil }
|
44
|
+
let(:date) { '2001-02-03' }
|
45
|
+
let(:time) { '10:00pm' }
|
46
|
+
|
47
|
+
it { should_not be_nil }
|
48
|
+
|
49
|
+
context 'returning back to nil' do
|
50
|
+
before do
|
51
|
+
target.created_at = nil
|
52
|
+
target.created_at_date = nil
|
53
|
+
end
|
54
|
+
|
55
|
+
it { should be_nil }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'time set' do
|
60
|
+
let(:date) { nil }
|
61
|
+
let(:time) { '23:00' }
|
62
|
+
|
63
|
+
its(:year) { should == Time.zone.now.year }
|
64
|
+
its(:month) { should == Time.zone.now.month }
|
65
|
+
its(:day) { should == Time.zone.now.day }
|
66
|
+
its(:hour) { should == 23 }
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'date set' do
|
70
|
+
let(:date) { '2001-02-03' }
|
71
|
+
let(:time) { nil }
|
72
|
+
|
73
|
+
its(:year) { should == 2001 }
|
74
|
+
its(:month) { should == 2 }
|
75
|
+
its(:day) { should == 3 }
|
76
|
+
its(:hour) { should == 0 }
|
77
|
+
|
78
|
+
context 'time set' do
|
79
|
+
let(:date) { '2001-02-03' }
|
80
|
+
let(:time) { '10:00pm' }
|
81
|
+
|
82
|
+
its(:year) { should == 2001 }
|
83
|
+
its(:month) { should == 2 }
|
84
|
+
its(:day) { should == 3 }
|
85
|
+
its(:hour) { should == 22 }
|
86
|
+
|
87
|
+
context 'timezone set explicitly' do
|
88
|
+
let(:time_zone) { 'Krasnoyarsk' }
|
89
|
+
|
90
|
+
its(:year) { should == 2001 }
|
91
|
+
its(:month) { should == 2 }
|
92
|
+
its(:day) { should == 3 }
|
93
|
+
its(:hour) { should == 22 }
|
94
|
+
|
95
|
+
specify do
|
96
|
+
subject.time_zone.name.should == 'Krasnoyarsk'
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -23,7 +23,7 @@ describe DateTimeAttribute::Container do
|
|
23
23
|
target.time = time
|
24
24
|
end
|
25
25
|
|
26
|
-
context
|
26
|
+
context 'nothing set' do
|
27
27
|
let(:date_time) { nil }
|
28
28
|
let(:date) { nil }
|
29
29
|
let(:time) { nil }
|
@@ -31,7 +31,7 @@ describe DateTimeAttribute::Container do
|
|
31
31
|
its(:date_time) { should be_nil }
|
32
32
|
end
|
33
33
|
|
34
|
-
context
|
34
|
+
context 'time set' do
|
35
35
|
let(:date) { nil }
|
36
36
|
let(:time) { '23:00' }
|
37
37
|
|
@@ -41,7 +41,7 @@ describe DateTimeAttribute::Container do
|
|
41
41
|
its(:hour) { should == 23 }
|
42
42
|
end
|
43
43
|
|
44
|
-
context
|
44
|
+
context 'date set' do
|
45
45
|
let(:date) { '2001-02-03' }
|
46
46
|
let(:time) { nil }
|
47
47
|
|
@@ -53,7 +53,7 @@ describe DateTimeAttribute::Container do
|
|
53
53
|
subject.hour.should == 0
|
54
54
|
end
|
55
55
|
|
56
|
-
context
|
56
|
+
context 'time set' do
|
57
57
|
let(:date) { '2001-02-03' }
|
58
58
|
let(:time) { '10:00pm' }
|
59
59
|
|
@@ -62,7 +62,7 @@ describe DateTimeAttribute::Container do
|
|
62
62
|
its(:day) { should == 3 }
|
63
63
|
its(:hour) { should == 22 }
|
64
64
|
|
65
|
-
context
|
65
|
+
context 'non string values' do
|
66
66
|
let(:date) { Date.new(2001, 02, 03) }
|
67
67
|
let(:time) { Time.new(2001, 02, 03, 04, 05) }
|
68
68
|
|
@@ -72,7 +72,7 @@ describe DateTimeAttribute::Container do
|
|
72
72
|
its(:hour) { should == 4 }
|
73
73
|
end
|
74
74
|
|
75
|
-
context
|
75
|
+
context 'time zone set' do
|
76
76
|
its(:year) { should == 2001 }
|
77
77
|
its(:month) { should == 2 }
|
78
78
|
|
@@ -88,7 +88,7 @@ describe DateTimeAttribute::Container do
|
|
88
88
|
expect { subject.time_zone = 'Moscow' }.to change { subject.hour }.from(22).to(9)
|
89
89
|
end
|
90
90
|
|
91
|
-
context
|
91
|
+
context 'different environment timezone set' do
|
92
92
|
before do
|
93
93
|
Time.zone = 'Krasnoyarsk'
|
94
94
|
end
|
@@ -5,7 +5,7 @@ describe DateTimeAttribute do
|
|
5
5
|
Time.zone = 'Pacific Time (US & Canada)'
|
6
6
|
end
|
7
7
|
|
8
|
-
describe
|
8
|
+
describe '.parser' do
|
9
9
|
its(:parser) { should == Time.zone }
|
10
10
|
end
|
11
11
|
|
@@ -26,7 +26,7 @@ describe DateTimeAttribute do
|
|
26
26
|
it { should respond_to :created_at_time }
|
27
27
|
it { should respond_to :created_at_date }
|
28
28
|
|
29
|
-
describe
|
29
|
+
describe 'values' do
|
30
30
|
let(:due_at) { nil }
|
31
31
|
let(:date) { nil }
|
32
32
|
let(:time) { nil }
|
@@ -40,7 +40,7 @@ describe DateTimeAttribute do
|
|
40
40
|
target.due_at_time_zone = time_zone
|
41
41
|
end
|
42
42
|
|
43
|
-
context
|
43
|
+
context 'nothing set' do
|
44
44
|
let(:due_at) { nil }
|
45
45
|
let(:date) { nil }
|
46
46
|
let(:time) { nil }
|
@@ -48,19 +48,19 @@ describe DateTimeAttribute do
|
|
48
48
|
it { should be_nil }
|
49
49
|
end
|
50
50
|
|
51
|
-
context
|
51
|
+
context 'date_time set' do
|
52
52
|
let(:due_at) { Time.zone.now }
|
53
53
|
it { should_not be_nil }
|
54
54
|
end
|
55
55
|
|
56
|
-
context
|
56
|
+
context 'date_time reset to nil' do
|
57
57
|
let(:due_at) { nil }
|
58
58
|
let(:date) { '2001-02-03' }
|
59
59
|
let(:time) { '10:00pm' }
|
60
60
|
|
61
61
|
it { should_not be_nil }
|
62
62
|
|
63
|
-
context
|
63
|
+
context 'returning back to nil' do
|
64
64
|
before do
|
65
65
|
target.due_at = nil
|
66
66
|
target.due_at_date = nil
|
@@ -70,7 +70,7 @@ describe DateTimeAttribute do
|
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
73
|
-
context
|
73
|
+
context 'time set' do
|
74
74
|
let(:date) { nil }
|
75
75
|
let(:time) { '23:00' }
|
76
76
|
|
@@ -80,7 +80,7 @@ describe DateTimeAttribute do
|
|
80
80
|
its(:hour) { should == 23 }
|
81
81
|
end
|
82
82
|
|
83
|
-
context
|
83
|
+
context 'date set' do
|
84
84
|
let(:date) { '2001-02-03' }
|
85
85
|
let(:time) { nil }
|
86
86
|
|
@@ -89,7 +89,7 @@ describe DateTimeAttribute do
|
|
89
89
|
its(:day) { should == 3 }
|
90
90
|
its(:hour) { should == 0 }
|
91
91
|
|
92
|
-
context
|
92
|
+
context 'time set' do
|
93
93
|
let(:date) { '2001-02-03' }
|
94
94
|
let(:time) { '10:00pm' }
|
95
95
|
|
@@ -98,7 +98,7 @@ describe DateTimeAttribute do
|
|
98
98
|
its(:day) { should == 3 }
|
99
99
|
its(:hour) { should == 22 }
|
100
100
|
|
101
|
-
context
|
101
|
+
context 'timezone set explicitly' do
|
102
102
|
let(:time_zone) { 'Krasnoyarsk' }
|
103
103
|
|
104
104
|
its(:year) { should == 2001 }
|
@@ -111,7 +111,7 @@ describe DateTimeAttribute do
|
|
111
111
|
end
|
112
112
|
end
|
113
113
|
|
114
|
-
context
|
114
|
+
context 'time zone set on attribute' do
|
115
115
|
let(:dummy_class) do
|
116
116
|
Class.new do
|
117
117
|
include DateTimeAttribute
|
@@ -128,7 +128,7 @@ describe DateTimeAttribute do
|
|
128
128
|
subject.time_zone.name.should == 'Moscow'
|
129
129
|
end
|
130
130
|
|
131
|
-
context
|
131
|
+
context 'timezone set explicitly' do
|
132
132
|
let(:time_zone) { 'Krasnoyarsk' }
|
133
133
|
|
134
134
|
its(:year) { should == 2001 }
|
@@ -141,7 +141,7 @@ describe DateTimeAttribute do
|
|
141
141
|
end
|
142
142
|
end
|
143
143
|
|
144
|
-
context
|
144
|
+
context 'different timezone set' do
|
145
145
|
before do
|
146
146
|
Time.zone = 'Krasnoyarsk'
|
147
147
|
end
|
@@ -156,7 +156,7 @@ describe DateTimeAttribute do
|
|
156
156
|
end
|
157
157
|
end
|
158
158
|
|
159
|
-
context
|
159
|
+
context 'timezone proc given' do
|
160
160
|
let(:dummy_class) do
|
161
161
|
Class.new do
|
162
162
|
include DateTimeAttribute
|
@@ -174,7 +174,7 @@ describe DateTimeAttribute do
|
|
174
174
|
end
|
175
175
|
end
|
176
176
|
|
177
|
-
context
|
177
|
+
context 'timezone method given' do
|
178
178
|
let(:dummy_class) do
|
179
179
|
Class.new do
|
180
180
|
include DateTimeAttribute
|
@@ -196,7 +196,7 @@ describe DateTimeAttribute do
|
|
196
196
|
end
|
197
197
|
end
|
198
198
|
|
199
|
-
context
|
199
|
+
context 'nil timezone' do
|
200
200
|
let(:dummy_class) do
|
201
201
|
Class.new do
|
202
202
|
include DateTimeAttribute
|
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,20 @@
|
|
1
|
-
if ENV['COVERALLS_CONFIG'] != 'nocoveralls'
|
2
|
-
require 'coveralls'
|
3
|
-
Coveralls.wear!
|
4
|
-
end
|
5
|
-
|
6
1
|
require 'date_time_attribute'
|
2
|
+
require 'active_record'
|
3
|
+
|
4
|
+
shared_context 'use active record', use_active_record: true do
|
5
|
+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
|
6
|
+
|
7
|
+
ActiveRecord::Schema.define do
|
8
|
+
create_table 'models' do |table|
|
9
|
+
table.column :created_at, :datetime
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Model < ActiveRecord::Base
|
14
|
+
include DateTimeAttribute
|
15
|
+
date_time_attribute :created_at
|
16
|
+
end
|
17
|
+
end
|
7
18
|
|
8
19
|
RSpec.configure do |config|
|
9
20
|
config.mock_with :rspec
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: date_time_attribute
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergei Zinin
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ! '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activerecord
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 4.0.2
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 4.0.2
|
41
55
|
description: Allows to assign date and time attributes separately for a DateTime attribute
|
42
56
|
in your model instance. Plays with time zones as well.
|
43
57
|
email: szinin@gmail.com
|
@@ -55,6 +69,7 @@ files:
|
|
55
69
|
- date_time_attribute.gemspec
|
56
70
|
- lib/date_time_attribute.rb
|
57
71
|
- lib/date_time_attribute/container.rb
|
72
|
+
- spec/active_record_spec.rb
|
58
73
|
- spec/date_time_attribute/container_spec.rb
|
59
74
|
- spec/date_time_attribute_spec.rb
|
60
75
|
- spec/spec_helper.rb
|