time_jawn 1.1.1 → 1.2.0

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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- N2Q3Zjk3MjhhNGY0OGY2MTg5YzUxMWU2NDlmZmM0MjdmMzI4NmM2NQ==
4
+ ZGZiZjdlNDRiOTc1YzY4NTcyM2ZiNzA5NmI0ZTNiN2EwMzVlNDg0NA==
5
5
  data.tar.gz: !binary |-
6
- ZjQ2MzVhNjJiNjlkYjFlZDAyOGU5NzJhNDRmMDg1OGJkYWRjNDNmZQ==
6
+ NDAxNTQxODRlYWY1ZDFkNTM2ZDk4MGY4YzdiMzBjYThhYmY1NTNkMw==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YzNiYWNhNjZkMzBjYzM2M2JjZmU1MzdhZjQ0MDE3ODFkYjAzNDMyODhkYTgw
10
- MjE5NmZjOWE4ODVjZDY5ZTU3NmE1YmFmMjAwZWU0YmNmNDBlZmE1NmQwYzk4
11
- YmIxMDk3YTA2NGUwNWEyOWM4N2FhNTBlNzM3NWIzNzBiYmI1ZWU=
9
+ ODA1OWU3YjAyNjI4NjMyNzBiYmY5YWI2YTVhYmNhYzUzY2Y2ZTAzOGQzZjVl
10
+ NzgyYTE1ZTcwZDY1NzM1NWU4NDc4MGM5MDJhNTcyZTZjZWYxMWFiNWQ4OWY2
11
+ MWIxNGQ2NmIyNTFiNjFmZDAxNTdhNTFhZTYwYmJhZDFiMWNlMjQ=
12
12
  data.tar.gz: !binary |-
13
- NTFjMDlhYTE2NTUwYmU0MmJhMzdhOWZlNDkzNzFhMzg3NzZhYWJjODk0MGIz
14
- NWVmODdlOWZhNzM4OTRkNWE4YjdiNGZiZmI2ODBiNTg2NDdjNTQ5NDc2ZmE0
15
- ZTA0NzA0ZWE1YmMwOTY0ZGU2MmY4NTM2N2I5MTAyMGU3ZDcwZTY=
13
+ MWM4MDY2OGU3Y2EwY2U3YjFhMGQxY2ZiMTMzNTAxYjY2OWE1OWRiNzI2M2Vk
14
+ YWNjNTk2NWM4YWQ3ZTg0M2RiYzBhYjFmZjc1YWI0NTFlODljNGU4Y2Y2MDli
15
+ YTlhZmVkOWM4OGE1OTMzYmRmY2Y0MTM2MjhlOGEyZGU2YzQxNDI=
@@ -1,46 +1,12 @@
1
1
  # The base module of the TimeJawn gem. Everything in here assumes that your model has a valid time zone
2
2
  # in a attribute name time_zone or has been delegating one to somewhere else.
3
3
  module TimeJawn
4
+ require 'time_jawn/time_jawn_private_class_methods'
4
5
  # Automatically runs and adds ClassMethods to ActiveRecord::Base
5
6
  def self.included(base)
6
7
  base.send :extend, ClassMethods
7
8
  end
8
9
 
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
-
44
10
  # Defines methods that will attached to all ActiveRecord classes.
45
11
  module ClassMethods
46
12
  include TimeJawnPrivateClassMethods
@@ -54,14 +20,12 @@ module TimeJawn
54
20
  # class Event<ActiveRecord::Base
55
21
  # has_time_zone :this_is_my_time_zone
56
22
  # end
57
- def has_time_zone(time_zone_attribute_name=:time_zone)
58
- @time_zone_attribute_name = time_zone_attribute_name
23
+ def has_time_zone(time_zone_attribute_name=:time_zone, options_hash={})
24
+ _set_instance_variables(time_zone_attribute_name, options_hash)
59
25
  send :include, InstanceMethods
60
26
  end
61
27
  end
62
28
 
63
-
64
-
65
29
  #Defines methods that will be added to instances of classes that have previously called has_time_zone.
66
30
  module InstanceMethods
67
31
  # This method generates a series of methods on instances by calling the _generate_to_local and
@@ -97,7 +61,8 @@ module TimeJawn
97
61
  #
98
62
  # You can see examples of how these methods work in the specs folder.
99
63
  def self.included(base)
100
- (base.send :_datetime_attributes).each do |attribute|
64
+ date_time_attributes = base.send(:_class_date_attributes_or_arguments)
65
+ date_time_attributes.each do |attribute|
101
66
  base.send(:_generate_to_local, attribute)
102
67
  base.send(:_generate_to_local_with_assignment, attribute)
103
68
  end
