Module: ROM::Mapper::ModelDSL

Included in:
AttributeDSL
Defined in:
lib/rom/mapper/model_dsl.rb

Overview

Model DSL allows setting a model class

Constant Summary collapse

DEFAULT_TYPE =
:poro

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



9
10
11
# File 'lib/rom/mapper/model_dsl.rb', line 9

def attributes
  @attributes
end

#builderObject (readonly)

Returns the value of attribute builder.



9
10
11
# File 'lib/rom/mapper/model_dsl.rb', line 9

def builder
  @builder
end

#klassObject (readonly)

Returns the value of attribute klass.



9
10
11
# File 'lib/rom/mapper/model_dsl.rb', line 9

def klass
  @klass
end

Instance Method Details

#model(options = nil) ⇒ Object

Set or generate a model

Examples:

class MyDefinition
  include ROM::Mapper::ModelDSL

  def initialize
    @attributes = [[:name], [:title]]
  end
end

definition = MyDefinition.new

# just set a model constant
definition.model(User)

# generate model class for the attributes
definition.model(name: 'User')


33
34
35
36
37
38
39
40
41
42
# File 'lib/rom/mapper/model_dsl.rb', line 33

def model(options = nil)
  if options.is_a?(Class)
    @klass = options
  elsif options
    type = options.fetch(:type) { DEFAULT_TYPE }
    @builder = ModelBuilder[type].new(options)
  end

  build_class unless options
end