Class: Periodical::Duration
- Inherits:
-
Object
- Object
- Periodical::Duration
- Defined in:
- lib/periodical/duration.rb
Instance Attribute Summary collapse
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
Instance Method Summary collapse
-
#/(period) ⇒ Object
Calculate the number of periods between from and to.
- #days ⇒ Object
-
#initialize(from, to) ⇒ Duration
constructor
A new instance of Duration.
- #months ⇒ Object
- #weeks ⇒ Object
- #whole_months ⇒ Object
- #whole_years ⇒ Object
- #years ⇒ Object
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
#from ⇒ Object (readonly)
Returns the value of attribute from.
30 31 32 |
# File 'lib/periodical/duration.rb', line 30 def from @from end |
#to ⇒ Object (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 |
#days ⇒ Object
33 34 35 |
# File 'lib/periodical/duration.rb', line 33 def days @to - @from end |
#months ⇒ Object
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 |
#weeks ⇒ Object
37 38 39 |
# File 'lib/periodical/duration.rb', line 37 def weeks days / 7 end |
#whole_months ⇒ Object
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_years ⇒ Object
54 55 56 |
# File 'lib/periodical/duration.rb', line 54 def whole_years @to.year - @from.year end |
#years ⇒ Object
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 |