Class: Torque::PostgreSQL::PredicateBuilder::StructHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/torque/postgresql/predicate_builder/struct_handler.rb,
sig/torque/postgresql/predicate_builder/struct_handler.rbs

Overview

Turns a hash of properties into a proper set of conditions over the document that holds them, handing each of them back to the predicate builder so that the whole where vocabulary works inside a struct

Constant Summary collapse

PLAIN_TYPES =

Properties are extracted as text, so only the ones whose type has a counterpart on the database are casted before being compared

Returns:

  • (Array[Symbol])
%i[string text].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(predicate_builder) ⇒ StructHandler

Returns a new instance of StructHandler.

Parameters:



25
26
27
# File 'lib/torque/postgresql/predicate_builder/struct_handler.rb', line 25

def initialize(predicate_builder)
  @predicate_builder = predicate_builder
end

Class Method Details

.candidate?(type, value = nil) ⇒ Boolean

Only a hash describes properties, everything else is left alone so that whole documents are still compared as documents. Without a value, a single document has properties that can be reached

Parameters:

  • type (type_caster, nil)
  • value (Object) (defaults to: nil)

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/torque/postgresql/predicate_builder/struct_handler.rb', line 18

def candidate?(type, value = nil)
  return false if !type.is_a?(Adapter::OID::Struct) || type.is_a?(Adapter::OID::StructList)

  value.nil? || value.is_a?(::Hash)
end

Instance Method Details

#call(attribute, struct, value) ⇒ ::Arel::Nodes::Grouping

Parameters:

  • attribute (arel_node)
  • struct (type_caster)
  • value (Hash[untyped, untyped])

Returns:

  • (::Arel::Nodes::Grouping)


29
30
31
32
33
34
# File 'lib/torque/postgresql/predicate_builder/struct_handler.rb', line 29

def call(attribute, struct, value)
  table = table_for(attribute, struct)
  nodes = predicate_builder.with(table).build_from_hash(value.stringify_keys)

  ::Arel::Nodes::Grouping.new(nodes.reduce(:and))
end

#property_for(attribute, struct, path) ⇒ arel_node

The node for a path into the document, resolved one property at a time so that each level is typed by the class that declares it

Parameters:

  • attribute (arel_node)
  • struct (type_caster, nil)
  • path (Array[path_key])

Returns:

  • (arel_node)


51
52
53
54
55
56
# File 'lib/torque/postgresql/predicate_builder/struct_handler.rb', line 51

def property_for(attribute, struct, path)
  path.reduce(attribute) do |node, key|
    struct = node.type_caster if node.is_a?(Arel::Nodes::Property)
    table_for(node, struct)[key]
  end
end

#table_for(attribute, struct) ⇒ PredicateTable

The properties of the document as the columns of a table, so that any of them can be resolved by name. A document that no class describes is open, which means any path into it is accepted, without a type

Parameters:

  • attribute (arel_node)
  • struct (type_caster, nil)

Returns:

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
# File 'lib/torque/postgresql/predicate_builder/struct_handler.rb', line 39

def table_for(attribute, struct)
  raise ArgumentError, <<~MSG.squish if struct.is_a?(Adapter::OID::Struct) && struct.type != :jsonb
    Unable to build a condition over "#{attribute.name}" because json
    columns cannot be queried by their properties. Use jsonb instead.
  MSG

  @struct = struct
  PredicateTable.new(self, attribute, Arel::Nodes::Property)
end

#type_of(name) ⇒ [type_caster?, String?]

The type of a single property, plus what it has to be casted to once it is extracted from the document as text

Parameters:

  • name (path_key)

Returns:

  • ([type_caster?, String?])

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/torque/postgresql/predicate_builder/struct_handler.rb', line 60

def type_of(name)
  return [ActiveModel::Type.default_value, nil] unless @struct.is_a?(Adapter::OID::Struct)

  klass = @struct.klass
  name = name.to_s

  raise ArgumentError, <<~MSG.squish if klass.strict? && !klass.attribute_names.include?(name)
    Unable to build a condition for "#{name}" because it is not a
    declared property of #{klass.name}.
  MSG

  type = klass.attribute_types[name]
  [type, cast_for(type)]
end