Class: ROM::Mapper
- Inherits:
-
Object
- Object
- ROM::Mapper
- Extended by:
- Plugins::ClassMethods, SettingProxy
- Includes:
- DSL
- Defined in:
- lib/rom/mapper.rb,
lib/rom/mapper/dsl.rb,
lib/rom/compat/mapper.rb,
lib/rom/mapper/model_dsl.rb,
lib/rom/mapper/attribute_dsl.rb
Overview
Mapper is a simple object that uses transformers to load relations
Defined Under Namespace
Modules: DSL, ModelDSL Classes: AttributeDSL
Instance Attribute Summary collapse
-
#header ⇒ Header
readonly
private
Header that was used to build the transformers.
-
#transformers ⇒ Object
readonly
private
Transformers object built by a processor.
Class Method Summary collapse
-
.build(header = self.header, processor = :transformer) ⇒ Mapper
private
Build a mapper using provided processor type.
-
.headers(header) ⇒ Array<Header>
private
Prepares an array of headers for a potentially multistep mapper.
-
.plugins ⇒ Object
extended
from Plugins::ClassMethods
Return all available plugins for the component type.
-
.processors ⇒ Hash
private
Registered processors.
-
.register_processor(processor) ⇒ Hash
private
Register a processor class.
- .setting_mapping ⇒ Object
-
.use(name, **options) ⇒ Object
extended
from Plugins::ClassMethods
Include a registered plugin in this relation class.
Instance Method Summary collapse
-
#call(relation) ⇒ Object
private
Process a relation using the transformers.
-
#initialize(header, processor = :transformer) ⇒ Mapper
constructor
private
A new instance of Mapper.
-
#model ⇒ Class
private
Optional model that is instantiated by a mapper.
Constructor Details
#initialize(header, processor = :transformer) ⇒ Mapper
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.
Returns a new instance of Mapper.
77 78 79 80 81 82 83 |
# File 'lib/rom/mapper.rb', line 77 def initialize(header, processor = :transformer) processor = Mapper.processors.fetch(processor) @transformers = self.class.headers(header).map do |hdr| processor.build(self, hdr) end @header = header end |
Instance Attribute Details
#header ⇒ Header (readonly)
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.
Returns header that was used to build the transformers.
35 36 37 |
# File 'lib/rom/mapper.rb', line 35 def header @header end |
#transformers ⇒ Object (readonly)
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.
Returns transformers object built by a processor.
30 31 32 |
# File 'lib/rom/mapper.rb', line 30 def transformers @transformers end |
Class Method Details
.build(header = self.header, processor = :transformer) ⇒ Mapper
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.
Build a mapper using provided processor type
72 73 74 |
# File 'lib/rom/mapper.rb', line 72 def self.build(header = self.header, processor = :transformer) new(header, processor) end |
.headers(header) ⇒ Array<Header>
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.
Prepares an array of headers for a potentially multistep mapper
60 61 62 63 64 65 |
# File 'lib/rom/mapper.rb', line 60 def self.headers(header) return [header] if steps.empty? return steps.map(&:header) if attributes.empty? raise(MapperMisconfiguredError, "cannot mix outer attributes and steps") end |
.plugins ⇒ Object Originally defined in module Plugins::ClassMethods
Return all available plugins for the component type
.processors ⇒ Hash
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.
Returns registered processors.
40 41 42 |
# File 'lib/rom/mapper.rb', line 40 def self.processors @_processors ||= {} end |
.register_processor(processor) ⇒ Hash
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.
Register a processor class
49 50 51 52 |
# File 'lib/rom/mapper.rb', line 49 def self.register_processor(processor) name = processor.name.split("::").last.downcase.to_sym processors.update(name => processor) end |
.setting_mapping ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/rom/compat/mapper.rb', line 10 def setting_mapping @setting_mapper ||= ROM::Transformer.setting_mapping.merge( inherit_header: [], reject_keys: [], symbolize_keys: [], copy_keys: [], prefix: [], prefix_separator: [] ).freeze end |
.use(name, **options) ⇒ Object Originally defined in module Plugins::ClassMethods
Include a registered plugin in this relation class
Instance Method Details
#call(relation) ⇒ 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.
Process a relation using the transformers
95 96 97 |
# File 'lib/rom/mapper.rb', line 95 def call(relation) transformers.reduce(relation.to_a) { |a, e| e.call(a) } end |
#model ⇒ Class
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.
Returns optional model that is instantiated by a mapper.
88 89 90 |
# File 'lib/rom/mapper.rb', line 88 def model header.model end |