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

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

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.



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

def initialize(predicate_builder)
  @predicate_builder = predicate_builder
end

Class Method Details

.candidate?(value, type) ⇒ Boolean

Values that this handler knows how to deal with, checked before the attribute itself because it is way cheaper

Returns:

  • (Boolean)


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

def candidate?(value, type)
  return false unless Adapter::OID::Composite.from(type)

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

.columns?(value) ⇒ Boolean

Whether the value describes columns, instead of whole records

Returns:

  • (Boolean)


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

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

Instance Method Details

#call(attribute, type, value) ⇒ Object



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

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

#type_of(name) ⇒ Object

The type of a single column of the composite type

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/torque/postgresql/predicate_builder/composite_handler.rb', line 45

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