Class: Datebox::Period

Inherits:
Object
  • Object
show all
Defined in:
lib/datebox/period.rb

Constant Summary collapse

PREDEFINED =
[:day, :n_days, :week, :month, :year]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#fromObject (readonly)

Returns the value of attribute from.



3
4
5
# File 'lib/datebox/period.rb', line 3

def from
  @from
end

#toObject (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, options = {})
  period = period.to_sym
  
  return (start_date..end_date).to_a                        if period == :day
  return split_days_dates(start_date, end_date, options)    if period == :n_days
  return split_weekly_dates(start_date, end_date, options)  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, options = {})
  days = options[:days] || options['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, options = {})
  last_weekday = options[:last_weekday] || options['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

#datesObject



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, options = {})
  raise "Expected one of: #{Period::PREDEFINED}" unless Period::PREDEFINED.include?(period.to_sym)
  self.class.split_dates(from, to, period.to_sym, options)
end