Class: Cronofy::DateOrTime
- Inherits:
-
Object
- Object
- Cronofy::DateOrTime
- Defined in:
- lib/cronofy/types.rb
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #date ⇒ Object
- #date? ⇒ Boolean
-
#initialize(args) ⇒ DateOrTime
constructor
A new instance of DateOrTime.
- #inspect ⇒ Object
- #time ⇒ Object
- #time? ⇒ Boolean
- #to_date ⇒ Object
- #to_s ⇒ Object
- #to_time ⇒ Object
Constructor Details
#initialize(args) ⇒ DateOrTime
Returns a new instance of DateOrTime.
109 110 111 112 113 114 115 116 |
# File 'lib/cronofy/types.rb', line 109 def initialize(args) # Prefer time if both provided as it is more accurate if args[:time] @time = args[:time] else @date = args[:date] end end |
Class Method Details
.coerce(value) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/cronofy/types.rb', line 118 def self.coerce(value) begin time = ISO8601Time.coerce(value) rescue begin date = Date.strptime(value, '%Y-%m-%d') rescue end end coerced = self.new(time: time, date: date) raise "Failed to coerce \"#{value}\"" unless coerced.time? or coerced.date? coerced end |
Instance Method Details
#==(other) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/cronofy/types.rb', line 168 def ==(other) case other when DateOrTime if self.time? other.time? and self.time == other.time elsif self.date? other.date? and self.date == other.date else # Both neither date nor time self.time? == other.time? and self.date? == other.date? end else false end end |
#date ⇒ Object
135 136 137 |
# File 'lib/cronofy/types.rb', line 135 def date @date end |
#date? ⇒ Boolean
139 140 141 |
# File 'lib/cronofy/types.rb', line 139 def date? !!@date end |
#inspect ⇒ Object
184 185 186 |
# File 'lib/cronofy/types.rb', line 184 def inspect to_s end |
#time ⇒ Object
143 144 145 |
# File 'lib/cronofy/types.rb', line 143 def time @time end |
#time? ⇒ Boolean
147 148 149 |
# File 'lib/cronofy/types.rb', line 147 def time? !!@time end |
#to_date ⇒ Object
151 152 153 154 155 156 157 |
# File 'lib/cronofy/types.rb', line 151 def to_date if date? date else time.to_date end end |
#to_s ⇒ Object
188 189 190 191 192 193 194 195 196 |
# File 'lib/cronofy/types.rb', line 188 def to_s if time? "<#{self.class} time=#{self.time}>" elsif date? "<#{self.class} date=#{self.date}>" else "<#{self.class} empty>" end end |
#to_time ⇒ Object
159 160 161 162 163 164 165 166 |
# File 'lib/cronofy/types.rb', line 159 def to_time if time? time else # Convert dates to UTC time, not local time Time.utc(date.year, date.month, date.day) end end |