class ROM::Mapping::Definition

Mapping definition DSL

@private

Attributes

attributes[R]
mapping[R]

Public Class Methods

build(header, &block) click to toggle source

Build new mapping definition

@api private

# File lib/rom/mapping/definition.rb, line 18
def self.build(header, &block)
  new(header, &block)
end
new(header, &block) click to toggle source

Initialize a new Definition instance

@return [undefined]

@api private

# 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

Public Instance Methods

header() click to toggle source

@api private

# File lib/rom/mapping/definition.rb, line 38
def header
  @header.project(attributes + Set[*mapping.keys])
end
map(*args) click to toggle source

Configure attribute mappings

@example

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

@params [Array<Symbol>,Symbol,Hash]

@return [Definition]

@api public

# 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) click to toggle source

Get or set mapper

@example

Mapping.build do
  users do
    mapper my_custom_mapper
  end
end

@param [Object]

@return [Object]

@api public

# File lib/rom/mapping/definition.rb, line 58
def mapper(mapper = Undefined)
  if mapper == Undefined
    @mapper
  else
    @mapper = mapper
  end
end
model(model = Undefined) click to toggle source

Get or set model for the mapper

@example

Mapping.build do
  users do
    model User
  end
end

@param [Class]

@return [Class]

@api public

# File lib/rom/mapping/definition.rb, line 81
def model(model = Undefined)
  if model == Undefined
    @model
  else
    @model = model
  end
end

Private Instance Methods

build_mapper() click to toggle source

Build default rom mapper

@api private

# File lib/rom/mapping/definition.rb, line 120
def build_mapper
  @mapper = Mapper.build(header, model, map: mapping)
end