date_time_attribute 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZWNmMGZkNmU3MjU3YmJlNmY5M2Y1OTE4ZTcxNjYxYjJjYzhjYzRmNQ==
4
+ M2Q3NWU3ZjQ5MTFmZGM1MzU3MTczMmUwNzViYTYxYzc1NThhNWMyOQ==
5
5
  data.tar.gz: !binary |-
6
- ODIyYTdjYzg0NTEyZWRlYWY1NGJkOTNiNWEyNWZjNzIyYjRiMzgyNw==
6
+ MWFiYWRjMDhmNTkyMTcwNDVhMjRlNjIxM2E0NGRkMGMwMDgyMWU1OA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YmFiMjNhNjIyYTcwOTJmNmZiYzQwY2E5OGI4ZjFjMzdhYmQ0NTkzYzllY2Ey
10
- MWRiYjYxYjZlZDUzNjc4YmM0MjUzZDQ3MzY0NmQxNjg1ZjQ3MTAzZTViOThk
11
- NmRjZjdkZTg0OGEwYjk4ZmYyYWQwNDYxMWU4YTZkZGU0ODY2NDk=
9
+ ZTdkN2Y1MDg3OWU4NjJhN2FmOGI5Y2ExZDZkMGZmNmYwZjIzMTU0NDM1MTdl
10
+ NGU1YzUyYTViYzlkYjdmNDAwMWQ2NDA3YzA2ODFlNzBlM2Y5YTZmYmMzZjE5
11
+ NDJkZDZkNjRkZDA0ZTgxNTE1ZGQwMjI5OWQ3ZWJlMWMzNDI0OWM=
12
12
  data.tar.gz: !binary |-
13
- YmU4NjQxNDIzYjMxNDQzODZmYmI2MTRkZjY3NjQzYTE5NWI4NzQ3MTZiODM0
14
- ZTIyNzE3ZTI5ZjJiNzBkYTRlNjNlNDg1NTE2N2I4ZDc2MjM4MTFlMTdkNDYz
15
- NjU5NDVmNGEwMjc4NTFkYjJlNWU5YTQ5YTRlY2U3ZmRmNDkxZGM=
13
+ MDc5ZTRmOTk0Y2E3Zjg1YTcwZDc1NjBhNzFlMWEwMjY5MzlmY2IwM2ZhNGNi
14
+ NWU4MDcyMDNjYTBmMTQ5MmMwNjc1MjI0ZWU4NDMzYjVkNWI0YTJhMzNiMGJm
15
+ Nzc1MTJiYzQ2ZjViNjA4NDkwOWVjYzBlNjQ0N2Q3YmZkMjhmMmQ=
data/.gitignore CHANGED
@@ -17,3 +17,4 @@ doc
17
17
  *.gem
18
18
  .rvmrc
19
19
  Gemfile.lock
