Class: Periodical::Duration
- Inherits:
-
Struct
- Object
- Struct
- Periodical::Duration
- Defined in:
- lib/periodical/duration.rb
Instance Attribute Summary collapse
-
#from ⇒ Object
Returns the value of attribute from.
-
#to ⇒ Object
Returns the value of attribute to.
Instance Method Summary collapse
-
#/(period) ⇒ Object
Calculate the number of periods between from and to.
- #days ⇒ Object
- #months ⇒ Object
- #weeks ⇒ Object
- #whole_months ⇒ Object
- #whole_years ⇒ Object
- #years ⇒ Object
Instance Attribute Details
#from ⇒ Object
Returns the value of attribute from
24 25 26 |
# File 'lib/periodical/duration.rb', line 24 def from @from end |
#to ⇒ Object
Returns the value of attribute 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 |
#days ⇒ Object
25 26 27 |
# File 'lib/periodical/duration.rb', line 25 def days to - from end |
#months ⇒ Object
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 |
#weeks ⇒ Object
29 30 31 |
# File 'lib/periodical/duration.rb', line 29 def weeks days / 7 end |
#whole_months ⇒ Object
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_years ⇒ Object
46 47 48 |
# File 'lib/periodical/duration.rb', line 46 def whole_years to.year - from.year end |
#years ⇒ Object
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 |