module ChgkRating::Utils::Transformations

Constants

TRANSFORMERS

Public Class Methods

load_transformers!() click to toggle source
# File lib/chgk_rating/utils/transformations.rb, line 18
def load_transformers!
  TRANSFORMERS.each do |method_name, transformer|
    define_method(method_name) { transformer }
  end
end

Private Class Methods

chgk_object(namespace, type = 'Models') click to toggle source
# File lib/chgk_rating/utils/transformations.rb, line 26
def chgk_object(namespace, type = 'Models')
  ->(d) do
    opts = type == 'Models' ?
               [d, {lazy: true}] :
               [{collection: d, lazy: true}]

    Module.const_get("ChgkRating::#{type}::#{namespace}").new(*opts)
  end
end
to_binary_boolean() click to toggle source
# File lib/chgk_rating/utils/transformations.rb, line 40
def to_binary_boolean
  ->(d) { d ? '1' : '0' }
end
to_boolean() click to toggle source
# File lib/chgk_rating/utils/transformations.rb, line 36
def to_boolean
  ->(d) { !d.to_i.zero? }
end
to_star(method = :to_s, iterate = false) click to toggle source
# File lib/chgk_rating/utils/transformations.rb, line 44
def to_star(method = :to_s, iterate = false)
  ->(d) do
    iterate ?
        d.map {|obj| obj.send method } :
        d.send(method)
  end
end

Public Instance Methods

transformation(name = 'integer_string') click to toggle source
# File lib/chgk_rating/utils/transformations.rb, line 4
def transformation(name = 'integer_string')
  up, down = name.to_s.split '_'
  up = 'integer' if up.nil? || up.empty?
  down = 'string' if down.nil? || down.empty?

  %i(up down).inject({}) do |result, t|
    current_transformer = binding.local_variable_get t
    result.merge({
                     "transform_#{t}".to_sym => send(current_transformer)
                 })
  end
end