Class: SearchCopGrammar::Attributes::Datetime

Inherits:
WithoutMatches show all
Defined in:
lib/search_cop_grammar/attributes.rb

Direct Known Subclasses

Date, Time, Timestamp

Instance Attribute Summary

Attributes inherited from Base

#attribute, #column_name, #options, #table_alias

Instance Method Summary collapse

Methods inherited from WithoutMatches

#matches

Methods inherited from Base

#compatible?, #fulltext?, #initialize, #method_missing, #respond_to?

Constructor Details

This class inherits a constructor from SearchCopGrammar::Attributes::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class SearchCopGrammar::Attributes::Base

Instance Method Details

#between(range) ⇒ Object



240
241
242
# File 'lib/search_cop_grammar/attributes.rb', line 240

def between(range)
  gteq(range.first).and(lteq(range.last))
end

#eq(value) ⇒ Object



228
229
230
# File 'lib/search_cop_grammar/attributes.rb', line 228

def eq(value)
  between parse(value)
end

#gt(value) ⇒ Object



236
237
238
# File 'lib/search_cop_grammar/attributes.rb', line 236

def gt(value)
  super parse(value).last
end

#map(value) ⇒ Object



224
225
226
# File 'lib/search_cop_grammar/attributes.rb', line 224

def map(value)
  parse(value).first
end

#not_eq(value) ⇒ Object



232
233
234
# File 'lib/search_cop_grammar/attributes.rb', line 232

def not_eq(value)
  between(parse(value)).not
end

#parse(value) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/search_cop_grammar/attributes.rb', line 198

def parse(value)
  return value .. value unless value.is_a?(::String)

  if value =~ /^[0-9]+ (hour|day|week|month|year)s{0,1} (ago)$/
    number,period,ago = value.split(' ')
    time = number.to_i.send(period.to_sym).send(ago.to_sym)
    time .. ::Time.now
  elsif value =~ /^[0-9]{4}$/
    ::Time.new(value).beginning_of_year .. ::Time.new(value).end_of_year
  elsif value =~ /^([0-9]{4})(\.|-|\/)([0-9]{1,2})$/
    ::Time.new($1, $3, 15).beginning_of_month .. ::Time.new($1, $3, 15).end_of_month
  elsif value =~ /^([0-9]{1,2})(\.|-|\/)([0-9]{4})$/
    ::Time.new($3, $1, 15).beginning_of_month .. ::Time.new($3, $1, 15).end_of_month
  elsif value =~ /^[0-9]{4}(\.|-|\/)[0-9]{1,2}(\.|-|\/)[0-9]{1,2}$/ || value =~ /^[0-9]{1,2}(\.|-|\/)[0-9]{1,2}(\.|-|\/)[0-9]{4}$/
    time = ::Time.parse(value)
    time.beginning_of_day .. time.end_of_day
  elsif value =~ /[0-9]{4}(\.|-|\/)[0-9]{1,2}(\.|-|\/)[0-9]{1,2}/ || value =~ /[0-9]{1,2}(\.|-|\/)[0-9]{1,2}(\.|-|\/)[0-9]{4}/
    time = ::Time.parse(value)
    time .. time
  else
    raise ArgumentError
  end
rescue ArgumentError
  raise SearchCop::IncompatibleDatatype, "Incompatible datatype for #{value}"
end