time_jawn 1.2.2 → 2.0.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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a6a4e6f180be9199ca277acb30b77eb1c669e5d2
4
- data.tar.gz: 7d4358869e51cdc0c2beabca8d190b88b0d413e3
3
+ metadata.gz: 43cab13ed141d58f7c5c1548f44add5d884b78ec
4
+ data.tar.gz: f77795556c4518bf255f72c2a5a7b722f73e0b12
5
5
  SHA512:
6
- metadata.gz: 267b4c81102f5f0802a19fd994588328e9dd58c5ea7f01f3b070cc36c33a0e885d3da624c6bad842a816dc47d64a2af0627ad2e404cd15faeddd6dd0425fb53e
7
- data.tar.gz: cad1e579ac4eacce73346c287be8d4d95bd3dfec0ec173b5852311ac46ec71038eaecb423f5d5e8183a64fd6b0de4e7d97c5a415e630c04e4328af9482070f22
6
+ metadata.gz: 0ceff171264974bf5b9a05f861bd15c79b32fe5b51d8b41d16283eccf5c70992e649e8ef53f29558bece9a64bc04ca780747eb264b48f3f299e36e44873329e2
7
+ data.tar.gz: a7f703ff5859bb5f580b14e4b6695dca293e5149c8f4969ca2bfccb9cc009b8cafd407da64f11575e885cf846ca26ca4339b2408e3eeaf7006f6bc45ef091894
@@ -1,5 +1,6 @@
1
- # The base module of the TimeJawn gem. Everything in here assumes that your model has a valid time zone
2
- # in a attribute name time_zone or has been delegating one to somewhere else.
1
+ # The base module of the TimeJawn gem. Everything in here assumes that your
2
+ # model has a valid time zone in a attribute name time_zone or has been
3
+ # delegating one to somewhere else.
3
4
  module TimeJawn
4
5
  require 'time_jawn/time_jawn_private_class_methods'
5
6
 
@@ -15,26 +16,31 @@ module TimeJawn
15
16
  include TimeJawnPrivateClassMethods
16
17
  attr_reader :time_zone_attribute_name
17
18
  # When called it loads the methods located in InstanceMethods.
18
- # It is typically included in a model's rb file so that instances of that class gain the InstanceMethods at each instantiation.
19
+ # It is typically included in a model's rb file so that instances of that
20
+ # class gain the InstanceMethods at each instantiation.
19
21
  # class Event<ActiveRecord::Base
20
22
  # has_time_zone
21
23
  # end
22
- # Optionally you may pass the name of your time zone attribute in as a symbol.
24
+ # Optionally you may pass the name of your time zone attribute in as a
25
+ # symbol.
23
26
  # class Event<ActiveRecord::Base
24
- # has_time_zone :this_is_my_time_zone
25
- def has_time_zone(time_zone_attribute_name=:time_zone, options_hash={})
26
- _set_instance_variables(time_zone_attribute_name, options_hash)
27
- send :include, InstanceMethods
27
+ # has_time_zone named: :this_is_my_time_zone
28
+ # end
29
+ def has_time_zone(options_hash={})
30
+ set_instance_variables(options_hash)
31
+ send(:include, InstanceMethods)
28
32
  end
29
33
  end
30
34
 
31
- #Defines methods that will be added to instances of classes that have previously called has_time_zone.
35
+ #Defines methods that will be added to instances of classes that have
36
+ # previously called has_time_zone.
32
37
  module InstanceMethods
33
- # This method generates a series of methods on instances by calling the _generate_to_local and
34
- # _generate_to_local_with_assignment that are private on teh parent class. The methods that are created are called
35
- # local_#{attribue} and local_#{attribute}= the attribute portion their names are completed by enumerating
36
- # the datetime_attributes of the class. Twice as many methods as there are DateTime attributes will
37
- # be created.
38
+ # This method generates a series of methods on instances by calling the
39
+ # enerate_to_local and generate_to_local_with_assignment that are private on
40
+ # the parent class. The methods that are created are called
41
+ # local_#{attribue} and local_#{attribute}= the attribute portion their
42
+ # names are completed by enumerating the datetime_attributes of the class.
43
+ # Twice as many methods as there are DateTime attributes will be created.
38
44
  #
