Class: ROM::ModelBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rom/model_builder.rb

Overview

Model builders can be used to build model classes for mappers

This is used when you define a mapper and setup a model using :name option.

Examples:

# this will define User model for you
class UserMapper < ROM::Mapper
  model name: 'User'
  attribute :id
  attribute :name
end

Direct Known Subclasses

PORO

Defined Under Namespace

Classes: PORO

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ModelBuilder

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 ModelBuilder.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rom/model_builder.rb', line 45

def initialize(options = {})
  @name = options[:name]

  if name
    parts = name.split('::')

    @const_name = parts.pop

    @namespace =
      if parts.any?
        Inflector.constantize(parts.join('::'))
      else
        Object
      end
  end
end

Instance Attribute Details

#const_nameObject (readonly)

Returns the value of attribute const_name.



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

def const_name
  @const_name
end

#klassObject (readonly)

Returns the value of attribute klass.



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

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/rom/model_builder.rb', line 16

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

Class Method Details

.[](type) ⇒ Class

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.

Return model builder subclass based on type

Parameters:

  • type (Symbol)

Returns:

  • (Class)


27
28
29
30
31
32
33
# File 'lib/rom/model_builder.rb', line 27

def self.[](type)
  case type
  when :poro then PORO
  else
    raise ArgumentError, "#{type.inspect} is not a supported model type"
  end
end

.call(*args) ⇒ Class

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 model class

Returns:

  • (Class)


40
41
42
# File 'lib/rom/model_builder.rb', line 40

def self.call(*args)
  new(*args).call
end

Instance Method Details

#call(attrs) ⇒ Class

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 model class supporting specific attributes

Returns:

  • (Class)


74
75
76
77
78
# File 'lib/rom/model_builder.rb', line 74

def call(attrs)
  define_class(attrs)
  define_const if const_name
  @klass
end

#define_constObject

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.

Define a model class constant



65
66
67
# File 'lib/rom/model_builder.rb', line 65

def define_const
  namespace.const_set(const_name, klass)
end