Class: Datebox::Period
- Inherits:
-
Object
- Object
- Datebox::Period
- Defined in:
- lib/datebox/period.rb
Constant Summary collapse
- PREDEFINED =
[:day, :n_days, :week, :month, :year]
Instance Attribute Summary collapse
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
Class Method Summary collapse
- .split_dates(start_date, end_date, period, options = {}) ⇒ Object
- .split_days_dates(start_date, end_date, options = {}) ⇒ Object
- .split_monthly_dates(start_date, end_date) ⇒ Object
- .split_weekly_dates(start_date, end_date, options = {}) ⇒ Object
- .split_yearly_dates(start_date, end_date) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #dates ⇒ Object
-
#initialize(from, to) ⇒ Period
constructor
A new instance of Period.
- #split_dates(period, options = {}) ⇒ Object
Constructor Details
#initialize(from, to) ⇒ Period
Returns a new instance of Period.
7 8 9 10 11 |
# File 'lib/datebox/period.rb', line 7 def initialize(from, to) @from = from.is_a?(Date) ? from : Date.parse(from) @to = to.is_a?(Date) ? to : Date.parse(to) raise "FROM date should not be later than TO date" if @to < @from end |
Instance Attribute Details
#from ⇒ Object (readonly)
Returns the value of attribute from.
3 4 5 |
# File 'lib/datebox/period.rb', line 3 def from @from end |
#to ⇒ Object (readonly)
Returns the value of attribute to.
3 4 5 |
# File 'lib/datebox/period.rb', line 3 def to @to end |
Class Method Details
.split_dates(start_date, end_date, period, options = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/datebox/period.rb', line 27 def split_dates(start_date, end_date, period, = {}) period = period.to_sym return (start_date..end_date).to_a if period == :day return split_days_dates(start_date, end_date, ) if period == :n_days return split_weekly_dates(start_date, end_date, ) if period == :week return split_monthly_dates(start_date, end_date) if period == :month return split_yearly_dates(start_date, end_date) if period == :year end |
.split_days_dates(start_date, end_date, options = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/datebox/period.rb', line 37 def split_days_dates(start_date, end_date, = {}) days = [:days] || ['days'] raise "days must be specified" if days.nil? end_dates = [] (start_date..end_date).to_a.reverse.each_slice(days) do |range| end_dates << range.first end end_dates.sort end |
.split_monthly_dates(start_date, end_date) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/datebox/period.rb', line 59 def split_monthly_dates(start_date, end_date) end_dates = [] beginning_of_month = ::Date.parse("#{end_date.year}-#{end_date.month}-01").next_month end_of_month = (beginning_of_month - 1 == end_date) ? end_date : beginning_of_month.prev_month - 1 while beginning_of_month.prev_month >= start_date end_dates << end_of_month beginning_of_month = ::Date.parse("#{end_of_month.year}-#{end_of_month.month}-01") end_of_month = beginning_of_month - 1 end end_dates.sort end |
.split_weekly_dates(start_date, end_date, options = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/datebox/period.rb', line 48 def split_weekly_dates(start_date, end_date, = {}) last_weekday = [:last_weekday] || ['last_weekday'] || 'Sunday' end_dates = [] end_of_week = (end_date.downto end_date - 6).to_a.find { |d| d.strftime("%A") == last_weekday } while end_of_week - 6 >= start_date end_dates << end_of_week end_of_week -= 7 end end_dates.sort end |
.split_yearly_dates(start_date, end_date) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/datebox/period.rb', line 71 def split_yearly_dates(start_date, end_date) end_dates = [] beginning_of_year = ::Date.parse("#{end_date.year}-01-01").next_year end_of_year = (beginning_of_year - 1 == end_date) ? end_date : beginning_of_year.prev_year - 1 while beginning_of_year.prev_year >= start_date end_dates << end_of_year beginning_of_year = ::Date.parse("#{end_of_year.year}-01-01") end_of_year = beginning_of_year - 1 end end_dates.sort end |
Instance Method Details
#==(other) ⇒ Object
17 18 19 |
# File 'lib/datebox/period.rb', line 17 def ==(other) @from == other.from && @to == other.to end |
#dates ⇒ Object
13 14 15 |
# File 'lib/datebox/period.rb', line 13 def dates (@from..@to).to_a end |
#split_dates(period, options = {}) ⇒ Object
21 22 23 24 |
# File 'lib/datebox/period.rb', line 21 def split_dates(period, = {}) raise "Expected one of: #{Period::PREDEFINED}" unless Period::PREDEFINED.include?(period.to_sym) self.class.split_dates(from, to, period.to_sym, ) end |