39
45
  # :created_at, and :updated_at
40
46
  #
@@ -43,54 +49,65 @@ module TimeJawn
43
49
  # local_created_at=
44
50
  # local_updated_at=
45
51
  #
46
- # The local_#{attribue} methods will take the value stored in the attribute indicated by the methods name
47
- # and apply a time zone conversion to it. This is useful for displaying the local time (according to the object).
52
+ # The local_#{attribue} methods will take the value stored in the attribute
53
+ # indicated by the methods name and apply a time zone conversion to it. This
54
+ # is useful for displaying the local time (according to the object).
48
55
  #
49
- # local_#{attribute}= methods are assignment shortcuts. They behave a little differently than you would expect.
50
- # They do not take a local time and convert it into utc (or whatever, ActiveSupport will handle that for us),
51
- # what these assigment methods do is take any sort of string that looks like a time, or any sort of time or datetime
52
- # object lop off whatever timezone is being fed in and glue the instances local timezone on the end before applying
53
- # it to the appropriate attribute. This is convenient for some one in one time zone setting a value for an instance
54
- # that represents a different time zone. For example:
56
+ # local_#{attribute}= methods are assignment shortcuts. They behave a little
57
+ # differently than you would expect. They do not take a local time and
58
+ # convert it into utc (or whatever, ActiveSupport will handle that for us),
59
+ # what these assigment methods do is take any sort of string that looks like
60
+ # a time, or any sort of time or datetime object lop off whatever timezone
61
+ # is being fed in and glue the instances local timezone on the end before
62
+ # applying it to the appropriate attribute. This is convenient for some one
63
+ # in one time zone setting a value for an instance that represents a
64
+ # different time zone. For example:
55
65
  #
56
- # I am in Philadelphia (EST), my application is set to UTC, and I want to set the time on an Alarm instance that
57
- # goes off in San Francisco (PST). I want that time to be 6PM. In Philadlephia I choose 6PM (local), the applications assumes I
58
- # meant 6PM UTC (2PM EST and 11AM PST). That is not what I intended, I intended on 6PM PST, and now my Alarm is all wrong.
59
- # The assignment methods turn 6PM (set in EST, and processed in UTC) into 6PM PST (or 9PM EST, 1AM UTC) the expected time. The
60
- # Alarm goes off as expected!*
66
+ # I am in Philadelphia (EST), my application is set to UTC, and I want
67
+ # to set the time on an Alarm instance that goes off in San Francisco
68
+ # (PST). I want that time to be 6PM. In Philadlephia I choose 6PM
69
+ # (local), the applications assumes I meant 6PM UTC (2PM EST and 11AM
70
+ # PST). That is not what I intended, I intended on 6PM PST, and now my
71
+ # Alarm is all wrong. The assignment methods turn 6PM (set in EST, and
72
+ # processed in UTC) into 6PM PST (or 9PM EST, 1AM UTC) the expected
73
+ # time. The Alarm goes off as expected!*
61
74
  #
62
75
  # *Times in this example may be wrong since I put them in myself.
63
76
  #
64
77
  # You can see examples of how these methods work in the specs folder.
65
78
  def self.included(base)
66
- date_time_attributes = base.send(:_class_date_attributes_or_arguments)
79
+ date_time_attributes = base.send(:class_date_attributes_or_arguments)
67
80
  date_time_attributes.each do |attribute|
68
- base.send(:_generate_to_local, attribute)
69
- base.send(:_generate_to_local_with_assignment, attribute)
81
+ base.send(:generate_to_local, attribute)
82
+ base.send(:generate_to_local_with_assignment, attribute)
70
83
  end
