date_time_attributes 1.0.0 → 1.0.1
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/lib/date_time_attributes.rb +41 -37
- data/lib/date_time_attributes/railtie.rb +1 -1
- data/lib/date_time_attributes/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af2a62eac1cbc0a0e847a7df56654c070036a40c
|
4
|
+
data.tar.gz: c9cfe06608e9a6f517f776724c761cd4dc78d0ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4fdb5cb10e914eb9de39cbaddf239e94877716fe067f1eeedc5df019613740abf309a924c9f2de3c2e5b3c0cd699e8e6516030ec9e203d94ffdf5dee82817833
|
7
|
+
data.tar.gz: 4b4f590102288a38df56c1312d7d0bb4fbc161519eb442475d62ae60be24fcb72d6c9dcb8cecb0d38f4fdc5cf147d9ad9bf60de9f27c9f73fabaccfa07dafcc9
|
data/lib/date_time_attributes.rb
CHANGED
@@ -2,56 +2,60 @@ require 'date_time_attributes/railtie' if defined?(Rails)
|
|
2
2
|
|
3
3
|
module DateTimeAttributes
|
4
4
|
|
5
|
-
|
6
|
-
date_time_attributes.each { |attribute| define_date_time_accessors_for attribute }
|
7
|
-
end
|
5
|
+
module ClassMethods
|
8
6
|
|
9
|
-
|
7
|
+
def date_time_attributes_for *date_time_attributes
|
8
|
+
date_time_attributes.each { |attribute| define_date_time_accessors_for attribute }
|
9
|
+
end
|
10
10
|
|
11
|
-
|
12
|
-
attribute_name = attribute.to_s.sub /_at$/, ""
|
13
|
-
assignment = "#{attribute}=".to_sym
|
11
|
+
private
|
14
12
|
|
15
|
-
|
13
|
+
def define_date_time_accessors_for attribute
|
14
|
+
attribute_name = attribute.to_s.sub /_at$/, ""
|
15
|
+
assignment = "#{attribute}=".to_sym
|
16
16
|
|
17
|
-
|
18
|
-
attribute_to_time_with_format attribute, '%Y-%m-%d'
|
19
|
-
end
|
17
|
+
self.class_eval do
|
20
18
|
|
21
|
-
|
22
|
-
|
23
|
-
|
19
|
+
define_method "#{attribute_name}_date" do
|
20
|
+
attribute_to_time_with_format attribute, '%Y-%m-%d'
|
21
|
+
end
|
24
22
|
|
25
|
-
|
26
|
-
|
27
|
-
self.send assignment, nil
|
28
|
-
return
|
23
|
+
define_method "#{attribute_name}_time" do
|
24
|
+
attribute_to_time_with_format attribute, '%H:%M:%S'
|
29
25
|
end
|
30
|
-
new_date = Date.parse new_date if new_date.is_a? String
|
31
|
-
self.send assignment, (self.send(attribute) || Time.now).change(year: new_date.year, month: new_date.month, day: new_date.day)
|
32
|
-
end
|
33
26
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
27
|
+
define_method "#{attribute_name}_date=" do |new_date|
|
28
|
+
if blank_value? new_date
|
29
|
+
self.send assignment, nil
|
30
|
+
return
|
31
|
+
end
|
32
|
+
new_date = Date.parse new_date if new_date.is_a? String
|
33
|
+
self.send assignment, (self.send(attribute) || Time.now).change(year: new_date.year, month: new_date.month, day: new_date.day)
|
34
|
+
end
|
39
35
|
|
40
|
-
|
36
|
+
define_method "#{attribute_name}_time=" do |new_time|
|
37
|
+
return if blank_value? new_time
|
38
|
+
new_time = Time.parse new_time if new_time.is_a? String
|
39
|
+
self.send assignment, (self.send(attribute) || Time.now).change(hour: new_time.hour, min: new_time.min, sec: new_time.sec)
|
40
|
+
end
|
41
41
|
|
42
|
-
|
43
|
-
value = self.send(attribute)
|
44
|
-
return '' unless value.respond_to? :to_time
|
45
|
-
value = value.to_time
|
46
|
-
return '' unless value.respond_to? :utc
|
47
|
-
value.utc.strftime format
|
48
|
-
end
|
42
|
+
private
|
49
43
|
|
50
|
-
|
51
|
-
|
52
|
-
|
44
|
+
def attribute_to_time_with_format attribute, format
|
45
|
+
value = self.send(attribute)
|
46
|
+
return '' unless value.respond_to? :to_time
|
47
|
+
value = value.to_time
|
48
|
+
return '' unless value.respond_to? :utc
|
49
|
+
value.utc.strftime format
|
50
|
+
end
|
53
51
|
|
52
|
+
def blank_value? value
|
53
|
+
value.respond_to?(:empty?) ? !!value.empty? : !value
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
54
57
|
end
|
58
|
+
|
55
59
|
end
|
56
60
|
|
57
61
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: date_time_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jesús Dugarte
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -126,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
126
126
|
version: '0'
|
127
127
|
requirements: []
|
128
128
|
rubyforge_project:
|
129
|
-
rubygems_version: 2.
|
129
|
+
rubygems_version: 2.2.3
|
130
130
|
signing_key:
|
131
131
|
specification_version: 4
|
132
132
|
summary: Creates date and time virtual attributes from a date/time attribute
|