Class: ROM::Mapping::Definition

Inherits:
Object
  • Object
show all
Includes:
Adamantium::Flat
Defined in:
lib/rom/mapping/definition.rb

Overview

Mapping definition DSL

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header, &block) ⇒ undefined

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.

Initialize a new Definition instance



27
28
29
30
31
32
33
34
35
# File 'lib/rom/mapping/definition.rb', line 27

def initialize(header, &block)
  @header     = header
  @mapping    = {}
  @attributes = Set.new

  instance_eval(&block)

  build_mapper unless mapper
end

Class Method Details

.build(header, &block) ⇒ 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.

Build new mapping definition



18
19
20
# File 'lib/rom/mapping/definition.rb', line 18

def self.build(header, &block)
  new(header, &block)
end

Instance Method Details

#headerObject

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.



38
39
40
# File 'lib/rom/mapping/definition.rb', line 38

def header
  @header.project(attributes + Set[*mapping.keys])
end

#map(*args) ⇒ Definition

Configure attribute mappings

Examples:


Mapping.build do
  users do
    map :id, :email
    map :user_name, to: :name
  end
end

Returns:



105
106
107
108
109
110
111
112
113
# File 'lib/rom/mapping/definition.rb', line 105

def map(*args)
  options = args.last

  if options.kind_of?(Hash)
    mapping.update(args.first => options[:to])
  else
    @attributes += Set[*args]
  end
end

#mapper(mapper = Undefined) ⇒ Object

Get or set mapper

Examples:


Mapping.build do
  users do
    mapper my_custom_mapper
  end
end

Parameters:

  • (Object)

Returns:

  • (Object)


58
59
60
61
62
63
64
# File 'lib/rom/mapping/definition.rb', line 58

def mapper(mapper = Undefined)
  if mapper == Undefined
    @mapper
  else
    @mapper = mapper
  end
end

#model(model = Undefined) ⇒ Class

Get or set model for the mapper

Examples:


Mapping.build do
  users do
    model User
  end
end

Parameters:

  • (Class)

Returns:

  • (Class)


81
82
83
84
85
86
87
# File 'lib/rom/mapping/definition.rb', line 81

def model(model = Undefined)
  if model == Undefined
    @model
  else
    @model = model
  end
end