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
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

#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

#hoursObject

Returns the value of attribute hours

Returns:

  • (Object)

    the current value of hours



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

def hours
  @hours
end

#minutesObject

Returns the value of attribute minutes

Returns:

  • (Object)

    the current value of minutes



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

def minutes
  @minutes
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

#secondsObject

Returns the value of attribute seconds

Returns:

  • (Object)

    the current value of seconds



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

def seconds
  @seconds
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



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

#inspectObject



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