Class: ROM::Mapper
- Inherits:
-
Object
- Object
- ROM::Mapper
- Includes:
- DSL
- Defined in:
- lib/rom/mapper.rb,
lib/rom/mapper/dsl.rb,
lib/rom/mapper/builder.rb,
lib/rom/mapper/version.rb,
lib/rom/mapper/model_dsl.rb,
lib/rom/mapper/mapper_dsl.rb,
lib/rom/mapper/attribute_dsl.rb,
lib/rom/mapper/configuration_plugin.rb
Overview
Mapper is a simple object that uses transformers to load relations
Defined Under Namespace
Modules: ConfigurationPlugin, DSL, ModelDSL Classes: AttributeDSL, Builder, MapperDSL
Constant Summary collapse
- VERSION =
'1.1.0'.freeze
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 = :transproc) ⇒ Mapper
private
Build a mapper using provided processor type.
-
.headers(header) ⇒ Array<Header>
private
Prepares an array of headers for a potentially multistep mapper.
-
.processors ⇒ Hash
private
Registered processors.
-
.register_processor(processor) ⇒ Hash
private
Register a processor class.
- .registry(descendants) ⇒ Object private
Instance Method Summary collapse
-
#call(relation) ⇒ Object
private
Process a relation using the transformers.
-
#initialize(header, processor = :transproc) ⇒ Mapper
constructor
private
A new instance of Mapper.
-
#model ⇒ Class
private
Optional model that is instantiated by a mapper.
Methods included from DSL
Constructor Details
#initialize(header, processor = :transproc) ⇒ 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.
76 77 78 79 80 81 82 |
# File 'lib/rom/mapper.rb', line 76 def initialize(header, processor = :transproc) 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.
28 29 30 |
# File 'lib/rom/mapper.rb', line 28 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.
23 24 25 |
# File 'lib/rom/mapper.rb', line 23 def transformers @transformers end |
Class Method Details
.build(header = self.header, processor = :transproc) ⇒ 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
63 64 65 |
# File 'lib/rom/mapper.rb', line 63 def self.build(header = self.header, processor = :transproc) 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
52 53 54 55 56 |
# File 'lib/rom/mapper.rb', line 52 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 |
.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.
33 34 35 |
# File 'lib/rom/mapper.rb', line 33 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
42 43 44 45 |
# File 'lib/rom/mapper.rb', line 42 def self.register_processor(processor) name = processor.name.split('::').last.downcase.to_sym processors.update(name => processor) end |
.registry(descendants) ⇒ 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.
68 69 70 71 72 73 |
# File 'lib/rom/mapper.rb', line 68 def self.registry(descendants) descendants.each_with_object({}) do |klass, h| name = klass.register_as || klass.relation (h[klass.base_relation] ||= {})[name] = klass.build end end |
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
94 95 96 |
# File 'lib/rom/mapper.rb', line 94 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.
87 88 89 |
# File 'lib/rom/mapper.rb', line 87 def model header.model end |