Class: Torque::PostgreSQL::PredicateBuilder::LtreeHandler

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

Overview

Serves every column of the ltree extension. A value of the same kind as the column is an equality, a value of the opposite kind is a match, and an Array is a list of values as soon as its first entry is a whole one

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(predicate_builder) ⇒ LtreeHandler

Returns a new instance of LtreeHandler.

Parameters:



20
21
22
# File 'lib/torque/postgresql/predicate_builder/ltree_handler.rb', line 20

def initialize(predicate_builder)
  @predicate_builder = predicate_builder
end

Class Method Details

.candidate?(type) ⇒ Boolean

Parameters:

  • type (type_caster, nil)

Returns:

  • (Boolean)


11
12
13
# File 'lib/torque/postgresql/predicate_builder/ltree_handler.rb', line 11

def candidate?(type)
  column_of(type).is_a?(Adapter::OID::Ltree)
end

.column_of(type) ⇒ type_caster?

Parameters:

  • type (type_caster, nil)

Returns:

  • (type_caster, nil)


15
16
17
# File 'lib/torque/postgresql/predicate_builder/ltree_handler.rb', line 15

def column_of(type)
  type.is_a?(ARRAY_OID) ? type.subtype : type
end

Instance Method Details

#call(attribute, type, value) ⇒ arel_node

Parameters:

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

Returns:

  • (arel_node)


24
25
26
27
28
29
30
31
32
33
# File 'lib/torque/postgresql/predicate_builder/ltree_handler.rb', line 24

def call(attribute, type, value)
  @attribute = attribute
  @column = self.class.column_of(type)
  @array = type.is_a?(ARRAY_OID)
  @list = list?(value)

  values = list ? value : [value]
  pattern = values.any? { |entry| lquery.cast(entry)&.pattern? }
  pattern == lquery_column? ? equality(values) : match(values)
end