Class: Appstats::DateRange

Inherits:
Object
  • Object
show all
Defined in:
lib/appstats/date_range.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ DateRange

Returns a new instance of DateRange.



7
8
9
10
11
# File 'lib/appstats/date_range.rb', line 7

def initialize(data = {})
  @from = data[:from]
  @to = data[:to]
  @format = data[:format] || :inclusive
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



5
6
7
# File 'lib/appstats/date_range.rb', line 5

def format
  @format
end

#fromObject

Returns the value of attribute from.



5
6
7
# File 'lib/appstats/date_range.rb', line 5

def from
  @from
end

#toObject

Returns the value of attribute to.



5
6
7
# File 'lib/appstats/date_range.rb', line 5

def to
  @to
end

Class Method Details

.parse(raw_input) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/appstats/date_range.rb', line 84

def self.parse(raw_input)
  range = DateRange.new
  return range if raw_input.nil? || raw_input == ''
  input = raw_input.strip
  
  m = input.match(/^today|yesterday|YTD|ytd$/)
  unless m.nil?
    range.from = EntryDate.parse(input)
    range.format = :fixed_point
  end
  
  m = input.match(/^between\s*(.*)\s*and\s*(.*)$/)
  unless m.nil?
    range.from = EntryDate.parse(m[1])
    range.to = EntryDate.parse(m[2])
    return range
  end
  
  m = input.match(/^(in|on)\s*(.*)$/)
  unless m.nil?
    range.from = EntryDate.parse(m[2])
    range.format = :fixed_point
    return range
  end

  m = input.match(/^before\s*(.*)$/)
  unless m.nil?
    range.to = EntryDate.parse(m[1])
    range.format = :exclusive
    return range
  end

  m = input.match(/^after\s*(.*)$/)
  unless m.nil?
    range.from = EntryDate.parse(m[1])
    range.format = :exclusive
    return range
  end

  m = input.match(/^since\s*(.*)$/)
  unless m.nil?
    range.from = EntryDate.parse(m[1])
    range.format = :inclusive
    return range
  end


  m = input.match(/^this\s*(year|quarter|month|week|day)$/)
  unless m.nil?
    range.from = EntryDate.parse(input)
    range.format = :fixed_point
    return range
  end

  m = input.match(/^(last|previous)\s*(year|quarter|month|week|day)$/)
  unless m.nil?
    range.from = EntryDate.parse(input)
    range.format = :fixed_point
    return range
  end

  m = input.match(/^last\s*(.+)\s*(year|years|quarter|quarters|month|months|week|weeks|day|days)$/)
  unless m.nil?
    range.from = EntryDate.parse(input)
    range.format = :inclusive
    return range
  end

  m = input.match(/^(.+)\s*(year|years|quarter|quarters|month|months|week|weeks|day|days)\s*ago$/)
  unless m.nil?
    range.from = EntryDate.parse(input)
    range.format = :fixed_point
    return range
  end


  m = input.match(/^previous\s*(.+)\s*(year|quarter|month|week|day)s?$/)
  unless m.nil?
    range.from = EntryDate.parse(input)
    to = EntryDate.parse("last #{m[2]}")
    if m[2] == "week"
      to = to.end_of_week
    elsif m[2] == "quarter"
      to = to.end_of_quarter  
    end
    range.to = to
    range.format = :inclusive
    return range
  end
  
  range
end

Instance Method Details

#==(o) ⇒ Object Also known as: eql?



177
178
179
# File 'lib/appstats/date_range.rb', line 177

def ==(o)
   o.class == self.class && o.send(:state) == state
end

#from_dateObject



13
14
15
16
17
# File 'lib/appstats/date_range.rb', line 13

def from_date
  return nil if @from.nil?
  mode = @format == :exclusive ? :end : :beginning
  @from.to_time(mode)
end

#from_date_to_sObject



28
29
30
31
# File 'lib/appstats/date_range.rb', line 28

def from_date_to_s
  return nil if from_date.nil?
  from_date.strftime('%Y-%m-%d %H:%M:%S')
end

#to_dateObject



19
20
21
22
23
24
25
26
# File 'lib/appstats/date_range.rb', line 19

def to_date
  if @format == :fixed_point && !@from.nil?
    return @from.to_time(:end)
  end
  return nil if @to.nil?
  mode = @format == :exclusive ? :beginning : :end
  @to.to_time(mode)
end

#to_date_to_sObject



33
34
35
36
# File 'lib/appstats/date_range.rb', line 33

def to_date_to_s
  return nil if to_date.nil?
  to_date.strftime('%Y-%m-%d %H:%M:%S')
end

#to_sqlObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/appstats/date_range.rb', line 38

def to_sql
  return "1=1" if @from.nil? && @to.nil?
  
  if !@from.nil? && @to.nil?
    return case @format
      when :inclusive then 
        "occurred_at >= '#{from_date_to_s}'"
      when :exclusive then 
        "occurred_at > '#{from_date_to_s}'"
      when :fixed_point then 
        if !@from.quarter.nil? 
          answer = "(year=#{@from.year} and quarter=#{@from.quarter})"
        elsif !@from.week.nil? && @from.week == -1
          answer = "((year=#{@from.year - 1} and week=#{EntryDate.calculate_week_of(@from.to_time.beginning_of_week)}) or (year=#{@from.year} and week=#{@from.week}))"
        elsif !@from.week.nil? && @from.to_time.end_of_week.year != @from.year
          answer = "((year=#{@from.year} and week=#{@from.week}) or (year=#{@from.year+1} and week=-1))"
        elsif !@from.week.nil?
          answer = "(year=#{@from.year} and week=#{@from.week})"
        else
          answer = "("
          [:year,:month,:day,:hour,:min,:sec].each do |t|
            next if @from.send(t).nil?
            answer += " and " unless answer.size == 1
            answer += "#{t}=#{from.send(t)}"
          end
          answer += ")"
        end
        answer 
    end
  elsif @from.nil? && !@to.nil?
    return case @format
      when :inclusive then "occurred_at <= '#{to_date_to_s}'"
      when :exclusive then "occurred_at < '#{to_date_to_s}'"
      else "1=1"
    end
  else
    return case @format
      when :inclusive then "(occurred_at >= '#{from_date_to_s}' and occurred_at <= '#{to_date_to_s}')"
      when :exclusive then "(occurred_at > '#{from_date_to_s}' and occurred_at < '#{to_date_to_s}')"
      else "1=1"
    end
  end
  
  
end