Class: ROM::Mapper

Inherits:
Object
  • Object
show all
Includes:
DSL
Defined in:
lib/rom/mapper.rb,
lib/rom/mapper/dsl.rb,
lib/rom/mapper/builder.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, Builder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

included

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.

API:

  • private



77
78
79
80
81
82
83
# File 'lib/rom/mapper.rb', line 77

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

#headerHeader (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.

Returns:

  • header that was used to build the transformers

API:

  • private



29
30
31
# File 'lib/rom/mapper.rb', line 29

def header
  @header
end

#transformersObject (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.

Returns:

  • transformers object built by a processor

API:

  • private



24
25
26
# File 'lib/rom/mapper.rb', line 24

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

Returns:

API:

  • private



64
65
66
# File 'lib/rom/mapper.rb', line 64

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

Returns:

Raises:

API:

  • private



53
54
55
56
57
# File 'lib/rom/mapper.rb', line 53

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

.processorsHash

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.

Returns:

  • registered processors

API:

  • private



34
35
36
# File 'lib/rom/mapper.rb', line 34

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

Returns:

API:

  • private



43
44
45
46
# File 'lib/rom/mapper.rb', line 43

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.

API:

  • private



69
70
71
72
73
74
# File 'lib/rom/mapper.rb', line 69

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

API:

  • private



95
96
97
# File 'lib/rom/mapper.rb', line 95

def call(relation)
  transformers.reduce(relation.to_a) { |a, e| e.call(a) }
end

#modelClass

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.

Returns:

  • optional model that is instantiated by a mapper

API:

  • private



88
89
90
# File 'lib/rom/mapper.rb', line 88

def model
  header.model
end