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_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



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

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

#eq(value) ⇒ Object



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

def eq(value)
  between parse(value)
end

#gt(value) ⇒ Object



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

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

#map(value) ⇒ Object



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

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

#not_eq(value) ⇒ Object



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

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

#parse(value) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/search_cop_grammar/attributes.rb', line 210

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