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.
-
#halfyears ⇒ Halfyear
Returns the halfyears included in this period including the halfyear of the start date of this period.
-
#include?(period) ⇒ Boolean
Test whether the given date or period is included in this one.
-
#initialize(start_date, end_date = nil) ⇒ Object
Initialize a new period with its start and end date.
-
#months ⇒ Month
Returns the months included in this period including the month of the start and end date of this period.
-
#next ⇒ Period
Returns next period.
-
#previous ⇒ Period
Returns previous period.
-
#quarters ⇒ Quarter
Returns the quarters included in this period including the quarter of the start date of this period.
-
#start_year ⇒ Fixnum
Returns start year of period.
-
#to_s ⇒ String
Return string representation of period.
-
#years ⇒ Year
Returns the years included in this period including the year of the start date of this 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.
74 75 76 77 78 79 80 81 82 |
# File 'lib/periods/modules/period.rb', line 74 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.
59 60 61 62 63 64 65 |
# File 'lib/periods/modules/period.rb', line 59 def ==(period) if period.respond_to?(:start_date) && period.respond_to?(:end_date) start_date == period.start_date && end_date == period.end_date else false end end |
#days ⇒ Fixnum
Returns number of days in period.
131 132 133 |
# File 'lib/periods/modules/period.rb', line 131 def days end_date.yday - start_date.yday + 1 end |
#end_year ⇒ Fixnum
Returns end year of period.
161 162 163 |
# File 'lib/periods/modules/period.rb', line 161 def end_year end_date.year end |
#halfyears ⇒ Halfyear
Returns the halfyears included in this period including the halfyear of the start date of this period. The halfyear of the end date of this period is only included if the halfyear’s end date is before/equal the end date of this period.
269 270 271 272 273 274 275 276 277 |
# File 'lib/periods/modules/period.rb', line 269 def halfyears halfyears = [] halfyear = Periods::Halfyear.for(start_date) while halfyear.end_date <= end_date halfyears << halfyear halfyear = halfyear.next end halfyears end |
#include?(period) ⇒ Boolean
Test whether the given date or period is included in this one.
184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/periods/modules/period.rb', line 184 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 = nil) ⇒ Object
Initialize a new period with its start and end date.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/periods/modules/period.rb', line 31 def initialize(start_date, end_date = nil) if start_date.is_a?(Period) @start_date = start_date.start_date @end_date = start_date.end_date elsif end_date.nil? raise ArgumentError, "Period is missing end_date, e.g. Period.new(start_date, end_date)." else @start_date = Date.parse(start_date.to_s) @end_date = Date.parse(end_date.to_s) end end |
#months ⇒ Month
Returns the months included in this period including the month of the start and end date of this period.
225 226 227 |
# File 'lib/periods/modules/period.rb', line 225 def months (Periods::Month.for(start_date)..Periods::Month.for(end_date)).to_a end |
#next ⇒ Period
Returns next period.
98 99 100 |
# File 'lib/periods/modules/period.rb', line 98 def next self.class.new(start_date + days, end_date + days) end |
#previous ⇒ Period
Returns previous period.
116 117 118 |
# File 'lib/periods/modules/period.rb', line 116 def previous self.class.new(start_date - days, end_date - days) end |
#quarters ⇒ Quarter
Returns the quarters included in this period including the quarter of the start date of this period. The quarter of the end date of this period is only included if the quarter’s end date is before/equal the end date of this period.
243 244 245 246 247 248 249 250 251 |
# File 'lib/periods/modules/period.rb', line 243 def quarters quarters = [] quarter = Periods::Quarter.for(start_date) while quarter.end_date <= end_date quarters << quarter quarter = quarter.next end quarters end |
#start_year ⇒ Fixnum
Returns start year of period.
146 147 148 |
# File 'lib/periods/modules/period.rb', line 146 def start_year start_date.year end |
#to_s ⇒ String
Return string representation of period.
208 209 210 |
# File 'lib/periods/modules/period.rb', line 208 def to_s "#{start_date.strftime("%d.%m.%Y")} - #{end_date.strftime("%d.%m.%Y")}" end |
#years ⇒ Year
Returns the years included in this period including the year of the start date of this period. The year of the end date of this period is only included if the year’s end date is before/equal the end date of this period.
295 296 297 298 299 300 301 302 303 |
# File 'lib/periods/modules/period.rb', line 295 def years years = [] year = Periods::Year.for(start_date) while year.end_date <= end_date years << year year = year.next end years end |