Class: ForestLiana::OperatorDateIntervalParser
- Inherits:
-
Object
- Object
- ForestLiana::OperatorDateIntervalParser
- Defined in:
- app/services/forest_liana/operator_date_interval_parser.rb
Constant Summary collapse
- PERIODS =
{ :$yesterday => { duration: 1, period: 'day' }, :$previousWeek => { duration: 1, period: 'week' }, :$previousMonth => { duration: 1, period: 'month' }, :$previousQuarter => { duration: 3, period: 'month', period_of_time: 'quarter' }, :$previousYear => { duration: 1, period: 'year' }, :$weekToDate => { duration: 1, period: 'week', to_date: true }, :$monthToDate => { duration: 1, period: 'month', to_date: true }, :$quarterToDate => { duration: 3, period: 'month', period_of_time: 'quarter', to_date: true }, :$yearToDate => { duration: 1, period: 'year', to_date: true } }
- PERIODS_PAST =
'$past'
- PERIODS_FUTURE =
'$future'
- PERIODS_TODAY =
'$today'
- PERIODS_PREVIOUS_X_DAYS =
/^\$previous(\d+)Days$/
- PERIODS_X_DAYS_TO_DATE =
/^\$(\d+)DaysToDate$/
Instance Method Summary collapse
- #get_interval_date_filter ⇒ Object
- #get_interval_date_filter_for_previous_interval ⇒ Object
- #has_previous_interval ⇒ Object
-
#initialize(value, timezone) ⇒ OperatorDateIntervalParser
constructor
A new instance of OperatorDateIntervalParser.
- #is_interval_date_value ⇒ Object
- #to_client_timezone(date) ⇒ Object
Constructor Details
#initialize(value, timezone) ⇒ OperatorDateIntervalParser
Returns a new instance of OperatorDateIntervalParser.
24 25 26 27 |
# File 'app/services/forest_liana/operator_date_interval_parser.rb', line 24 def initialize(value, timezone) @value = value @timezone_offset = timezone.to_i end |
Instance Method Details
#get_interval_date_filter ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/services/forest_liana/operator_date_interval_parser.rb', line 64 def get_interval_date_filter return nil unless is_interval_date_value() return ">= '#{Time.now}'" if @value == PERIODS_FUTURE return "<= '#{Time.now}'" if @value == PERIODS_PAST if @value == PERIODS_TODAY return "BETWEEN '#{to_client_timezone(Time.now.beginning_of_day)}' " + "AND '#{to_client_timezone(Time.now.end_of_day)}'" end match = PERIODS_PREVIOUS_X_DAYS.match(@value) if match && match[1] return "BETWEEN '" + "#{to_client_timezone(Integer(match[1]).day.ago.beginning_of_day)}'" + " AND '#{to_client_timezone(1.day.ago.end_of_day)}'" end match = PERIODS_X_DAYS_TO_DATE.match(@value) if match && match[1] return "BETWEEN '" + "#{to_client_timezone((Integer(match[1]) - 1).day.ago.beginning_of_day)}'" + " AND '#{Time.now}'" end duration = PERIODS[@value.to_sym][:duration] period = PERIODS[@value.to_sym][:period] period_of_time = PERIODS[@value.to_sym][:period_of_time] || PERIODS[@value.to_sym][:period] to_date = PERIODS[@value.to_sym][:to_date] if to_date from = to_client_timezone(Time.now.send("beginning_of_#{period_of_time}")) to = Time.now else from = to_client_timezone(duration.send(period).ago .send("beginning_of_#{period_of_time}")) to = to_client_timezone(1.send(period).ago .send("end_of_#{period_of_time}")) end "BETWEEN '#{from}' AND '#{to}'" end |
#get_interval_date_filter_for_previous_interval ⇒ Object
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 |
# File 'app/services/forest_liana/operator_date_interval_parser.rb', line 107 def get_interval_date_filter_for_previous_interval return nil unless has_previous_interval() if @value == PERIODS_TODAY return "BETWEEN '#{to_client_timezone(1.day.ago.beginning_of_day)}' AND " + "'#{to_client_timezone(1.day.ago.end_of_day)}'" end match = PERIODS_PREVIOUS_X_DAYS.match(@value) if match && match[1] return "BETWEEN '" + "#{to_client_timezone((Integer(match[1]) * 2).day.ago.beginning_of_day)}'" + " AND '#{to_client_timezone((Integer(match[1]) + 1).day.ago.end_of_day)}'" end match = PERIODS_X_DAYS_TO_DATE.match(@value) if match && match[1] return "BETWEEN '" + "#{to_client_timezone(((Integer(match[1]) * 2) - 1).day.ago.beginning_of_day)}'" + " AND '#{to_client_timezone(Integer(match[1]).day.ago)}'" end duration = PERIODS[@value.to_sym][:duration] period = PERIODS[@value.to_sym][:period] period_of_time = PERIODS[@value.to_sym][:period_of_time] || PERIODS[@value.to_sym][:period] to_date = PERIODS[@value.to_sym][:to_date] if to_date from = to_client_timezone((duration).send(period).ago .send("beginning_of_#{period_of_time}")) to = to_client_timezone((duration).send(period).ago) else from = to_client_timezone((duration * 2).send(period).ago .send("beginning_of_#{period_of_time}")) to = to_client_timezone((1 + duration).send(period).ago .send("end_of_#{period_of_time}")) end "BETWEEN '#{from}' AND '#{to}'" end |
#has_previous_interval ⇒ Object
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 44 def has_previous_interval return false if @value.nil? return true if PERIODS[@value.to_sym] return true if PERIODS_TODAY == @value match = PERIODS_PREVIOUS_X_DAYS.match(@value) return true if match && match[1] match = PERIODS_X_DAYS_TO_DATE.match(@value) return true if match && match[1] false end |
#is_interval_date_value ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/services/forest_liana/operator_date_interval_parser.rb', line 29 def is_interval_date_value return false if @value.nil? return true if PERIODS[@value.to_sym] return true if [PERIODS_PAST, PERIODS_FUTURE, PERIODS_TODAY].include? @value match = PERIODS_PREVIOUS_X_DAYS.match(@value) return true if match && match[1] match = PERIODS_X_DAYS_TO_DATE.match(@value) return true if match && match[1] false end |
#to_client_timezone(date) ⇒ Object
59 60 61 62 |
# File 'app/services/forest_liana/operator_date_interval_parser.rb', line 59 def to_client_timezone(date) # NOTICE: By default, Rails store the dates without timestamp in the database. date - @timezone_offset.hours end |