20
+ .idea
data/README.md CHANGED
@@ -2,12 +2,9 @@
2
2
  [![Gem Version](https://badge.fury.io/rb/date_time_attribute.png)](http://badge.fury.io/rb/date_time_attribute)
3
3
  [![Build Status](https://travis-ci.org/einzige/date_time_attribute.png?branch=master)](https://travis-ci.org/einzige/date_time_attribute)
4
4
  [![Dependency Status](https://gemnasium.com/einzige/date_time_attribute.png)](https://gemnasium.com/einzige/date_time_attribute)
5
- [![Coverage Status](https://coveralls.io/repos/einzige/date_time_attribute/badge.png)](https://coveralls.io/r/einzige/date_time_attribute)
6
5
 
7
6
  Splits DateTime attribute access into two Data, Time and TimeZone attributes.
8
7
 
9
- See also [ActiveRecord (Rails) version](https://github.com/einzige/date_time_attribute_rails).
10
-
11
8
  ## Install
12
9
 
13
10
  ```bash
@@ -121,8 +118,24 @@ my_date_time.dime = '10:00pm'
121
118
  my_date_time.date_time # => 2001-02-03 22:00:00 +0700
122
119
 
123
120
  # ...same as described above
121
+
122
+ my_date_time = DateTimeAttribute::Container.new(Time.zone.now)
123
+ my_date_time.date_time # => 2013-12-03 00:02:01 +0000
124
124
  ```
125
125
 
126
- ## See also
126
+ ## ActiveRecord users
127
+
128
+ If you are using Rails put in your initializers (eg `config/initializers/date_time_attribute.rb`):
129
+
130
+ ```ruby
131
+ ActiveRecord::Base.send(:include, DateTimeAttribute)
132
+ ```
133
+
134
+ Then add attributes into your models:
135
+
136
+ ```ruby
137
+ class MyModel < ActiveRecord::Base
138
+ date_time_attribute :created_at, :updated_at # See more examples above
139
+ end
140
+ ```
127
141
 
128
- [ActiveRecord (Rails) version](https://github.com/einzige/date_time_attribute_rails)
@@ -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.4"
5
+ s.version = "0.0.5"
6
6
 
7
7
  s.date = %q{2013-11-22}
8
8
  s.authors = ["Sergei Zinin"]
@@ -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.4'
7
+ VERSION = '0.0.5'
8
8
 
9
9
  extend ActiveSupport::Concern
10
10
 
@@ -65,6 +65,16 @@ module DateTimeAttribute
65
65
  end
66
66
  end
67
67
 
68
+ alias_method "old_#{attribute}=", "#{attribute}="
69
+
70
+ define_method("#{attribute}=") do |val|
71
+ in_time_zone(time_zone) do |time_zone|
72
+ container = date_time_container(attribute).in_time_zone(time_zone)
73
+ container.date_time = val
74
+ self.send("old_#{attribute}=", container.date_time)
75
+ end
76
+ end
77
+
68
78
  define_method("#{attribute}_time") do
69
79
  in_time_zone(time_zone) do |time_zone|
70
80
  date_time_container(attribute).in_time_zone(time_zone).time
@@ -3,8 +3,19 @@ require 'active_support/core_ext/time/zones'
3
3
  require 'active_support/core_ext/module/delegation'
4
4
 
5
5
  module DateTimeAttribute
6
- class Container < Struct.new(:date_time, :date, :time, :time_zone)
7
- delegate :year, :month, :day, :hour, to: :date_time
6
+ class Container
7
+ attr_accessor :date_time, :date, :time, :time_zone
8
+
9
+ # @param [String, Date, Time, DateTime, nil] date_time
10
+ # @param [String, Date, Time, DateTime, nil] date
11
+ # @param [String, Date, Time, DateTime, nil] time
12
+ # @param [String] time_zone
13
+ def initialize(date_time = nil, date = nil, time = nil, time_zone = nil)
14
+ self.date_time = date_time
15
+ self.date = date
16
+ self.time = time
17
+ self.time_zone = time_zone
18
+ end
8
19
 
9
20
  # @param [String] time_zone
10
21
  # @return [Container] self
@@ -14,6 +25,17 @@ module DateTimeAttribute
14
25
  self
15
26
  end
16
27
 
28
+ # @param [String, Date, Time, DateTime, nil] val
29
+ def date_time=(val)
30
+ @date = parse(val)
31
+ @time = parse(val)
32
+ @date_time = val
33
+ end
34
+
35
+ def date_time
36
+ @date_time
37
+ end
38
+
17
39
  # @return [String, nil]
18
40
  def time_zone
19
41
  @time_zone
@@ -48,6 +70,22 @@ module DateTimeAttribute
48
70
  @time || date_time
49
71
  end
50
72
 
73
+ def year
74
+ date_time.try(:year)
75
+ end
76
+
77
+ def month
78
+ date_time.try(:month)
79
+ end
80
+
81
+ def day
82
+ date_time.try(:day)
83
+ end
84
+
85
+ def hour
86
+ date_time.try(:hour)
87
+ end
88
+
51
89
  def self.parser
52
90
  @parser || Time.zone || Time
53
91
  end
@@ -53,6 +53,23 @@ describe DateTimeAttribute do
53
53
  it { should_not be_nil }
54
54
  end
55
55
 
56
+ context "date_time reset to nil" do
57
+ let(:due_at) { nil }
58
+ let(:date) { '2001-02-03' }
59
+ let(:time) { '10:00pm' }
60
+
61
+ it { should_not be_nil }
62
+
63
+ context "returning back to nil" do
64
+ before do
65
+ target.due_at = nil
66
+ target.due_at_date = nil
67
+ end
68
+
69
+ it { should be_nil }
70
+ end
71
+ end
72
+
56
73
  context "time set" do
57
74
  let(:date) { nil }
58
75
  let(:time) { '23:00' }
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
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergei Zinin