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?

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



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/search_cop_grammar/attributes.rb', line 248

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