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_apart(difference) ⇒ Object



52
53
54
# 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_beforeObject

Deprecated.

for backwards compatibility, we had only ‘day_before’ but last_day is more consistent with other calls



50
# File 'lib/datebox/relative.rb', line 50

def day_before; last_day; 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 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_dayObject



44
45
46
# 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_monthObject



130
131
132
133
134
135
136
# 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 = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
# 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 = {}) ⇒ Object



81
82
83
84
85
86
87
88
# 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) ⇒ Object



90
91
92
93
94
95
96
# 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) ⇒ Object

this one returns array!



98
99
100
101
102
103
104
# 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_yearObject



153
154
155
156
157
158
159
# 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) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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_dateObject



138
139
140
141
142
143
# 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

.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

.same_monthObject



145
146
147
148
149
150
151
# 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 = {}) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# 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 = {}) ⇒ 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



106
107
108
109
110
111
112
113
114
115
116
# 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_dateObject



161
162
163
164
165
166
# 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

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