Class: Mongoid::QueryStringInterface::Parsers::FilterParser

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/mongoid/parsers/filter_parser.rb

Constant Summary collapse

PARSERS =
[
  Mongoid::QueryStringInterface::Parsers::DateTimeParser.new,
  Mongoid::QueryStringInterface::Parsers::NumberParser.new,
  Mongoid::QueryStringInterface::Parsers::ArrayParser.new,
  Mongoid::QueryStringInterface::Parsers::RegexParser.new,
  Mongoid::QueryStringInterface::Parsers::BooleanAndNilParser.new
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#hash_with_indifferent_access, #replace_attribute, #replaced_attribute_name, #replaced_attribute_value

Constructor Details

#initialize(raw_attribute, raw_value, attributes_to_replace = {}, raw_params = {}) ⇒ FilterParser

Returns a new instance of FilterParser.



25
26
27
28
29
30
# File 'lib/mongoid/parsers/filter_parser.rb', line 25

def initialize(raw_attribute, raw_value, attributes_to_replace={}, raw_params={})
  @raw_attribute = raw_attribute
  @raw_value = raw_value
  @attributes_to_replace = attributes_to_replace
  @raw_params = raw_params
end

Instance Attribute Details

#raw_attributeObject (readonly)

Returns the value of attribute raw_attribute.



15
16
17
# File 'lib/mongoid/parsers/filter_parser.rb', line 15

def raw_attribute
  @raw_attribute
end

#raw_valueObject (readonly)

Returns the value of attribute raw_value.



15
16
17
# File 'lib/mongoid/parsers/filter_parser.rb', line 15

def raw_value
  @raw_value
end

Instance Method Details

#attributeObject



32
33
34
# File 'lib/mongoid/parsers/filter_parser.rb', line 32

def attribute
  @attribute ||= replaced_attribute_name(parsed_attribute, @attributes_to_replace).to_s
end

#include?(other_filter) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mongoid/parsers/filter_parser.rb', line 48

def include?(other_filter)
  if or_attribute?
    json_value.any? do |filters|
      filters.filter_parsers.any? do |filter_parser|
        filter_parser.attribute == other_filter.attribute &&
          conditional_array_operators.include?(filter_parser.operator) &&
          filter_parser.operator == other_filter.operator
      end
    end
  end
end

#merge(other_filter) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mongoid/parsers/filter_parser.rb', line 60

def merge(other_filter)
  if or_attribute?
    @value = json_value.map do |filters|
      filters.filter_parsers << other_filter
      filters.parse
    end
  elsif conditional_array_operators.include?(other_filter.operator) && operator == other_filter.operator
    @value = value.inject({}) do |result, filter|
      filter_operation, filter_value = filter
      filter_value = filter_value.concat(other_filter.value[filter_operation]) if other_filter.value[filter_operation]
      result[filter_operation] = filter_value
      result
    end
  else
    @value = value.merge(other_filter.value)
  end
end

#operatorObject



40
41
42
# File 'lib/mongoid/parsers/filter_parser.rb', line 40

def operator
  @operator ||= operator_from(raw_attribute)
end

#or_attribute?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/mongoid/parsers/filter_parser.rb', line 44

def or_attribute?
  raw_attribute == 'or'
end

#valueObject



36
37
38
# File 'lib/mongoid/parsers/filter_parser.rb', line 36

def value
  @value ||= expanded_value
end