Module: TimeJawn::InstanceMethods
- Defined in:
- lib/time_jawn/time_jawn.rb
Overview
Defines methods that will be added to instances of classes that have previously called has_time_zone.
Class Method Summary collapse
-
.included(base) ⇒ Object
This method generates a series of methods on instances by calling the _generate_to_local and _generate_to_local_with_assignment that are private on teh parent class.
Instance Method Summary collapse
-
#_add_zone(time_string) ⇒ Object
Given a string that looks like a time.
-
#_change_zone(time) ⇒ Object
Returns a string representation of a time object suitable for consumption by add_zone.
-
#_to_local(time) ⇒ Object
converts a time object into it’s local counter part (they will have the same value but differnt presentation.).
-
#current_time ⇒ Object
Returns the current time according to the instance.
Class Method Details
.included(base) ⇒ Object
This method generates a series of methods on instances by calling the _generate_to_local and
_generate_to_local_with_assignment that are private on teh parent class. The methods that are created are called
local_#attribue and local_#attribute= the attribute portion their names are completed by enumerating the datetime_attributes of the class. Twice as many methods as there are DateTime attributes will be created.
:created_at, and :updated_at
local_created_at
local_updated_at
local_created_at=
local_updated_at=
The local_#attribue methods will take the value stored in the attribute indicated by the methods name and apply a time zone conversion to it. This is useful for displaying the local time (according to the object).
local_#attribute= methods are assignment shortcuts. They behave a little differently than you would expect. They do not take a local time and convert it into utc (or whatever, ActiveSupport will handle that for us), what these assigment methods do is take any sort of string that looks like a time, or any sort of time or datetime object lop off whatever timezone is being fed in and glue the instances local timezone on the end before applying it to the appropriate attribute. This is convenient for some one in one time zone setting a value for an instance that represents a different time zone. For example:
I am in Philadelphia (EST), my application is set to UTC, and I want to set the time on an Alarm instance that
goes off in San Francisco (PST). I want that time to be 6PM. In Philadlephia I choose 6PM (local), the applications assumes I
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.
The assignment methods turn 6PM (set in EST, and processed in UTC) into 6PM PST (or 9PM EST, 1AM UTC) the expected time. The
Alarm goes off as expected!*
*Times in this example may be wrong since I put them in myself.
You can see examples of how these methods work in the specs folder.
63 64 65 66 67 68 69 |
# File 'lib/time_jawn/time_jawn.rb', line 63 def self.included(base) date_time_attributes = base.send(:_class_date_attributes_or_arguments) date_time_attributes.each do |attribute| base.send(:_generate_to_local, attribute) base.send(:_generate_to_local_with_assignment, attribute) end end |
Instance Method Details
#_add_zone(time_string) ⇒ Object
Given a string that looks like a time. It will convert that string into a time object that matches the time but with the instances time zone appended.
82 83 84 85 86 |
# File 'lib/time_jawn/time_jawn.rb', line 82 def _add_zone(time_string) ActiveSupport::Deprecation.warn "_add_zone will be made private in a future version." Time.zone = self.send(self.class.time_zone_attribute_name) Time.zone.parse(Time.parse(time_string).strftime('%a, %d %b %Y %H:%M:%S')) end |
#_change_zone(time) ⇒ Object
Returns a string representation of a time object suitable for consumption by add_zone.
89 90 91 92 |
# File 'lib/time_jawn/time_jawn.rb', line 89 def _change_zone(time) ActiveSupport::Deprecation.warn "_change_zone will be made private in a future version." _add_zone(time.strftime('%a, %d %b %Y %H:%M:%S')) end |
#_to_local(time) ⇒ Object
converts a time object into it’s local counter part (they will have the same value but differnt presentation.)
75 76 77 78 |
# File 'lib/time_jawn/time_jawn.rb', line 75 def _to_local(time) ActiveSupport::Deprecation.warn "_to_local will be made private in a future version." time.in_time_zone(self.send(self.class.time_zone_attribute_name)) end |
#current_time ⇒ Object
Returns the current time according to the instance.
71 72 73 |
# File 'lib/time_jawn/time_jawn.rb', line 71 def current_time _to_local(DateTime.current) end |