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
Defined Under Namespace
Classes: Error, InvalidNameError, Mapper, MapperNotFoundError, MapperRegistry
Constant Summary collapse
- VERSION =
'1.1.0'
Class Method Summary collapse
- ._handle_map_args(from, to) ⇒ Object private
- ._map(from, to, map_method) ⇒ Object private
-
.map(from:, to:) ⇒ Object
Map an object to another using a registered mapper.
-
.map!(from:, to:) ⇒ Object
Map an object to another using a registered mapper.
-
.mapper_registry ⇒ MapperRegistry
Holds the global registry of mappers.
-
.register_mapper(mapper, from_klass: nil, to_klass: nil) ⇒ MapperRegistry
Register a new mapper to the registry.
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.
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.
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.
53 54 55 |
# File 'lib/karta.rb', line 53 def self.map!(from:, to:) _map(from, to, :map!) end |
.mapper_registry ⇒ MapperRegistry
Holds the global registry of mappers
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
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 |