Class: AttrSearchableGrammar::Attributes::Date

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

Instance Attribute Summary

Attributes inherited from Base

#attribute, #options

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 AttrSearchableGrammar::Attributes::Base

Dynamic Method Handling

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

Instance Method Details

#parse(value) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/attr_searchable_grammar/attributes.rb', line 197

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

  if 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
  else
    date = ::Date.parse(value)
    date .. date
  end
rescue ArgumentError
  raise AttrSearchable::IncompatibleDatatype, "Incompatible datatype for #{value}"
end