class Datebox::Relative

Attributes

period_name[R]

Public Class Methods

day_apart(difference) click to toggle source
# File lib/datebox/relative.rb, line 52
def day_apart(difference)
  new Proc.new {|relative_to| Period.new(relative_to + difference, relative_to + difference) }, __method__.to_s
end
day_before() click to toggle source

for backwards compatibility, we had only 'day_before' but last_day is more consistent with other calls @deprecated

# File lib/datebox/relative.rb, line 50
def day_before; last_day; end
last(period, options = {}) click to toggle source
# File lib/datebox/relative.rb, line 19
def last(period, options = {})
  raise "Expected one of: #{Period::PREDEFINED}" unless Period::PREDEFINED.include?(period.to_sym)
  case period.to_sym
    when :day then last_day
    when :n_days then last_n_days(options)
    when :week then last_week(options)
    when :month then last_month
    when :year then last_year
  end
end
last_day() click to toggle source
# File lib/datebox/relative.rb, line 44
def last_day
  new Proc.new {|relative_to| Period.new(relative_to - 1, relative_to - 1) }, __method__.to_s
end
last_month() click to toggle source
# File lib/datebox/relative.rb, line 130
def last_month
  new Proc.new { |relative_to|
    previous_month_start = Date.parse("#{relative_to.prev_month.strftime('%Y-%m')}-01")
    previous_month_end = previous_month_start.next_month - 1
    Period.new(previous_month_start, previous_month_end)
  }, __method__.to_s
end
last_n_days(options = {}) click to toggle source
# File lib/datebox/relative.rb, line 71
def last_n_days(options = {})
  days = (options[:days] || options['days']).to_i
  inclusive = (options[:inclusive] || options['inclusive']) ? true : false # NOT inclusive by default
  days = 1 if days.nil? || days <= 0 # days should always > 0 since it only return last x days
  proc = inclusive ?
    Proc.new {|relative_to| Period.new(relative_to - days + 1, relative_to) } :
    Proc.new {|relative_to| Period.new(relative_to - days, relative_to - 1) }
  new proc, __method__.to_s
end
last_week(options = {}) click to toggle source
# File lib/datebox/relative.rb, line 81
def last_week(options = {})
  last_weekday = options[:last_weekday] || options['last_weekday'] || 'Sunday'
  new Proc.new { |relative_to|
    relative_to -= 1
    end_date = (relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == last_weekday }
    Period.new(end_date - 6, end_date)
  }, __method__.to_s
end
last_weekdays_between(start_day, end_day) click to toggle source
# File lib/datebox/relative.rb, line 90
def last_weekdays_between(start_day, end_day)
  new Proc.new { |relative_to|
    end_date = (relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == end_day }
    start_date = (end_date - 7 .. end_date).to_a.find { |d| d.strftime("%A") == start_day }
    Period.new(start_date, end_date)
  }, __method__.to_s
end
last_weeks_weekdays_as!(*days) click to toggle source
# File lib/datebox/relative.rb, line 98
def last_weeks_weekdays_as!(*days) #this one returns array!
  new Proc.new { |relative_to|
    days.map do |p|
      (relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == p }
    end
  }, __method__.to_s
end
last_year() click to toggle source
# File lib/datebox/relative.rb, line 153
def last_year
  new Proc.new { |relative_to|
    previous_year_start = Date.parse("#{relative_to.prev_year.year}-01-01")
    previous_year_end = previous_year_start.next_year - 1
    Period.new(previous_year_start, previous_year_end)
  }, __method__.to_s
end
method_missing(m, *args, &block) click to toggle source
Calls superclass method
# File lib/datebox/relative.rb, line 56
def method_missing(m, *args, &block)
  if (m.to_s =~ /^day_ago_\d+$/) or (m.to_s =~ /^day_in_\d+$/)
    days = m.to_s.split('_').last.to_i
    if m.to_s =~ /^day_ago_\d+$/
      return new Proc.new {|relative_to| Period.new(relative_to - days, relative_to - days) }, m
    elsif m.to_s =~ /^day_in_\d+$/
      return new Proc.new {|relative_to| Period.new(relative_to + days, relative_to + days) }, m
    end
    relative.send(:period_name, m)
    return relative
  else
    super
  end
end
month_to_date() click to toggle source
# File lib/datebox/relative.rb, line 138
def month_to_date
  new Proc.new { |relative_to|
    month_start = Date.parse("#{relative_to.strftime('%Y-%m')}-01")
    Period.new(month_start, relative_to)
  }, __method__.to_s
end
new(proc, name = nil) click to toggle source
# File lib/datebox/relative.rb, line 7
def initialize(proc, name = nil)
  @period_proc = proc
  @period_name = name
end
same_day() click to toggle source
# File lib/datebox/relative.rb, line 40
def same_day
  new Proc.new {|relative_to| Period.new(relative_to, relative_to) }, __method__.to_s
end
same_month() click to toggle source
# File lib/datebox/relative.rb, line 145
def same_month
  new Proc.new { |relative_to|
    same_month_start = Date.parse("#{relative_to.strftime('%Y-%m')}-01")
    same_month_end = same_month_start.next_month - 1
    Period.new(same_month_start, same_month_end)
  }, __method__.to_s
end
same_week(options = {}) click to toggle source
# File lib/datebox/relative.rb, line 118
def same_week(options = {})
  last_weekday = options[:last_weekday] || options['last_weekday'] || 'Sunday'
  new Proc.new { |relative_to|
    if relative_to.strftime("%A") == last_weekday
      Period.new(relative_to - 6, relative_to)
    else
      end_date = (relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == last_weekday }
      Period.new(end_date + 1, end_date + 7)
    end
  }, __method__.to_s
end
to_date(period, options = {}) click to toggle source
# File lib/datebox/relative.rb, line 30
def to_date(period, options = {})
  raise "Expected one of: #{Period::PREDEFINED}" unless Period::PREDEFINED.include?(period.to_sym)
  raise "Current doesn't make sense for parameter: #{period}" if [:day, :n_days].include?(period)
  case period.to_sym
    when :week then week_to_date(options)
    when :month then month_to_date
    when :year then year_to_date
  end
end
week_to_date(options = {}) click to toggle source
# File lib/datebox/relative.rb, line 106
def week_to_date(options = {})
  last_weekday = options[:last_weekday] || options['last_weekday'] || 'Sunday'
  new Proc.new { |relative_to|
    if relative_to.strftime("%A") == last_weekday
      Period.new(relative_to - 6, relative_to)
    else
      end_date = (relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == last_weekday }
      Period.new(end_date + 1, relative_to)
    end
  }, __method__.to_s
end
year_to_date() click to toggle source
# File lib/datebox/relative.rb, line 161
def year_to_date
  new Proc.new { |relative_to|
    year_start = Date.parse("#{relative_to.year}-01-01")
    Period.new(year_start, relative_to)
  }, __method__.to_s
end

Public Instance Methods

to(relative_to) click to toggle source
# File lib/datebox/relative.rb, line 12
def to(relative_to)
  relative_to = (relative_to.is_a?(Date) ? relative_to : Date.parse(relative_to))
  @period_proc.call relative_to
end