Class: Datebox::Relative

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proc, name = nil) ⇒ Relative

Returns a new instance of Relative.



7
8
9
10
# File 'lib/datebox/relative.rb', line 7

def initialize(proc, name = nil)
  @period_proc = proc
  @period_name = name
end

Instance Attribute Details

#period_nameObject (readonly)

Returns the value of attribute period_name.



5
6
7
# File 'lib/datebox/relative.rb', line 5

def period_name
  @period_name
end

Class Method Details

.day_beforeObject



44
45
46
# File 'lib/datebox/relative.rb', line 44

def day_before
  new Proc.new {|relative_to| Period.new(relative_to - 1, relative_to - 1) }, __method__.to_s
end

.last(period, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# 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 day_before
    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_monthObject



94
95
96
97
98
99
100
# File 'lib/datebox/relative.rb', line 94

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 = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/datebox/relative.rb', line 48

def last_n_days(options = {})
  days = (options[:days] || options['days']).to_i
  inclusive = (options[:exclusive] || options['exclusive']) ? false : true # 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 = {}) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/datebox/relative.rb', line 58

def last_week(options = {})
  last_weekday = options[:last_weekday] || options['last_weekday'] || 'Sunday'
  new Proc.new { |relative_to|
    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) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/datebox/relative.rb', line 66

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) ⇒ Object

this one returns array!



74
75
76
77
78
79
80
# File 'lib/datebox/relative.rb', line 74

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_yearObject



109
110
111
112
113
114
115
# File 'lib/datebox/relative.rb', line 109

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

.month_to_dateObject



102
103
104
105
106
107
# File 'lib/datebox/relative.rb', line 102

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

.same_dayObject



40
41
42
# 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

.to_date(period, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
# 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 = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/datebox/relative.rb', line 82

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_dateObject



117
118
119
120
121
122
# File 'lib/datebox/relative.rb', line 117

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

Instance Method Details

#to(relative_to) ⇒ Object



12
13
14
15
# 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