Class: Attentive::Duration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#daysObject

Returns the value of attribute days

Returns:

  • (Object)

    the current value of days



2
3
4
# File 'lib/attentive/duration.rb', line 2

def days
  @days
end

#monthsObject

Returns the value of attribute months

Returns:

  • (Object)

    the current value of months



2
3
4
# File 'lib/attentive/duration.rb', line 2

def months
  @months
end

#yearsObject

Returns the value of attribute years

Returns:

  • (Object)

    the current value of 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

#inspectObject



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