Class: Attentive::Duration
- Inherits:
-
Struct
- Object
- Struct
- Attentive::Duration
- Defined in:
- lib/attentive/duration.rb
Instance Attribute Summary collapse
-
#days ⇒ Object
Returns the value of attribute days.
-
#hours ⇒ Object
Returns the value of attribute hours.
-
#minutes ⇒ Object
Returns the value of attribute minutes.
-
#months ⇒ Object
Returns the value of attribute months.
-
#seconds ⇒ Object
Returns the value of attribute seconds.
-
#years ⇒ Object
Returns the value of attribute years.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #after(date) ⇒ Object
- #before(date) ⇒ Object
-
#initialize(attributes) ⇒ Duration
constructor
A new instance of Duration.
- #inspect ⇒ Object
Constructor Details
#initialize(attributes) ⇒ Duration
Returns a new instance of Duration.
4 5 6 7 8 9 10 11 12 |
# File 'lib/attentive/duration.rb', line 4 def initialize(attributes) super( attributes.fetch(:years, 0), attributes.fetch(:months, 0), attributes.fetch(:days, 0), attributes.fetch(:hours, 0), attributes.fetch(:minutes, 0), attributes.fetch(:seconds, 0)) end |
Instance Attribute Details
#days ⇒ Object
Returns the value of attribute days
2 3 4 |
# File 'lib/attentive/duration.rb', line 2 def days @days end |
#hours ⇒ Object
Returns the value of attribute hours
2 3 4 |
# File 'lib/attentive/duration.rb', line 2 def hours @hours end |
#minutes ⇒ Object
Returns the value of attribute minutes
2 3 4 |
# File 'lib/attentive/duration.rb', line 2 def minutes @minutes end |
#months ⇒ Object
Returns the value of attribute months
2 3 4 |
# File 'lib/attentive/duration.rb', line 2 def months @months end |
#seconds ⇒ Object
Returns the value of attribute seconds
2 3 4 |
# File 'lib/attentive/duration.rb', line 2 def seconds @seconds end |
#years ⇒ Object
Returns the value of attribute years
2 3 4 |
# File 'lib/attentive/duration.rb', line 2 def years @years end |
Instance Method Details
#+(other) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/attentive/duration.rb', line 14 def +(other) self.class.new( years: years + other.years, months: months + other.months, days: days + other.days, hours: hours + other.hours, minutes: minutes + other.minutes, seconds: seconds + other.seconds) end |
#after(date) ⇒ Object
35 36 37 |
# File 'lib/attentive/duration.rb', line 35 def after(date) (date >> (years * 12 + months)) + days end |
#before(date) ⇒ Object
39 40 41 |
# File 'lib/attentive/duration.rb', line 39 def before(date) (date >> -(years * 12 + months)) - days end |
#inspect ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/attentive/duration.rb', line 24 def inspect phrases = [] phrases.push "#{years} years" if years > 0 phrases.push "#{months} months" if months > 0 phrases.push "#{days} days" if days > 0 phrases.push "#{hours} hours" if hours > 0 phrases.push "#{minutes} minutes" if minutes > 0 phrases.push "#{seconds} seconds" if seconds > 0 "<#{phrases.join(" ")}>" end |