Module: ROM::Mapper::ModelDSL

Included in:
AttributeDSL
Defined in:
core/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.



11
12
13
# File 'core/lib/rom/mapper/model_dsl.rb', line 11

def attributes
  @attributes
end

#builderObject (readonly)

Returns the value of attribute builder.



11
12
13
# File 'core/lib/rom/mapper/model_dsl.rb', line 11

def builder
  @builder
end

#klassObject (readonly)

Returns the value of attribute klass.



11
12
13
# File 'core/lib/rom/mapper/model_dsl.rb', line 11

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')


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

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