Class: ForestLiana::OperatorDateIntervalParser

Inherits:
Object
  • Object
show all
Defined in:
app/services/forest_liana/operator_date_interval_parser.rb

Constant Summary collapse

PERIODS =
{
  yesterday: { duration: 1, period: 'day' },
  lastWeek: { duration: 1, period: 'week' },
  last2Weeks: { duration: 2, period: 'week' },
  lastMonth: { duration: 1, period: 'month' },
  last3Months: { duration: 3, period: 'month' },
  lastYear: { duration: 1, period: 'year' }
}
PERIODS_LAST_X_DAYS =
/^last(\d+)days$/

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ OperatorDateIntervalParser

Returns a new instance of OperatorDateIntervalParser.



14
15
16
# File 'app/services/forest_liana/operator_date_interval_parser.rb', line 14

def initialize(value)
  @value = value
end

Instance Method Details

#get_interval_date_filterObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/services/forest_liana/operator_date_interval_parser.rb', line 28

def get_interval_date_filter
  return nil unless is_interval_date_value()

  match = PERIODS_LAST_X_DAYS.match(@value)
  return ">= '#{Integer(match[1]).day.ago}'" if match && match[1]

  duration = PERIODS[@value.to_sym][:duration]
  period = PERIODS[@value.to_sym][:period]

  from = duration.send(period).ago.send("beginning_of_#{period}")
  to = 1.send(period).ago.send("end_of_#{period}")
  "BETWEEN '#{from}' AND '#{to}'"
end

#get_interval_date_filter_for_previous_intervalObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/services/forest_liana/operator_date_interval_parser.rb', line 42

def get_interval_date_filter_for_previous_interval
  return nil unless is_interval_date_value()

  match = PERIODS_LAST_X_DAYS.match(@value)
  if match && match[1]
    return "BETWEEN #{Integer(match[1] * 2).day.ago}" +
      " AND #{Integer(match[1]).day.ago}"
  end

  duration = PERIODS[@value.to_sym][:duration]
  period = PERIODS[@value.to_sym][:period]

  from = (duration * 2).send(period).ago.send("beginning_of_#{period}")
  to = (1 + duration).send(period).ago.send("end_of_#{period}")
  "BETWEEN '#{from}' AND '#{to}'"
end

#is_interval_date_valueObject



18
19
20
21
22
23
24
25
26
# File 'app/services/forest_liana/operator_date_interval_parser.rb', line 18

def is_interval_date_value
  return false if @value.nil?
  return true if PERIODS[@value.to_sym]

  match = PERIODS_LAST_X_DAYS.match(@value)
  return true if match && match[1]

  false
end