Class: Datebox::Period
- Inherits:
-
Object
- Object
- Datebox::Period
- Defined in:
- lib/datebox/period.rb
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_monthly_dates(start_date, end_date) ⇒ Object
- .split_weekly_dates(start_date, end_date, options = {}) ⇒ 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.
5 6 7 8 9 |
# File 'lib/datebox/period.rb', line 5 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
24 25 26 27 28 29 30 31 |
# File 'lib/datebox/period.rb', line 24 def split_dates(start_date, end_date, period, = {}) return (start_date..end_date).to_a if period == "day" return split_monthly_dates(start_date, end_date) if period == "month" if period =~ /week/ return split_weekly_dates(start_date, end_date, .merge({last_weekday: "Saturday"})) if period == "week_ss" return split_weekly_dates(start_date, end_date, ) end end |
.split_monthly_dates(start_date, end_date) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/datebox/period.rb', line 44 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
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/datebox/period.rb', line 33 def split_weekly_dates(start_date, end_date, = {}) 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 |
Instance Method Details
#==(other) ⇒ Object
15 16 17 |
# File 'lib/datebox/period.rb', line 15 def ==(other) @from == other.from && @to == other.to end |
#dates ⇒ Object
11 12 13 |
# File 'lib/datebox/period.rb', line 11 def dates (@from..@to).to_a end |
#split_dates(period, options = {}) ⇒ Object
19 20 21 |
# File 'lib/datebox/period.rb', line 19 def split_dates(period, = {}) self.class.split_dates(from, to, period, ) end |