Class: GBWorkDay::Duration

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/gb_work_day/duration.rb

Constant Summary collapse

SEC_IN_DAY =
86400

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(days, week = nil) ⇒ Duration

Returns a new instance of Duration.



8
9
10
11
# File 'lib/gb_work_day/duration.rb', line 8

def initialize(days, week=nil)
  @work_days = days
  @week = week || WorkWeek.current
end

Instance Attribute Details

#weekObject

Returns the value of attribute week.



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

def week
  @week
end

#work_daysObject

Returns the value of attribute work_days.



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

def work_days
  @work_days
end

Instance Method Details

#+(other) ⇒ Object

:nodoc:



13
14
15
16
17
18
19
# File 'lib/gb_work_day/duration.rb', line 13

def +(other) # :nodoc:
  if Duration === other
    Duration.new(work_days + other.work_days, week)
  else
    Duration.new(work_days + other, week)
  end
end

#-(other) ⇒ Object

:nodoc:



21
22
23
# File 'lib/gb_work_day/duration.rb', line 21

def -(other) # :nodoc:
  self + (-other)
end

#-@Object

:nodoc:



25
26
27
# File 'lib/gb_work_day/duration.rb', line 25

def -@ # :nodoc:
  Duration.new(-work_days, week)
end

#==(other) ⇒ Object

:nodoc:



38
39
40
41
42
43
44
# File 'lib/gb_work_day/duration.rb', line 38

def ==(other) # :nodoc:
  if Duration === other
    other.work_days == work_days && other.week == week
  else
    other == work_days
  end
end

#ago(time = ::Time.current) ⇒ Object Also known as: until

Calculates a new Time or Date that is as far in the past as this Duration represents. Time has to be a work day, it is calculated starting from the next work day



85
86
87
# File 'lib/gb_work_day/duration.rb', line 85

def ago(time = ::Time.current)
  self.work_days > 0 ? subtract(time) : sum(time)
end

#eql?(other) ⇒ Boolean

Returns true if other is also a Duration instance, which has the same parts as this one.

Returns:

  • (Boolean)


66
67
68
# File 'lib/gb_work_day/duration.rb', line 66

def eql?(other)
  Duration === other && other.work_days.eql?(work_days) && other.week.eql?(week)
end

#in_time_zone(zone) ⇒ Object



90
91
92
# File 'lib/gb_work_day/duration.rb', line 90

def in_time_zone(zone)
  self
end

#inspectObject

:nodoc:



70
71
72
# File 'lib/gb_work_day/duration.rb', line 70

def inspect #:nodoc:
  "#{self.work_days} working days in a working week: #{week}"
end

#instance_of?(klass) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


34
35
36
# File 'lib/gb_work_day/duration.rb', line 34

def instance_of?(klass) # :nodoc:
  Duration == klass || work_days.instance_of?(klass)
end

#is_a?(klass) ⇒ Boolean Also known as: kind_of?

:nodoc:

Returns:

  • (Boolean)


29
30
31
# File 'lib/gb_work_day/duration.rb', line 29

def is_a?(klass) # :nodoc:
  Duration == klass || work_days.is_a?(klass)
end

#since(time = ::Time.current) ⇒ Object Also known as: from_now

Calculates a new Time or Date that is as far in the future as this Duration represents. If time is a free day, it is calculated starting from the next work day



77
78
79
# File 'lib/gb_work_day/duration.rb', line 77

def since(time = ::Time.current)
  self.work_days > 0 ? sum(time) : subtract(time)
end

#to_i(format = :seconds) ⇒ Object

Returns the number of seconds that this Duration represents.



55
56
57
58
59
60
61
# File 'lib/gb_work_day/duration.rb', line 55

def to_i(format = :seconds)
  if format == :days
    work_days
  else
    work_days * SEC_IN_DAY
  end
end

#to_sObject

Returns the amount of seconds a duration covers as a string. For more information check to_i method.

1.work_day.to_s # => "86400"


50
51
52
# File 'lib/gb_work_day/duration.rb', line 50

def to_s
  to_i.to_s
end