Class: Brut::FrontEnd::Forms::Input::TimeOfDay
- Inherits:
-
Object
- Object
- Brut::FrontEnd::Forms::Input::TimeOfDay
- Includes:
- Comparable
- Defined in:
- lib/brut/front_end/forms/input.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#-(other) ⇒ Object
-
#<=>(other) ⇒ Object
-
#initialize(hour:, minute:, second:) ⇒ TimeOfDay
constructor
A new instance of TimeOfDay.
-
#to_s ⇒ Object
-
#to_seconds ⇒ Object
Constructor Details
#initialize(hour:, minute:, second:) ⇒ TimeOfDay
Returns a new instance of TimeOfDay.
297 298 299 300 301 |
# File 'lib/brut/front_end/forms/input.rb', line 297 def initialize(hour:, minute:, second:) @hour = in_range!(hour, 0, 23, "hour") @minute = in_range!(minute, 0, 59, "minute") @second = second.nil? ? nil : in_range!(second, 0, 59, "second") end |
Class Method Details
.from_string(string) ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/brut/front_end/forms/input.rb', line 285 def self.from_string(string) match_data = string.match(/\A([01]?[0-9]|2[0-3]):([0-5][0-9])(:([0-5][0-9]))?\z/) if match_data hour = match_data[1].to_i minute = match_data[2].to_i second = match_data[4] ? match_data[4].to_i : nil self.new(hour:, minute:, second:) else nil end end |
Instance Method Details
#-(other) ⇒ Object
317 318 319 320 321 322 323 |
# File 'lib/brut/front_end/forms/input.rb', line 317 def -(other) if other.kind_of?(self.class) self.to_seconds - other.to_seconds else raise ArgumentError, "Cannot subtract #{other.class} from TimeOfDay" end end |
#<=>(other) ⇒ Object
325 326 327 328 |
# File 'lib/brut/front_end/forms/input.rb', line 325 def <=>(other) return -1 if !other.kind_of?(self.class) self.to_seconds <=> other.to_seconds end |
#to_s ⇒ Object
303 304 305 306 307 308 309 |
# File 'lib/brut/front_end/forms/input.rb', line 303 def to_s if @second.nil? sprintf("%02d:%02d", @hour, @minute) else sprintf("%02d:%02d:%02d", @hour, @minute, @second) end end |
#to_seconds ⇒ Object
311 312 313 314 315 |
# File 'lib/brut/front_end/forms/input.rb', line 311 def to_seconds (@second || 0) + (@minute * 60) + (@hour * 3600) end |