Class: GBWorkDay::Duration
- Inherits:
-
Object
- Object
- GBWorkDay::Duration
- Includes:
- Comparable
- Defined in:
- lib/gb_work_day/duration.rb
Constant Summary collapse
- SEC_IN_DAY =
86400
Instance Attribute Summary collapse
-
#week ⇒ Object
Returns the value of attribute week.
-
#work_days ⇒ Object
Returns the value of attribute work_days.
Instance Method Summary collapse
-
#+(other) ⇒ Object
:nodoc:.
-
#-(other) ⇒ Object
:nodoc:.
-
#-@ ⇒ Object
:nodoc:.
-
#==(other) ⇒ Object
:nodoc:.
-
#ago(time = ::Time.current) ⇒ Object
(also: #until)
Calculates a new Time or Date that is as far in the past as this Duration represents.
-
#eql?(other) ⇒ Boolean
Returns
true
ifother
is also a Duration instance, which has the same parts as this one. - #in_time_zone(zone) ⇒ Object
-
#initialize(days, week = nil) ⇒ Duration
constructor
A new instance of Duration.
-
#inspect ⇒ Object
:nodoc:.
-
#instance_of?(klass) ⇒ Boolean
:nodoc:.
-
#is_a?(klass) ⇒ Boolean
(also: #kind_of?)
:nodoc:.
-
#since(time = ::Time.current) ⇒ Object
(also: #from_now)
Calculates a new Time or Date that is as far in the future as this Duration represents.
-
#to_i(format = :seconds) ⇒ Object
Returns the number of seconds that this Duration represents.
-
#to_s ⇒ Object
Returns the amount of seconds a duration covers as a string.
Constructor Details
Instance Attribute Details
#week ⇒ Object
Returns the value of attribute week.
7 8 9 |
# File 'lib/gb_work_day/duration.rb', line 7 def week @week end |
#work_days ⇒ Object
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.
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 |
#inspect ⇒ Object
: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:
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:
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_s ⇒ Object
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 |