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.


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rom/model_builder.rb', line 49

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


22
23
24
# File 'lib/rom/model_builder.rb', line 22

def const_name
  @const_name
end

#klassObject (readonly)

Returns the value of attribute klass


22
23
24
# File 'lib/rom/model_builder.rb', line 22

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name


20
21
22
# File 'lib/rom/model_builder.rb', line 20

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace


22
23
24
# File 'lib/rom/model_builder.rb', line 22

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)

31
32
33
34
35
36
37
# File 'lib/rom/model_builder.rb', line 31

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)

44
45
46
# File 'lib/rom/model_builder.rb', line 44

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)

78
79
80
81
82
# File 'lib/rom/model_builder.rb', line 78

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


69
70
71
# File 'lib/rom/model_builder.rb', line 69

def define_const
  namespace.const_set(const_name, klass)
end