Module: Karta

Defined in:
lib/karta.rb,
lib/karta/error.rb,
lib/karta/mapper.rb,
lib/karta/version.rb,
lib/karta/mapper_registry.rb

Overview

The module that contains everything relating to Karta

Author:

Defined Under Namespace

Classes: Error, InvalidNameError, Mapper, MapperNotFoundError, MapperRegistry

Constant Summary collapse

VERSION =
'1.1.0'

Class Method Summary collapse

Class Method Details

._handle_map_args(from, to) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (ArgumentError)


66
67
68
69
70
# File 'lib/karta.rb', line 66

def self._handle_map_args(from, to)
  raise ArgumentError, 'cannot map from a class' if from.is_a?(Class)
  to_klass = to.is_a?(Class) ? to : to.class
  [to, to_klass, from, from.class]
end

._map(from, to, map_method) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
61
62
63
# File 'lib/karta.rb', line 58

def self._map(from, to, map_method)
  to, to_klass, from, from_klass = *_handle_map_args(from, to)

  mapper_registry.find(from_klass: from_klass, to_klass: to_klass)
                 .send(map_method, from: from, to: to)
end

.map(from:, to:) ⇒ Object

Map an object to another using a registered mapper. Returns a new instance of the mapped object.

Parameters:

  • from (Object)

    the object to map from

  • to (Object, Class)

    the object or class to map to

Returns:

  • (Object)

    a new instance of the same type as ‘to`

Raises:

  • (ArgumentError)

    if trying to map from a class rather than an instance



40
41
42
# File 'lib/karta.rb', line 40

def self.map(from:, to:)
  _map(from, to, :map)
end

.map!(from:, to:) ⇒ Object

Map an object to another using a registered mapper. Performs the mapping “in place” and thus modifies the ‘to’ object and overrides attributes.

Parameters:

  • from (Object)

    the object to map from

  • to (Object, Class)

    the object or class to map to

Returns:

  • (Object)

    returns modified version of ‘to’

Raises:

  • (ArgumentError)

    if trying to map from a class rather than an instance



53
54
55
# File 'lib/karta.rb', line 53

def self.map!(from:, to:)
  _map(from, to, :map!)
end

.mapper_registryMapperRegistry

Holds the global registry of mappers

Returns:



14
15
16
# File 'lib/karta.rb', line 14

def self.mapper_registry
  @mapper_registry ||= MapperRegistry.new
end

.register_mapper(mapper, from_klass: nil, to_klass: nil) ⇒ MapperRegistry

Register a new mapper to the registry

Parameters:

  • mapper (Karta::Mapper)

    the mapper to be registered

  • from_klass (Class) (defaults to: nil)

    the class the mapper is supposed to map from

  • to_klass (Class) (defaults to: nil)

    the class the mapper is supposed to map to

Returns:



25
26
27
28
29
# File 'lib/karta.rb', line 25

def self.register_mapper(mapper, from_klass: nil, to_klass: nil)
  mapper_registry.register(mapper: mapper,
                           from_klass: from_klass,
                           to_klass: to_klass)
end