Class: QueryStringInterface::Filter

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/query_string_interface/filter.rb

Constant Summary collapse

PARSERS =
[
  QueryStringInterface::Parsers::ArrayParser.new,
  QueryStringInterface::Parsers::DateTimeParser.new,
  QueryStringInterface::Parsers::NumberParser.new,
  QueryStringInterface::Parsers::RegexParser.new,
  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 = {}) ⇒ Filter

Returns a new instance of Filter.



18
19
20
21
22
23
# File 'lib/query_string_interface/filter.rb', line 18

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.



8
9
10
# File 'lib/query_string_interface/filter.rb', line 8

def raw_attribute
  @raw_attribute
end

#raw_valueObject (readonly)

Returns the value of attribute raw_value.



8
9
10
# File 'lib/query_string_interface/filter.rb', line 8

def raw_value
  @raw_value
end

Instance Method Details

#attributeObject



25
26
27
# File 'lib/query_string_interface/filter.rb', line 25

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

#include?(other_filter) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/query_string_interface/filter.rb', line 41

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/query_string_interface/filter.rb', line 53

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



33
34
35
# File 'lib/query_string_interface/filter.rb', line 33

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

#or_attribute?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/query_string_interface/filter.rb', line 37

def or_attribute?
  raw_attribute == 'or'
end

#valueObject



29
30
31
# File 'lib/query_string_interface/filter.rb', line 29

def value
  @value ||= expanded_value
end