Class: Periodical::Duration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to) ⇒ Duration

Returns a new instance of Duration.



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

def initialize(from, to)
	@from = from
	@to = to
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



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

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



31
32
33
# File 'lib/periodical/duration.rb', line 31

def to
  @to
end

Instance Method Details

#/(period) ⇒ Object

Calculate the number of periods between from and to



68
69
70
# File 'lib/periodical/duration.rb', line 68

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

#daysObject



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

def days
	@to - @from
end

#monthsObject



45
46
47
48
49
50
51
52
# File 'lib/periodical/duration.rb', line 45

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



37
38
39
# File 'lib/periodical/duration.rb', line 37

def weeks
	days / 7
end

#whole_monthsObject



41
42
43
# File 'lib/periodical/duration.rb', line 41

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

#whole_yearsObject



54
55
56
# File 'lib/periodical/duration.rb', line 54

def whole_years
	@to.year - @from.year
end

#yearsObject



58
59
60
61
62
63
64
65
# File 'lib/periodical/duration.rb', line 58

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