Class: Parse::TimeZone
- Inherits:
-
Object
- Object
- Parse::TimeZone
- Defined in:
- lib/parse/model/time_zone.rb
Overview
This class a wrapper around ActiveSupport::TimeZone when using Parse columns that store IANA time zone identifiers (ex. Installation collection). Parse does not have a native time zone data type, but this class is provided to manage and perform timezone-like operation on those properties which you have marked as type :timezone.
When declaring a property of type :timezone, you may also define a default just like any other property. In addition, the framework will automatically add a validation to make sure that your property is either nil or one of the valid IANA time zone identifiers.
Each instance of TimeZone has a #zone attribute that provides access to the underlying ActiveSupport::TimeZone instance, which you can use to perform time zone operations.
Constant Summary collapse
- MAPPING =
The mapping of TimeZones
ActiveSupport::TimeZone::MAPPING
Instance Attribute Summary collapse
-
#name ⇒ String
The IANA identifier for this time zone.
-
#zone ⇒ ActiveSupport::TimeZone
Returns an instance of ActiveSupport::TimeZone based on the IANA identifier.
Instance Method Summary collapse
-
#as_json(*args) ⇒ String
The IANA identifier for this timezone or nil.
-
#initialize(iana) ⇒ TimeZone
constructor
Creates a new instance given the IANA identifier (ex. America/Los_Angeles).
-
#to_s ⇒ String
The IANA identifier for this timezone or nil.
-
#valid? ⇒ Bool
Returns true or false whether the time zone exists in the ActiveSupport::TimeZone mapping.
Constructor Details
#new(iana) ⇒ Parse::TimeZone #new(timezone) ⇒ Parse::TimeZone
Creates a new instance given the IANA identifier (ex. America/Los_Angeles)
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/parse/model/time_zone.rb', line 63 def initialize(iana) if iana.is_a?(String) @name = iana @zone = nil elsif iana.is_a?(::Parse::TimeZone) @zone = iana.zone @name = nil elsif iana.is_a?(::ActiveSupport::TimeZone) @zone = iana @name = nil end end |
Instance Attribute Details
#name ⇒ String
Returns the IANA identifier for this time zone.
79 80 81 |
# File 'lib/parse/model/time_zone.rb', line 79 def name @zone.present? ? zone.name : @name end |
#zone ⇒ ActiveSupport::TimeZone
Returns an instance of ActiveSupport::TimeZone based on the IANA identifier. The setter may allow usign an IANA string identifier, a Parse::TimeZone or an ActiveSupport::TimeZone object.
100 101 102 103 104 105 106 107 108 |
# File 'lib/parse/model/time_zone.rb', line 100 def zone # lazy load the TimeZone object only when the user requests it, otherwise # just keep the name of the string around. Makes encoding/decoding faster. if @zone.nil? && @name.present? @zone = ::ActiveSupport::TimeZone.new(@name) @name = nil # clear out the cache end @zone end |
Instance Method Details
#as_json(*args) ⇒ String
Returns the IANA identifier for this timezone or nil.
126 127 128 |
# File 'lib/parse/model/time_zone.rb', line 126 def as_json(*args) name end |
#to_s ⇒ String
Returns the IANA identifier for this timezone or nil.
131 132 133 |
# File 'lib/parse/model/time_zone.rb', line 131 def to_s name end |
#valid? ⇒ Bool
Returns true or false whether the time zone exists in the ActiveSupport::TimeZone mapping.
137 138 139 |
# File 'lib/parse/model/time_zone.rb', line 137 def valid? ActiveSupport::TimeZone[to_s].present? end |