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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dae606e5d3375bd48154139a26361f6bf8b869a3
4
- data.tar.gz: a067411953defc897cea46c1def5e72dcd180306
3
+ metadata.gz: af2a62eac1cbc0a0e847a7df56654c070036a40c
4
+ data.tar.gz: c9cfe06608e9a6f517f776724c761cd4dc78d0ab
5
5
  SHA512:
6
- metadata.gz: 00738484d32ed6108e7627860b1964ddedb09f673881386a5f54858ff091ce19c82799bafd3cdbc631d9f73d4734b07306402e3ed00ac0a8d0698cc81a87c3ef
7
- data.tar.gz: 222659bba1e8e58cc816767b3788e55802828337eeeb69c1049d1580cc34e025ee97e3691c728f061f90004a2bc2f5de650ea0754a1caca34cfa9e63e2d6f454
6
+ metadata.gz: 4fdb5cb10e914eb9de39cbaddf239e94877716fe067f1eeedc5df019613740abf309a924c9f2de3c2e5b3c0cd699e8e6516030ec9e203d94ffdf5dee82817833
7
+ data.tar.gz: 4b4f590102288a38df56c1312d7d0bb4fbc161519eb442475d62ae60be24fcb72d6c9dcb8cecb0d38f4fdc5cf147d9ad9bf60de9f27c9f73fabaccfa07dafcc9
@@ -2,56 +2,60 @@ require 'date_time_attributes/railtie' if defined?(Rails)
2
2
 
3
3
  module DateTimeAttributes
4
4
 
5
- def date_time_attributes_for *date_time_attributes
6
- date_time_attributes.each { |attribute| define_date_time_accessors_for attribute }
7
- end
5
+ module ClassMethods
8
6
 
9
- private
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
- def define_date_time_accessors_for attribute
12
- attribute_name = attribute.to_s.sub /_at$/, ""
13
- assignment = "#{attribute}=".to_sym
11
+ private
14
12
 
15
- self.class_eval do
13
+ def define_date_time_accessors_for attribute
14
+ attribute_name = attribute.to_s.sub /_at$/, ""
15
+ assignment = "#{attribute}=".to_sym
16
16
 
17
- define_method "#{attribute_name}_date" do
18
- attribute_to_time_with_format attribute, '%Y-%m-%d'
19
- end
17
+ self.class_eval do
20
18
 
21
- define_method "#{attribute_name}_time" do
22
- attribute_to_time_with_format attribute, '%H:%M:%S'
23
- end
19
+ define_method "#{attribute_name}_date" do
20
+ attribute_to_time_with_format attribute, '%Y-%m-%d'
21
+ end
24
22
 
25
- define_method "#{attribute_name}_date=" do |new_date|
26
- if blank_value? new_date
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
- define_method "#{attribute_name}_time=" do |new_time|
35
- return if blank_value? new_time
36
- new_time = Time.parse new_time if new_time.is_a? String
37
- self.send assignment, (self.send(attribute) || Time.now).change(hour: new_time.hour, min: new_time.min, sec: new_time.sec)
38
- end
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
- private
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
- def attribute_to_time_with_format attribute, format
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
- def blank_value? value
51
- value.respond_to?(:empty?) ? !!value.empty? : !value
52
- end
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
@@ -4,7 +4,7 @@ module DateTimeAttribute
4
4
 
5
5
  initializer 'date_time_attributes.include' do
6
6
  ActiveSupport.on_load(:active_record) do
7
- ActiveRecord::Base.send :extend, DateTimeAttributes
7
+ ActiveRecord::Base.send :extend, DateTimeAttributes::ClassMethods
8
8
  end
9
9
  end
10
10
 
@@ -1,3 +1,3 @@
1
1
  module DateTimeAttributes
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  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.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-10-29 00:00:00.000000000 Z
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.4.8
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