@@ -0,0 +1,65 @@
1
+ # Defines private methods necessary for TimeJawn to work.
2
+ module TimeJawnPrivateClassMethods
3
+ # Locates all of an ActiveRecord class' DateTime Attributes and returns them as an array of symbols.
4
+ def _datetime_attributes
5
+ ActiveSupport::Deprecation.warn "_datetime_attributes will be made private in a future version."
6
+ klass = name.constantize
7
+
8
+ datetime_attributes = []
9
+ klass.columns.each do |column|
10
+ datetime_attributes << column.name.to_sym if column.type == :datetime
11
+ end
12
+ return datetime_attributes
13
+ end
14
+
15
+ private
16
+
17
+ def _set_instance_variables(time_zone_attribute_name, options_hash)
18
+ @time_zone_attribute_name = _evaluate_time_zone_attribute_name(time_zone_attribute_name)
19
+ @time_jawn_date_time_attributes = _evaluate_time_jawn_date_time_attributes(time_zone_attribute_name, options_hash)
20
+ end
21
+
22
+ def _evaluate_time_zone_attribute_name(time_zone_attribute_name)
23
+ if time_zone_attribute_name.kind_of?(Hash)
24
+ :time_zone
25
+ else
26
+ time_zone_attribute_name
27
+ end
28
+ end
29
+
30
+ def _evaluate_time_jawn_date_time_attributes(time_zone_attribute_name, options_hash)
31
+ if options_hash.fetch(:time_attributes, false)
32
+ @time_jawn_date_time_attributes = options_hash.fetch(:time_attributes, nil)
33
+ elsif time_zone_attribute_name.kind_of?(Hash)
34
+ @time_jawn_date_time_attributes = time_zone_attribute_name.fetch(:time_attributes, nil)
35
+ else
36
+ @time_jawn_date_time_attributes = nil
37
+ end
38
+ end
39
+
40
+ # generates an instance method called "local_#{attribute}" that calls the _to_local instance method.
41
+ def _generate_to_local(attribute)
42
+ define_method(:"local_#{attribute}") { _to_local(send(attribute)) }
43
+ end
44
+
45
+ # generates an instance method called "local_#{attribute}=" that calls either the _add_zone or _change_zone
46
+ # instance methods depending on teh class of the input.
47
+ def _generate_to_local_with_assignment(attribute)
48
+ define_method(:"local_#{attribute}=") do |time_or_string_value|
49
+ if time_or_string_value.is_a? String
50
+ write_attribute(attribute, _add_zone(time_or_string_value))
51
+ else
52
+ write_attribute(attribute, _change_zone(time_or_string_value))
53
+ end
54
+ end
55
+ end
56
+
57
+ # returns all of the date_time attributes for a class unless it is specified in the class.
58
+ def _class_date_attributes_or_arguments
59
+ if @time_jawn_date_time_attributes
60
+ @time_jawn_date_time_attributes
61
+ else
62
+ _datetime_attributes
63
+ end
64
+ end
65
+ end
@@ -210,3 +210,61 @@ describe Event do
210
210
  it { should respond_to :_change_zone }
211
211
  end
212
212
  end
213
+
214
+ describe Occurrence do
215
+ before do
216
+ @occurrence1 = Occurrence.find_by_name('Eastern Time (US & Canada)')
217
+ end
218
+
219
+ context "Ocurrence instance attribute accessor" do
220
+ describe "time_zone_attribute_name" do
221
+ it 'should respond with the time_zone attribute name as defined in the class.' do
222
+ expect( @occurrence1.class.time_zone_attribute_name ).to eq :time_zone
223
+ end
224
+ end
225
+ end
226
+
227
+ context "Occurrence should have time_jawn methods, except for local_updated_at, since it is not specified in the model" do
228
+ subject { @occurrence1 }
229
+
230
+ it { should respond_to :local_start_time }
231
+ it { should respond_to :local_created_at }
232
+ it { should_not respond_to :local_updated_at }
233
+ it { should respond_to :local_start_time= }
234
+ it { should respond_to :local_created_at= }
235
+ it { should_not respond_to :local_updated_at= }
236
+ it { should respond_to :current_time }
237
+ it { should respond_to :_to_local }
238
+ it { should respond_to :_add_zone }
239
+ it { should respond_to :_change_zone }
240
+ end
241
+ end
242
+
243
+ describe Occasion do
244
+ before do
245
+ @occasion1 = Occasion.find_by_name('Eastern Time (US & Canada)')
246
+ end
247
+
248
+ context "Occasion instance attribute accessor" do
249
+ describe "time_zone_attribute_name" do
250
+ it 'should respond with the time_zone attribute name as defined in the class.' do
251
+ expect( @occasion1.class.time_zone_attribute_name ).to eq :t_z
252
+ end
253
+ end
254
+ end
255
+
256
+ context "Occasion should have time_jawn methods, except for local_updated_at, since it is not specified in the model, even though it has a non_conventional attribute" do
257
+ subject { @occasion1 }
258
+
259
+ it { should respond_to :local_start_time }
260
+ it { should_not respond_to :local_created_at }
261
+ it { should_not respond_to :local_updated_at }
262
+ it { should respond_to :local_start_time= }
263
+ it { should_not respond_to :local_created_at= }
264
+ it { should_not respond_to :local_updated_at= }
265
+ it { should respond_to :current_time }
266
+ it { should respond_to :_to_local }
267
+ it { should respond_to :_add_zone }
268
+ it { should respond_to :_change_zone }
269
+ end
270
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time_jawn
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Platt
@@ -90,6 +90,7 @@ extra_rdoc_files: []
90
90
  files:
91
91
  - lib/time_jawn.rb
92
92
  - lib/time_jawn/time_jawn.rb
93
+ - lib/time_jawn/time_jawn_private_class_methods.rb
93
94
  - spec/time_jawn_spec.rb
94
95
  homepage: http://tammantech.com
95
96
  licenses: