Module: ChgkRating::Utils::Transformations

Included in:
AttributeMappings
Defined in:
lib/chgk_rating/utils/transformations.rb

Constant Summary collapse

TRANSFORMERS =
{
    string: to_star,
    integer: to_star(:to_i),
    float: to_star(:to_f),
    id: to_star(:id),
    ids: to_star(:id, true),
    sym: to_star(:to_sym),
    strdate: to_star(:to_s_chgk),
    uri: ->(d) { URI.parse_safely d },
    boolean: to_boolean,
    binboolean: to_binary_boolean,
    date: ->(d) { Date.parse_safely d},
    datetime: ->(d) { DateTime.parse_safely d},
    splitboolean: ->(d) do
      d&.split('')&.map {|result| to_boolean.call(result)}
    end,
    arraystrboolean: ->(d) do
      d&.map {|result| to_binary_boolean.call(result)}
    end,
    arrayboolean: ->(d) do
      d&.map {|result| to_boolean.call(result)}
    end,
    team: chgk_object('Team'),
    player: chgk_object('Player'),
    players: chgk_object('Players', 'Collections')
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_transformers!Object


18
19
20
21
22
# 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

Instance Method Details

#transformation(name = 'integer_string') ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
# 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