71
84
  end
85
+
72
86
  # Returns the current time according to the instance.
73
87
  def current_time
74
- _to_local(DateTime.current)
88
+ to_local(DateTime.current)
75
89
  end
76
- # converts a time object into it's local counter part (they will have the same value but differnt presentation.)
77
- def _to_local(time)
78
- ActiveSupport::Deprecation.warn "_to_local will be made private in a future version."
90
+
91
+ private
92
+
93
+ # converts a time object into it's local counter part (they will have the
94
+ # same value but differnt presentation.)
95
+ def to_local(time)
79
96
  time.in_time_zone(self.send(self.class.time_zone_attribute_name))
80
97
  end
81
98
 
82
- # Given a string that looks like a time. It will convert that string into a time object that matches the time but with
83
- # the instances time zone appended.
84
- def _add_zone(time_string)
85
- ActiveSupport::Deprecation.warn "_add_zone will be made private in a future version."
99
+ # Given a string that looks like a time. It will convert that string into a
100
+ # time object that matches the time but with the instances time zone
101
+ # appended.
102
+ def add_zone(time_string)
86
103
  Time.zone = self.send(self.class.time_zone_attribute_name)
87
104
  Time.zone.parse(Time.parse(time_string).strftime(DATE_FORMAT))
88
105
  end
89
106
 
90
- # Returns a string representation of a time object suitable for consumption by add_zone.
91
- def _change_zone(time)
92
- ActiveSupport::Deprecation.warn "_change_zone will be made private in a future version."
93
- _add_zone(time.strftime(DATE_FORMAT))
107
+ # Returns a string representation of a time object suitable for consumption
108
+ # by add_zone.
109
+ def change_zone(time)
110
+ add_zone(time.strftime(DATE_FORMAT))
94
111
  end
95
112
  end
96
113
  end
@@ -1,61 +1,44 @@
1
1
  # Defines private methods necessary for TimeJawn to work.
2
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
- datetime_attributes
13
- end
14
3
 
15
4
  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
5
 
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
6
+ # Locates all of an ActiveRecord class' DateTime Attributes and returns them
7
+ # as an array of symbols.
8
+ def datetime_attributes
9
+ name.constantize.columns.map do |column|
10
+ next unless column.type == :datetime
11
+ column.name.to_sym
12
+ end.compact
28
13
  end
29
14
 
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
15
+ def set_instance_variables(options_hash)
16
+ @time_zone_attribute_name = options_hash.fetch(:named, :time_zone)
17
+ @time_jawn_date_time_attributes = options_hash.fetch(:time_attributes, nil)
38
18
  end
39
19
 
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)) }
20
+ # generates an instance method called "local_#{attribute}" that calls the
21
+ # to_local instance method.
22
+ def generate_to_local(attribute)
23
+ define_method(:"local_#{attribute}") { to_local(send(attribute)) }
43
24
  end
44
25
 
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)
26
+ # generates an instance method called "local_#{attribute}=" that calls either
27
+ # the add_zone or _change_zone instance methods depending on the class of the
28
+ # input.
29
+ def generate_to_local_with_assignment(attribute)
48
30
  define_method(:"local_#{attribute}=") do |time_or_string_value|
49
31
  if time_or_string_value.is_a? String
50
- write_attribute(attribute, _add_zone(time_or_string_value))
32
+ write_attribute(attribute, add_zone(time_or_string_value))
51
33
  else
52
- write_attribute(attribute, _change_zone(time_or_string_value))
34
+ write_attribute(attribute, change_zone(time_or_string_value))
53
35
  end
54
36
  end
55
37
  end
56
38
 
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
- @time_jawn_date_time_attributes || _datetime_attributes
39
+ # returns all of the date_time attributes for a class unless it is specified
40
+ # in the class.
41
+ def class_date_attributes_or_arguments
42
+ @time_jawn_date_time_attributes || datetime_attributes
60
43
  end
61
44
  end
