time_jawn 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/time_jawn/time_jawn.rb +48 -20
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YmI2YzM1ZmYwNzAwZTY5ZWY2ZDYyNzRkNGI1M2NjNmI2ZWEwMzk0YQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MWVmNzIwNjY0MDc2MGJkZDUxMzE4OTNhZDgzYTEzYzNmYTU2NDFhNw==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NjgyMGIyYTRiYzY5NTQ4YzExMTgzYWYxZDQ5Y2E0NDc4NjhhMDQwNjQ0NDhm
|
10
|
+
MTZkYTc5OTVhMTdiYzA4NjUzZDMyYTZiMzhmYjllZjAwMTdhNzYzOWQ3MWEw
|
11
|
+
NDgxM2MxNDFjOTcxNTcyZTA4NzFjMTJjMDFmOTUwMzhiNjRiM2E=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NTg2OWRhZDE0Y2FmODI5MmQ1M2M2MDVkMDIzZmU0YzM0NzQ3YTk4MTJmOTRk
|
14
|
+
MTFmOThlMWQzNWIwOTQ0ZGJhNmFmOWNkNTYwMjBkNzg3YzkwYTRhZWFiMjA0
|
15
|
+
ODI2YzRlMmEyNjgxOWNiMmQ3YjM3OTIxN2E3OTU0ODg4MjZhNTU=
|
data/lib/time_jawn/time_jawn.rb
CHANGED
@@ -5,8 +5,45 @@ module TimeJawn
|
|
5
5
|
def self.included(base)
|
6
6
|
base.send :extend, ClassMethods
|
7
7
|
end
|
8
|
+
|
9
|
+
|
10
|
+
# Defines private methods necessary for TimeJawn to work.
|
11
|
+
module TimeJawnPrivateClassMethods
|
12
|
+
# Locates all of an ActiveRecord class' DateTime Attributes and returns them as an array of symbols.
|
13
|
+
def _datetime_attributes
|
14
|
+
ActiveSupport::Deprecation.warn "_datetime_attributes will be made private in a future version."
|
15
|
+
klass = name.constantize
|
16
|
+
|
17
|
+
datetime_attributes = []
|
18
|
+
klass.columns.each do |column|
|
19
|
+
datetime_attributes << column.name.to_sym if column.type == :datetime
|
20
|
+
end
|
21
|
+
return datetime_attributes
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
# generates an instance method called "local_#{attribute}" that calls the _to_local instance method.
|
27
|
+
def _generate_to_local(attribute)
|
28
|
+
define_method(:"local_#{attribute}") { _to_local(send(attribute)) }
|
29
|
+
end
|
30
|
+
|
31
|
+
# generates an instance method called "local_#{attribute}=" that calls either the _add_zone or _change_zone
|
32
|
+
# instance methods depending on teh class of the input.
|
33
|
+
def _generate_to_local_with_assignment(attribute)
|
34
|
+
define_method(:"local_#{attribute}=") do |time_or_string_value|
|
35
|
+
if time_or_string_value.is_a? String
|
36
|
+
write_attribute(attribute, _add_zone(time_or_string_value))
|
37
|
+
else
|
38
|
+
write_attribute(attribute, _change_zone(time_or_string_value))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
8
44
|
# Defines methods that will attached to all ActiveRecord classes.
|
9
45
|
module ClassMethods
|
46
|
+
include TimeJawnPrivateClassMethods
|
10
47
|
# When called it loads the methods located in InstanceMethods.
|
11
48
|
# It is typically included in a model's rb file so that instances of that class gain the InstanceMethods at each instantiation.
|
12
49
|
# class Event<ActiveRecord::Base
|
@@ -15,20 +52,14 @@ module TimeJawn
|
|
15
52
|
def has_time_zone
|
16
53
|
send :include, InstanceMethods
|
17
54
|
end
|
18
|
-
# Locates all of an ActiveRecord class' DateTime Attributes and returns them as an array of symbols.
|
19
|
-
def _datetime_attributes
|
20
|
-
klass = self.name.constantize
|
21
|
-
|
22
|
-
datetime_attributes = []
|
23
|
-
klass.columns.each do |column|
|
24
|
-
datetime_attributes << column.name.to_sym if column.type == :datetime
|
25
|
-
end
|
26
|
-
return datetime_attributes
|
27
|
-
end
|
28
55
|
end
|
56
|
+
|
57
|
+
|
58
|
+
|
29
59
|
#Defines methods that will be added to instances of classes that have previously called has_time_zone.
|
30
60
|
module InstanceMethods
|
31
|
-
# This method generates a series of methods on instances
|
61
|
+
# This method generates a series of methods on instances by calling the _generate_to_local and
|
62
|
+
# _generate_to_local_with_assignment that are private on teh parent class. The methods that are created are called
|
32
63
|
# local_#{attribue} and local_#{attribute}= the attribute portion their names are completed by enumerating
|
33
64
|
# the datetime_attributes of the class. Twice as many methods as there are DateTime attributes will
|
34
65
|
# be created.
|
@@ -60,15 +91,9 @@ module TimeJawn
|
|
60
91
|
#
|
61
92
|
# You can see examples of how these methods work in the specs folder.
|
62
93
|
def self.included(base)
|
63
|
-
base._datetime_attributes.each do |attribute|
|
64
|
-
|
65
|
-
|
66
|
-
if time_or_string_value.is_a? String
|
67
|
-
write_attribute(attribute, _add_zone(time_or_string_value))
|
68
|
-
else
|
69
|
-
write_attribute(attribute, _change_zone(time_or_string_value))
|
70
|
-
end
|
71
|
-
end
|
94
|
+
(base.send :_datetime_attributes).each do |attribute|
|
95
|
+
base.send(:_generate_to_local, attribute)
|
96
|
+
base.send(:_generate_to_local_with_assignment, attribute)
|
72
97
|
end
|
73
98
|
end
|
74
99
|
# Returns the current time according to the instance.
|
@@ -77,18 +102,21 @@ module TimeJawn
|
|
77
102
|
end
|
78
103
|
# converts a time object into it's local counter part (they will have the same value but differnt presentation.)
|
79
104
|
def _to_local(time)
|
105
|
+
ActiveSupport::Deprecation.warn "_to_local will be made private in a future version."
|
80
106
|
time.in_time_zone(self.time_zone)
|
81
107
|
end
|
82
108
|
|
83
109
|
# Given a string that looks like a time. It will convert that string into a time object that matches the time but with
|
84
110
|
# the instances time zone appended.
|
85
111
|
def _add_zone(time_string)
|
112
|
+
ActiveSupport::Deprecation.warn "_add_zone will be made private in a future version."
|
86
113
|
Time.zone = self.time_zone
|
87
114
|
Time.zone.parse(time_string)
|
88
115
|
end
|
89
116
|
|
90
117
|
# Returns a string representation of a time object suitable for consumption by add_zone.
|
91
118
|
def _change_zone(time)
|
119
|
+
ActiveSupport::Deprecation.warn "_change_zone will be made private in a future version."
|
92
120
|
_add_zone(time.strftime('%a, %d %b %Y %H:%M:%S'))
|
93
121
|
end
|
94
122
|
end
|