Sha256: 6eb405bb32a95842e37d868093499c6101fc7891a561aa19e0424d7232492d32
Contents?: true
Size: 1.06 KB
Versions: 2
Compression:
Stored size: 1.06 KB
Contents
# frozen_string_literal: true module WiseGopher # Cast query columns and transform value class Column attr_reader :name, :alias def initialize(name, type_symbol, transform: nil, as: nil) @alias = as&.to_s.freeze || name.to_s.freeze @name = name.to_s.freeze @type_symbol = type_symbol @transform = transform&.to_proc end def cast(value) casted_value = type.deserialize(value) @transform ? transform_value(casted_value) : casted_value end def type @type ||= ActiveRecord::Type.lookup type_symbol end def define_getter(row_class) column = self row_class.define_method(@alias) do instance_variable_get(column.instance_variable_name) end end def instance_variable_name @instance_variable_name ||= "@#{@alias.tr("?!", "")}" end private attr_reader :type_symbol def transform_value(value) case @transform.arity when 0 then value.instance_exec(&@transform) else value.instance_eval(&@transform) end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
wise_gopher-0.3.2 | lib/wise_gopher/column.rb |
wise_gopher-0.2.2 | lib/wise_gopher/column.rb |