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.
-
#months ⇒ Object
Returns the value of attribute months.
-
#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 |
# File 'lib/attentive/duration.rb', line 4 def initialize(attributes) super( attributes.fetch(:years, 0), attributes.fetch(:months, 0), attributes.fetch(:days, 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 |
#months ⇒ Object
Returns the value of attribute months
2 3 4 |
# File 'lib/attentive/duration.rb', line 2 def months @months 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
11 12 13 14 15 16 |
# File 'lib/attentive/duration.rb', line 11 def +(other) self.class.new( years: years + other.years, months: months + other.months, days: days + other.days) end |
#after(date) ⇒ Object
26 27 28 |
# File 'lib/attentive/duration.rb', line 26 def after(date) (date >> (years * 12 + months)) + days end |
#before(date) ⇒ Object
30 31 32 |
# File 'lib/attentive/duration.rb', line 30 def before(date) (date >> -(years * 12 + months)) - days end |
#inspect ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/attentive/duration.rb', line 18 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.join(" ")}>" end |