Module: Torque::PostgreSQL::Function

Defined in:
lib/torque/postgresql/function.rb,
sig/torque/postgresql/function.rbs

Overview

Simplified module for creating arel functions. This is used internally but can also be made available to other devs on their own projects

Class Method Summary collapse

Class Method Details

.bind(*args) ⇒ ::Arel::Nodes::BindParam

A facilitator to create a bind param that is fully compatible with Arel and ActiveRecord

Parameters:

  • name (column_ref)
  • value (Object)
  • type (type_caster, ActiveModel::Attributes::Normalization::NormalizedValueType)
  • original_attribute (Object)

Returns:

  • (::Arel::Nodes::BindParam)


12
13
14
15
# File 'lib/torque/postgresql/function.rb', line 12

def bind(*args)
  attr = ::ActiveRecord::Relation::QueryAttribute.new(*args)
  ::Arel::Nodes::BindParam.new(attr)
end

.bind_for(model, attribute, value) ⇒ ::Arel::Nodes::BindParam

Just a shortcut to create a bind param for a model attribute and a value for it

Parameters:

  • model (model)
  • attribute (column_ref)
  • value (Object)

Returns:

  • (::Arel::Nodes::BindParam)


19
20
21
# File 'lib/torque/postgresql/function.rb', line 19

def bind_for(model, attribute, value)
  bind(attribute, value, model.attribute_types[attribute])
end

.bind_type(value, type = nil, name: 'value', cast: nil) ⇒ ::Arel::Nodes::BindParam, Arel::Nodes::Cast

A facilitator to create a bind param with a specific type

Parameters:

  • value (Object)
  • type (Symbol, type_caster, nil) (defaults to: nil)
  • name: (column_ref) (defaults to: 'value')
  • cast: (column_ref, nil) (defaults to: nil)

Returns:



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

def bind_type(value, type = nil, name: 'value', cast: nil)
  type ||= ruby_type_to_model_type(value)
  type = ActiveModel::Type.lookup(type) if type.is_a?(Symbol)
  result = bind(name, value, type)
  cast ? result.pg_cast(cast) : result
end

.bind_with(arel_attribute, value) ⇒ ::Arel::Nodes::BindParam

Another shortcut, when we already have the arel attribute at hand

Parameters:

  • arel_attribute (arel_attribute)
  • value (Object)

Returns:

  • (::Arel::Nodes::BindParam)


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

def bind_with(arel_attribute, value)
  bind(arel_attribute.name, value, arel_attribute.type_caster)
end

.concat(*args) ⇒ arel_node

A facilitator to use several Infix operators to concatenate all the provided arguments. Arguments won't be sanitized, as other methods under this module

Parameters:

  • args (arel_node)

Returns:

  • (arel_node)


44
45
46
47
# File 'lib/torque/postgresql/function.rb', line 44

def concat(*args)
  return args.first if args.one?
  args.reduce { |left, right| infix(:"||", left, right) }
end

.group_by(arel, name) ⇒ Arel::Nodes::Ref

A simple helper to trick Rails into producing the right SQL for grouping operations

Parameters:

  • arel (arel_node)
  • name (column_ref)

Returns:



51
52
53
# File 'lib/torque/postgresql/function.rb', line 51

def group_by(arel, name)
  Arel::Nodes::Ref.new(name.to_s, arel)
end

.infix(op, left, right) ⇒ ::Arel::Nodes::InfixOperation

A facilitator to create an infix operation

Parameters:

  • op (Symbol, String)
  • left (arel_node)
  • right (arel_node)

Returns:

  • (::Arel::Nodes::InfixOperation)


37
38
39
# File 'lib/torque/postgresql/function.rb', line 37

def infix(op, left, right)
  ::Arel::Nodes::InfixOperation.new(op, left, right)
end

.method_missing(name, *args) {|arg0| ... } ⇒ ::Arel::Nodes::NamedFunction

This method is used to catch any method calls that are not defined in this module. It will simply return an Arel function with the same name as the method called, passing all arguments to it, without any sanitization

Parameters:

  • name (Symbol)
  • args (Object)

Yields:

Yield Parameters:

  • arg0

Yield Returns:

  • (Object)

Returns:

  • (::Arel::Nodes::NamedFunction)


66
67
68
# File 'lib/torque/postgresql/function.rb', line 66

def method_missing(name, *args, &block)
  ::Arel::Nodes::NamedFunction.new(name.to_s.upcase, args)
end

.respond_to_missing? ⇒ Boolean

As of now, this indicates that it supports any direct calls, since the idea is to simply map to an Arel function with the same name, without checking if it actually exists

Parameters:

  • (Object)

Returns:

  • (Boolean)


58
59
60
# File 'lib/torque/postgresql/function.rb', line 58

def respond_to_missing?(*)
  true
end