class ROM::ModelBuilder

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.

@example

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

@private

Attributes

const_name[R]
klass[R]
name[R]
namespace[R]

Public Class Methods

[](type) click to toggle source

Return model builder subclass based on type

@param [Symbol] type

@return [Class]

@api private

# File lib/rom/model_builder.rb, line 29
def self.[](type)
  case type
  when :poro then PORO
  else
    raise ArgumentError, "#{type.inspect} is not a supported model type"
  end
end
call(*args) click to toggle source

Build a model class

@return [Class]

@api private

# File lib/rom/model_builder.rb, line 42
def self.call(*args)
  new(*args).call
end
new(options = {}) click to toggle source

@api private

# File lib/rom/model_builder.rb, line 47
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

Public Instance Methods

call(attrs) click to toggle source

Build a model class supporting specific attributes

@return [Class]

@api private

# File lib/rom/model_builder.rb, line 76
def call(attrs)
  define_class(attrs)
  define_const if const_name
  @klass
end
define_const() click to toggle source

Define a model class constant

@api private

# File lib/rom/model_builder.rb, line 67
def define_const
  namespace.const_set(const_name, klass)
end