class WiseGopher::Param

Register query's params and build query's bind variables

Attributes

name[R]
type[R]

Public Class Methods

new(name, type_symbol, transform = nil) click to toggle source
# File lib/wise_gopher/param.rb, line 8
def initialize(name, type_symbol, transform = nil)
  @name      = name.to_s.freeze
  @type      = ActiveRecord::Type.lookup type_symbol
  @transform = transform&.to_proc
end

Public Instance Methods

build_bind(value) click to toggle source
# File lib/wise_gopher/param.rb, line 14
def build_bind(value)
  prepared_value = @transform ? transform_value(value) : value

  ActiveRecord::Relation::QueryAttribute.new(name, prepared_value, type)
end

Private Instance Methods

transform_value(value) click to toggle source
# File lib/wise_gopher/param.rb, line 22
def transform_value(value)
  case @transform.arity
  when 0 then value.instance_exec(&@transform)
  else
    value.instance_eval(&@transform)
  end
end