Class: WorkingHours::Duration
- Inherits:
-
Object
- Object
- WorkingHours::Duration
- Includes:
- Computation
- 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.
Methods included from Computation
#add_days, #add_hours, #add_minutes, #add_seconds, #advance_to_working_time, #in_working_hours?, #return_to_working_time, #working_day?, #working_days_between, #working_time_between
Constructor Details
#initialize(value, kind) ⇒ Duration
Returns a new instance of Duration.
12 13 14 15 16 |
# File 'lib/working_hours/duration.rb', line 12 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.
8 9 10 |
# File 'lib/working_hours/duration.rb', line 8 def kind @kind end |
#value ⇒ Object
Returns the value of attribute value.
8 9 10 |
# File 'lib/working_hours/duration.rb', line 8 def value @value end |
Instance Method Details
#-@ ⇒ Object
Value object methods
30 31 32 |
# File 'lib/working_hours/duration.rb', line 30 def -@ Duration.new(-value, kind) end |
#==(other) ⇒ Object Also known as: eql?
34 35 36 |
# File 'lib/working_hours/duration.rb', line 34 def ==(other) self.class == other.class and kind == other.kind and value == other.value end |
#hash ⇒ Object
39 40 41 |
# File 'lib/working_hours/duration.rb', line 39 def hash [self.class, kind, value].hash end |
#since(time = ::Time.current) ⇒ Object Also known as: from_now
24 25 26 |
# File 'lib/working_hours/duration.rb', line 24 def since(time = ::Time.current) send("add_#{@kind}", time, @value) end |
#until(time = ::Time.current) ⇒ Object Also known as: ago
Computation methods
19 20 21 |
# File 'lib/working_hours/duration.rb', line 19 def until(time = ::Time.current) send("add_#{@kind}", time, -@value) end |