Class: SearchCopGrammar::Attributes::Date

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

Instance Attribute Summary

Attributes inherited from Base

#attribute, #column_name, #options, #table_alias

Instance Method Summary collapse

Methods inherited from Datetime

#between, #eq, #gt, #map, #not_eq

Methods inherited from WithoutMatches

#matches

Methods inherited from Base

#compatible?, #fulltext?, #initialize, #map, #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

#parse(value) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/search_cop_grammar/attributes.rb', line 260

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

  if value =~ /^[0-9]+ (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.to_date..::Date.today
  elsif value =~ /^[0-9]{4}$/
    ::Date.new(value.to_i).beginning_of_year..::Date.new(value.to_i).end_of_year
  elsif value =~ %r{^([0-9]{4})(\.|-|/)([0-9]{1,2})$}
    ::Date.new(Regexp.last_match(1).to_i, Regexp.last_match(3).to_i, 15).beginning_of_month..::Date.new(Regexp.last_match(1).to_i, Regexp.last_match(3).to_i, 15).end_of_month
  elsif value =~ %r{^([0-9]{1,2})(\.|-|/)([0-9]{4})$}
    ::Date.new(Regexp.last_match(3).to_i, Regexp.last_match(1).to_i, 15).beginning_of_month..::Date.new(Regexp.last_match(3).to_i, Regexp.last_match(1).to_i, 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}}
    date = ::Date.parse(value)
    date..date
  else
    raise ArgumentError
  end
rescue ArgumentError
  raise SearchCop::IncompatibleDatatype, "Incompatible datatype for #{value}"
end