Class: Torque::PostgreSQL::PredicateBuilder::CompositeHandler

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

Overview

Turns a hash of columns into a proper set of conditions over a composite column, handing each of them back to the predicate builder so that the whole where vocabulary works inside a composite

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(predicate_builder) ⇒ CompositeHandler

Returns a new instance of CompositeHandler.

Parameters:



30
31
32
# File 'lib/torque/postgresql/predicate_builder/composite_handler.rb', line 30

def initialize(predicate_builder)
  @predicate_builder = predicate_builder
end

Class Method Details

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

Values that this handler knows how to deal with. Without a value, only a single composite has parts that can be reached on their own

Parameters:

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

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
# File 'lib/torque/postgresql/predicate_builder/composite_handler.rb', line 13

def candidate?(type, value = nil)
  return false unless Adapter::OID::Composite.from(type)
  return !type.is_a?(ARRAY_OID) if value.nil?

  case value
  when ::Hash, Attributes::Composite then true
  when ::Array then value.compact.any? { |entry| candidate?(type, entry) }
  else false
  end
end

.columns?(value) ⇒ Boolean

Whether the value describes columns, instead of whole records

Parameters:

  • value (Object)

Returns:

  • (Boolean)


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

def columns?(value)
  value.is_a?(::Hash) || (value.is_a?(::Array) && value.all?(::Hash))
end

Instance Method Details

#call(attribute, type, value) ⇒ arel_node

Parameters:

  • attribute (arel_node)
  • type (type_caster)
  • value (Object)

Returns:

  • (arel_node)


34
35
36
37
38
39
40
41
42
43
# File 'lib/torque/postgresql/predicate_builder/composite_handler.rb', line 34

def call(attribute, type, value)
  @composite = Adapter::OID::Composite.from(type)
  array = type.is_a?(ARRAY_OID)
  record = !self.class.columns?(value)

  return call_for_record(attribute, value, array) if record
  return call_for_array(attribute, value) if array

  conditions_for(attribute, value)
end

#table_for(source, type) ⇒ PredicateTable

The columns of the composite value as the columns of a table, so that any of them can be resolved by name

Parameters:

  • source (arel_node)
  • type (type_caster)

Returns:



47
48
49
50
# File 'lib/torque/postgresql/predicate_builder/composite_handler.rb', line 47

def table_for(source, type)
  @composite = Adapter::OID::Composite.from(type)
  PredicateTable.new(self, source, Arel::Nodes::Column)
end

#type_of(name) ⇒ [type_caster]

The type of a single column of the composite type

Parameters:

  • name (column_ref)

Returns:

  • ([type_caster])

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/torque/postgresql/predicate_builder/composite_handler.rb', line 53

def type_of(name)
  columns = @composite.columns
  name = name.to_s

  raise ArgumentError, <<~MSG.squish unless columns.key?(name)
    Unable to build a condition for "#{name}" because it is not a
    column of the "#{@composite.name}" composite type.
  MSG

  [columns[name]]
end