Module: Torque::PostgreSQL::Function

Defined in:
lib/torque/postgresql/function.rb

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) ⇒ Object

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



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) ⇒ Object

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



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) ⇒ Object

A facilitator to create a bind param with a specific type



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) ⇒ Object

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



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) ⇒ Object

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



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) ⇒ Object

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



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) ⇒ Object

A facilitator to create an infix operation



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, &block) ⇒ Object

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



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

Returns:

  • (Boolean)


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

def respond_to_missing?(*)
  true
end