@@ -2,16 +2,27 @@ require 'spec_helper'
2
2
 
3
3
  describe Happening do
4
4
  before(:each){
5
- @happening1 = Happening.find_by_name('Eastern Time (US & Canada)')
6
- @happening2 = Happening.find_by_name('Pacific/Honolulu')
7
- Time.zone = 'UTC'
5
+ @happening1 =
6
+ Happening.new(
7
+ start_time: DateTime.new+6725.years+3.months+1.minute,
8
+ time_zone: 'Eastern Time (US & Canada)',
9
+ name:'Eastern Time (US & Canada)',
10
+ updated_at: DateTime.new+6725.years+1.minute,
11
+ created_at: DateTime.new+6725.years+1.minute
12
+ )
13
+
14
+ @happening2 =
15
+ Happening.new(
16
+ start_time: DateTime.new+6725.years+3.months,
17
+ time_zone: 'Pacific/Honolulu', name:'Pacific/Honolulu',
18
+ updated_at: DateTime.new+6725.years,
19
+ created_at: DateTime.new+6725.years
20
+ )
21
+
22
+ Time.zone = 'UTC'
8
23
  }
24
+
9
25
  context 'class method' do
10
- describe "datetime_attributes" do
11
- it "returns an array of all datetime objects for the class" do
12
- expect(Happening._datetime_attributes).to eq [:start_time, :created_at, :updated_at]
13
- end
14
- end
15
26
  describe "has_time_zone" do
16
27
  it "does not have instance methods until called" do
17
28
  expect(Happening.instance_methods.include? :local_start_time).to eq false
@@ -34,12 +45,8 @@ describe Happening do
34
45
  end
35
46
  end
36
47
  end
37
- context 'static instance methods' do
38
-
39
- describe "self.included(base)" do
40
- pending "How do you test this? I suppose if the dynamic tests exist this worked. Seems shoddy."
41
- end
42
48
 
49
+ context 'static instance methods' do
43
50
  describe "current_time" do
44
51
  it "returns a time with zone object reflecting the current local time of the instance" do
45
52
  Timecop.freeze
@@ -47,39 +54,8 @@ describe Happening do
47
54
  Timecop.return
48
55
  end
49
56
  end
50
-
51
- describe "_to_local(time)" do
52
- it "returns a time with zone that has been coverted to reflect the local time" do
53
- expect(@happening1.start_time).to eq 'Mon, 01 Apr 2013 00:01:00 UTC +00:00'
54
- expect(@happening1._to_local(@happening1.start_time)).to eq 'Sun, 31 Mar 2013 20:01:00 EDT -04:00'
55
- expect(@happening1._to_local(@happening1.start_time).to_s).to eq "2013-03-31 20:01:00 -0400"
56
- end
57
- end
58
-
59
- describe "_add_zone(time_string)" do
60
- it "returns a time with zone object that reflects the time value passed in time_string with time zone information of instance appended" do
61
- expect(@happening1._add_zone("2013-08-19 12:34:56")).to eq 'Mon, 19 Aug 2013 12:34:56 EDT -04:00'
62
- expect(@happening2._add_zone("2013-08-19 12:34:56")).to eq 'Mon, 19 Aug 2013 12:34:56 HST -10:00'
63
- expect(@happening1._add_zone("2013-11-11 12:34:56")).to eq 'Mon, 11 Nov 2013 12:34:56 EST -05:00'
64
- expect(@happening2._add_zone("2013-11-11 12:34:56")).to eq 'Mon, 11 Nov 2013 12:34:56 HST -10:00'
65
- end
66
- end
67
-
68
- describe "_change_zone(time)" do
69
- it "returns a time with zone object that has had only it's time zone switched to local time of instance" do
70
- Time.zone = 'Rangoon'
71
- time = Time.zone.parse("Wed, 28 Aug 2015 15:16:16")
72
- expect(time.to_s.split(' ')[2]).to eq ('+0630')
73
- expect(time.to_s.split(' ')[2]).to_not eq ('-0400')
74
- expect(@happening1._change_zone(time)).to_not eq time
75
- expect(@happening1._change_zone(time).to_s.split(' ')[0]).to eq time.to_s.split(' ')[0]
76
- expect(@happening1._change_zone(time).to_s.split(' ')[1]).to eq time.to_s.split(' ')[1]
77
- expect(@happening1._change_zone(time).to_s.split(' ')[2]).to_not eq time.to_s.split(' ')[2]
78
- expect(@happening1._change_zone(time).to_s.split(' ')[2]).to_not eq ('+0630')
79
- expect(@happening1._change_zone(time).to_s.split(' ')[2]).to eq ('-0400')
80
- end
81
- end
82
57
  end
