Class: Arel::Enhance::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/arel/enhance/query.rb

Class Method Summary collapse

Class Method Details

.call(node, kwargs) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/arel/enhance/query.rb', line 4

def self.call(node, kwargs)
  node_attributes = %i[context parent]
  node_args = kwargs.slice(*node_attributes)
  object_args = kwargs.except(*node_attributes)

  node.each.select do |child_node|
    next unless matches?(child_node, node_args)

    matches?(child_node.object, object_args)
  end
end

.matches?(object, test) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/arel/enhance/query.rb', line 16

def self.matches?(object, test)
  case test
  when Hash
    case object
    when Hash
      test <= object
    else
      test.all? do |test_key, test_value|
        next false unless object.respond_to?(test_key)

        object_attribute_value = object.public_send(test_key)
        matches? object_attribute_value, test_value
      end
    end
  when Arel::Enhance::QueryMethods::QueryMethod
    test.matches?(object)
  else
    object == test
  end
end