Class: WorkingHours::Duration

Inherits:
Object
  • Object
show all
Defined in:
lib/working_hours/duration.rb

Constant Summary collapse

SUPPORTED_KINDS =
[:days, :hours, :minutes, :seconds]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, kind) ⇒ Duration

Returns a new instance of Duration.

Raises:

  • (ArgumentError)


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

#kindObject

Returns the value of attribute kind.



7
8
9
# File 'lib/working_hours/duration.rb', line 7

def kind
  @kind
end

#valueObject

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

#hashObject



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