Class: Periodical::Duration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fromObject

Returns the value of attribute from

Returns:

  • (Object)

    the current value of from



24
25
26
# File 'lib/periodical/duration.rb', line 24

def from
  @from
end

#toObject

Returns the value of attribute to

Returns:

  • (Object)

    the current value of to



24
25
26
# File 'lib/periodical/duration.rb', line 24

def to
  @to
end

Instance Method Details

#/(period) ⇒ Object

Calculate the number of periods between from and to



60
61
62
# File 'lib/periodical/duration.rb', line 60

def / period
	self.send(period.unit) / period.count
end

#daysObject



25
26
27
# File 'lib/periodical/duration.rb', line 25

def days
	to - from
end

#monthsObject



37
38
39
40
41
42
43
44
# File 'lib/periodical/duration.rb', line 37

def months
	whole = self.whole_months
	
	partial_start = from >> whole
	partial_end = from >> whole + 1
	
	return whole + (to - partial_start) / (partial_end - partial_start)
end

#weeksObject



29
30
31
# File 'lib/periodical/duration.rb', line 29

def weeks
	days / 7
end

#whole_monthsObject



33
34
35
# File 'lib/periodical/duration.rb', line 33

def whole_months
	(to.year * 12 + to.month) - (from.year * 12 + from.month)
end

#whole_yearsObject



46
47
48
# File 'lib/periodical/duration.rb', line 46

def whole_years
	to.year - from.year
end

#yearsObject



50
51
52
53
54
55
56
57
# File 'lib/periodical/duration.rb', line 50

def years
	whole = self.whole_years
	
	partial_start = from >> (whole * 12)
	partial_end = from >> ((whole + 1) * 12)
	
	return whole + (to - partial_start) / (partial_end - partial_start)
end