Class: SearchCopGrammar::Attributes::Datetime

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

Direct Known Subclasses

Date, Time, Timestamp, Timestamptz

Instance Attribute Summary

Attributes inherited from Base

#attribute, #column_name, #field_names, #options, #table_alias

Instance Method Summary collapse

Methods inherited from WithoutMatches

#matches

Methods inherited from Base

#compatible?, #fulltext?, #initialize, #method_missing, #respond_to_missing?

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



257
258
259
# File 'lib/search_cop_grammar/attributes.rb', line 257

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

#eq(value) ⇒ Object



245
246
247
# File 'lib/search_cop_grammar/attributes.rb', line 245

def eq(value)
  between parse(value)
end

#gt(value) ⇒ Object



253
254
255
# File 'lib/search_cop_grammar/attributes.rb', line 253

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

#map(value) ⇒ Object



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

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

#not_eq(value) ⇒ Object



249
250
251
# File 'lib/search_cop_grammar/attributes.rb', line 249

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

#parse(value) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/search_cop_grammar/attributes.rb', line 215

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 =~ %r{^([0-9]{4})(\.|-|/)([0-9]{1,2})$}
    ::Time.new(Regexp.last_match(1), Regexp.last_match(3), 15).beginning_of_month..::Time.new(Regexp.last_match(1), Regexp.last_match(3), 15).end_of_month
  elsif value =~ %r{^([0-9]{1,2})(\.|-|/)([0-9]{4})$}
    ::Time.new(Regexp.last_match(3), Regexp.last_match(1), 15).beginning_of_month..::Time.new(Regexp.last_match(3), Regexp.last_match(1), 15).end_of_month
  elsif value =~ %r{^[0-9]{4}(\.|-|/)[0-9]{1,2}(\.|-|/)[0-9]{1,2}$} || value =~ %r{^[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 =~ %r{[0-9]{4}(\.|-|/)[0-9]{1,2}(\.|-|/)[0-9]{1,2}} || value =~ %r{[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