Module: Periods::Modules::Period::InstanceMethods
- Defined in:
- lib/periods/modules/period.rb
Instance Attribute Summary collapse
-
#end_date ⇒ Date
readonly
Returns the end date of this period.
-
#start_date ⇒ Date
readonly
Returns the start date of this period.
Instance Method Summary collapse
-
#<=>(period) ⇒ Fixnum
Test whether the given period is older, newer or equal to this one.
-
#==(period) ⇒ Boolean
Test whether the given period is equal to this one.
-
#days ⇒ Fixnum
Returns number of days in period.
-
#end_year ⇒ Fixnum
Returns end year of period.
-
#include?(period) ⇒ Boolean
Test whether the given date or period is included in this one.
-
#initialize(start_date, end_date) ⇒ Object
Initialize a new period with its start and end date.
-
#next ⇒ Period
Returns next period.
-
#previous ⇒ Period
Returns previous period.
-
#start_year ⇒ Fixnum
Returns start year of period.
-
#to_s ⇒ String
Return string representation of period.
Instance Attribute Details
#end_date ⇒ Date (readonly)
Returns the end date of this period.
20 21 22 |
# File 'lib/periods/modules/period.rb', line 20 def end_date @end_date end |
#start_date ⇒ Date (readonly)
Returns the start date of this period.
16 17 18 |
# File 'lib/periods/modules/period.rb', line 16 def start_date @start_date end |
Instance Method Details
#<=>(period) ⇒ Fixnum
Test whether the given period is older, newer or equal to this one.
61 62 63 64 65 66 67 68 69 |
# File 'lib/periods/modules/period.rb', line 61 def <=>(period) if start_date > period.start_date 1 elsif start_date < period.start_date -1 else 0 end end |
#==(period) ⇒ Boolean
Test whether the given period is equal to this one. A period is considered equal if it has the same start and end date.
50 51 52 |
# File 'lib/periods/modules/period.rb', line 50 def ==(period) start_date == period.start_date && end_date == period.end_date end |
#days ⇒ Fixnum
Returns number of days in period.
118 119 120 |
# File 'lib/periods/modules/period.rb', line 118 def days end_date.yday - start_date.yday + 1 end |
#end_year ⇒ Fixnum
Returns end year of period.
148 149 150 |
# File 'lib/periods/modules/period.rb', line 148 def end_year end_date.year end |
#include?(period) ⇒ Boolean
Test whether the given date or period is included in this one.
171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/periods/modules/period.rb', line 171 def include?(period) if period.is_a?(Date) start_date <= period && period <= end_date elsif period.is_a?(String) date = Date.parse(period.to_s) start_date <= date && date <= end_date elsif period.is_a?(Period) start_date <= period.start_date && period.end_date <= end_date else false end end |
#initialize(start_date, end_date) ⇒ Object
Initialize a new period with its start and end date.
29 30 31 32 |
# File 'lib/periods/modules/period.rb', line 29 def initialize(start_date, end_date) @start_date = Date.parse(start_date.to_s) @end_date = Date.parse(end_date.to_s) end |
#next ⇒ Period
Returns next period.
85 86 87 |
# File 'lib/periods/modules/period.rb', line 85 def next self.class.new(start_date + days, end_date + days) end |
#previous ⇒ Period
Returns previous period.
103 104 105 |
# File 'lib/periods/modules/period.rb', line 103 def previous self.class.new(start_date - days, end_date - days) end |
#start_year ⇒ Fixnum
Returns start year of period.
133 134 135 |
# File 'lib/periods/modules/period.rb', line 133 def start_year start_date.year end |
#to_s ⇒ String
Return string representation of period.
195 196 197 |
# File 'lib/periods/modules/period.rb', line 195 def to_s "#{start_date.strftime("%d.%m.%Y")} - #{end_date.strftime("%d.%m.%Y")}" end |