Class: AttrSearchableGrammar::Attributes::Datetime

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

Direct Known Subclasses

Date, Time, Timestamp

Instance Attribute Summary

Attributes inherited from Base

#attribute, #options

Instance Method Summary collapse

Methods inherited from WithoutMatches

#matches

Methods inherited from Base

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

#between(range) ⇒ Object



174
175
176
# File 'lib/attr_searchable_grammar/attributes.rb', line 174

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

#eq(value) ⇒ Object



166
167
168
# File 'lib/attr_searchable_grammar/attributes.rb', line 166

def eq(value)
  between parse(value)
end

#map(value) ⇒ Object



162
163
164
# File 'lib/attr_searchable_grammar/attributes.rb', line 162

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

#not_eq(value) ⇒ Object



170
171
172
# File 'lib/attr_searchable_grammar/attributes.rb', line 170

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

#parse(value) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/attr_searchable_grammar/attributes.rb', line 142

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

  if value =~ /^[0-9]{4,}$/
    ::Time.new(value).beginning_of_year .. ::Time.new(value).end_of_year
  elsif value =~ /^([0-9]{4,})(\.|-|\/)([0-9]{1,2})$/
    ::Time.new($1, $3, 15).beginning_of_month .. ::Time.new($1, $3, 15).end_of_month
  elsif value =~ /^([0-9]{1,2})(\.|-|\/)([0-9]{4,})$/
    ::Time.new($3, $1, 15).beginning_of_month .. ::Time.new($3, $1, 15).end_of_month
  elsif value !~ /:/ 
    time = ::Time.parse(value)
    time.beginning_of_day .. time.end_of_day
  else
    time = ::Time.parse(value)
    time .. time
  end 
rescue ArgumentError
  raise AttrSearchable::IncompatibleDatatype, "Incompatible datatype for #{value}"
end