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: lambda do |d|
                  d&.split('')&.map { |result| to_boolean.call(result) }
                end,
  arraystrboolean: lambda do |d|
                     d&.map { |result| to_binary_boolean.call(result) }
                   end,
  arrayboolean: lambda do |d|
                  d&.map { |result| to_boolean.call(result) }
                end,
  team: chgk_object('Team'),
  tournament: chgk_object('Tournament'),
  player: chgk_object('Player'),
  players: chgk_object('Players', 'Collections')
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_transformers!Object



20
21
22
23
24
# File 'lib/chgk_rating/utils/transformations.rb', line 20

def load_transformers!
  TRANSFORMERS.each do |method_name, transformer|
    define_method(method_name) { transformer }
  end
end

Instance Method Details

#transformation(name = 'integer_string') ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/chgk_rating/utils/transformations.rb', line 6

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].reduce({}) do |result, t|
    current_transformer = binding.local_variable_get t
    result.merge({
                   "transform_#{t}".to_sym => send(current_transformer)
                 })
  end
end