58
+
83
59
  context 'dynamic instance methods' do
84
60
  describe "local_start_time" do
85
61
  it "returns a time with zone object that reflects the value of start_time altered to the instance's time zone" do
@@ -185,7 +161,6 @@ describe Happening do
185
161
 
186
162
  @happening1.local_created_at = "2013-11-11 12:34:56"
187
163
  expect(@happening1.created_at).to eq Time.parse("2013-11-11 12:34:56 -0500")
188
-
189
164
  end
190
165
  end
191
166
  end
@@ -195,6 +170,7 @@ describe Event do
195
170
  before do
196
171
  @event1 = Event.find_by_name('Eastern Time (US & Canada)')
197
172
  end
173
+
198
174
  context "Event should have time_jawn methods even though it has a non_conventional attribute" do
199
175
  subject { @event1 }
200
176
 
@@ -205,9 +181,6 @@ describe Event do
205
181
  it { should respond_to :local_created_at= }
206
182
  it { should respond_to :local_updated_at= }
207
183
  it { should respond_to :current_time }
208
- it { should respond_to :_to_local }
209
- it { should respond_to :_add_zone }
210
- it { should respond_to :_change_zone }
211
184
  end
212
185
  end
213
186
 
@@ -234,9 +207,6 @@ describe Occurrence do
234
207
  it { should respond_to :local_created_at= }
235
208
  it { should_not respond_to :local_updated_at= }
236
209
  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
210
  end
241
211
  end
242
212
 
@@ -263,8 +233,5 @@ describe Occasion do
263
233
  it { should_not respond_to :local_created_at= }
264
234
  it { should_not respond_to :local_updated_at= }
265
235
  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
236
  end
270
237
  end
metadata CHANGED
@@ -1,83 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time_jawn
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Platt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-16 00:00:00.000000000 Z
11
+ date: 2015-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: '3.2'
19
+ version: '4.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: '3.2'
26
+ version: '4.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - '='
32
32
  - !ruby/object:Gem::Version
33
- version: '3.2'
33
+ version: '4.1'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - '='
39
39
  - !ruby/object:Gem::Version
40
- version: '3.2'
40
+ version: '4.1'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec-rails
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: sqlite3-ruby
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: timecop
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - '>='
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  description: TimeJawn makes class instances time zone aware. It doesn't care one iota
@@ -102,17 +102,17 @@ require_paths:
102
102
  - lib
103
103
  required_ruby_version: !ruby/object:Gem::Requirement
104
104
  requirements:
105
- - - ">="
105
+ - - '>='
106
106
  - !ruby/object:Gem::Version
107
- version: 1.9.3
107
+ version: '2.1'
108
108
  required_rubygems_version: !ruby/object:Gem::Requirement
109
109
  requirements:
110
- - - ">="
110
+ - - '>='
111
111
  - !ruby/object:Gem::Version
112
112
  version: '0'
113
113
  requirements: []
114
114
  rubyforge_project:
115
- rubygems_version: 2.2.2
115
+ rubygems_version: 2.0.14
116
116
  signing_key:
117
117
  specification_version: 4
118
118
  summary: TimeJawn makes time zone aware class instances.