Class: WorkingHours::Duration
- Inherits:
-
Object
- Object
- WorkingHours::Duration
- Defined in:
- lib/working_hours/duration.rb
Constant Summary collapse
- SUPPORTED_KINDS =
[:days, :hours, :minutes, :seconds]
Instance Attribute Summary collapse
-
#kind ⇒ Object
Returns the value of attribute kind.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#-@ ⇒ Object
Value object methods.
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(value, kind) ⇒ Duration
constructor
A new instance of Duration.
- #since(time = ::Time.current) ⇒ Object (also: #from_now)
-
#until(time = ::Time.current) ⇒ Object
(also: #ago)
Computation methods.
Constructor Details
#initialize(value, kind) ⇒ Duration
Returns a new instance of Duration.
11 12 13 14 15 |
# File 'lib/working_hours/duration.rb', line 11 def initialize(value, kind) raise ArgumentError.new("Invalid working time unit: #{kind}") unless SUPPORTED_KINDS.include?(kind) @value = value @kind = kind end |
Instance Attribute Details
#kind ⇒ Object
Returns the value of attribute kind.
7 8 9 |
# File 'lib/working_hours/duration.rb', line 7 def kind @kind end |
#value ⇒ Object
Returns the value of attribute value.
7 8 9 |
# File 'lib/working_hours/duration.rb', line 7 def value @value end |
Instance Method Details
#-@ ⇒ Object
Value object methods
29 30 31 |
# File 'lib/working_hours/duration.rb', line 29 def -@ Duration.new(-value, kind) end |
#==(other) ⇒ Object Also known as: eql?
33 34 35 |
# File 'lib/working_hours/duration.rb', line 33 def ==(other) self.class == other.class and kind == other.kind and value == other.value end |
#hash ⇒ Object
38 39 40 |
# File 'lib/working_hours/duration.rb', line 38 def hash [self.class, kind, value].hash end |
#since(time = ::Time.current) ⇒ Object Also known as: from_now
23 24 25 |
# File 'lib/working_hours/duration.rb', line 23 def since(time = ::Time.current) ::WorkingHours.send("add_#{@kind}", time, @value) end |
#until(time = ::Time.current) ⇒ Object Also known as: ago
Computation methods
18 19 20 |
# File 'lib/working_hours/duration.rb', line 18 def until(time = ::Time.current) ::WorkingHours.send("add_#{@kind}", time, -@value) end |