Class: ROM::Schema::DSL

Inherits:
BasicObject
Defined in:
core/lib/rom/schema/dsl.rb

Overview

Schema DSL exposed as schema { .. } in relation classes

Constant Summary collapse

KERNEL_METHODS =
%i[extend method].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#adapterSymbol (readonly)

Returns The adapter identifier used in gateways.

Returns:

  • (Symbol)

    The adapter identifier used in gateways



38
# File 'core/lib/rom/schema/dsl.rb', line 38

option :adapter, default: -> { :default }

#associations_dslObject (readonly)



57
58
59
# File 'core/lib/rom/schema/dsl.rb', line 57

def associations_dsl
  @associations_dsl
end

#attr_classClass (readonly)

Returns Attribute class that should be used.

Returns:

  • (Class)

    Attribute class that should be used



34
# File 'core/lib/rom/schema/dsl.rb', line 34

option :attr_class, default: -> { Attribute }

#attributesObject (readonly)



45
46
47
# File 'core/lib/rom/schema/dsl.rb', line 45

def attributes
  @attributes
end

#definitionObject (readonly)



53
54
55
# File 'core/lib/rom/schema/dsl.rb', line 53

def definition
  @definition
end

#inferrerInferrer (readonly)

Returns Optional attribute inferrer.

Returns:

  • (Inferrer)

    Optional attribute inferrer



26
# File 'core/lib/rom/schema/dsl.rb', line 26

option :inferrer, default: -> { DEFAULT_INFERRER }

#pluginsObject (readonly)



49
50
51
# File 'core/lib/rom/schema/dsl.rb', line 49

def plugins
  @plugins
end

#relationRelation::Name (readonly)

Returns The name of the schema's relation.

Returns:

  • (Relation::Name)

    The name of the schema's relation



22
# File 'core/lib/rom/schema/dsl.rb', line 22

param :relation

#schema_classClass (readonly)

Returns Schema class that should be instantiated.

Returns:

  • (Class)

    Schema class that should be instantiated



30
# File 'core/lib/rom/schema/dsl.rb', line 30

option :schema_class, default: -> { Schema }

Instance Method Details

#associations(&block) ⇒ AssociationDSL

Define associations for a relation

Examples:

class Users < ROM::Relation[:sql]
  schema(infer: true) do
    associations do
      has_many :tasks
      has_many :posts
      has_many :posts, as: :priority_posts, view: :prioritized
      belongs_to :account
    end
  end
end

class Posts < ROM::Relation[:sql]
  schema(infer: true) do
    associations do
      belongs_to :users, as: :author
    end
  end

  view(:prioritized) do
    where { priority <= 3 }
  end
end

Returns:

  • (AssociationDSL)


117
118
119
# File 'core/lib/rom/schema/dsl.rb', line 117

def associations(&block)
  @associations_dsl = AssociationsDSL.new(relation, &block)
end

#attribute(name, type_or_options, options = EMPTY_HASH) ⇒ Object

Defines a relation attribute with its type and options.

When only options are given, type is left as nil. It makes sense when it is used alongside an schema inferrer, which will populate the type.

See Also:



79
80
81
82
83
84
85
86
# File 'core/lib/rom/schema/dsl.rb', line 79

def attribute(name, type_or_options, options = EMPTY_HASH)
  if attributes.key?(name)
    ::Kernel.raise ::ROM::AttributeAlreadyDefinedError,
                   "Attribute #{name.inspect} already defined"
  end

  attributes[name] = build_attribute_info(name, type_or_options, options)
end

#primary_key(*names) ⇒ Object

Specify which key(s) should be the primary key



158
159
160
161
162
163
164
# File 'core/lib/rom/schema/dsl.rb', line 158

def primary_key(*names)
  names.each do |name|
    attributes[name][:type] =
      attributes[name][:type].meta(primary_key: true)
  end
  self
end

#use(plugin_name, options = ::ROM::EMPTY_HASH) ⇒ Object

Enables for the schema

Parameters:

  • plugin_name (Symbol)

    Plugin name

  • options (Hash) (defaults to: ::ROM::EMPTY_HASH)

    Plugin options



172
173
174
175
# File 'core/lib/rom/schema/dsl.rb', line 172

def use(plugin_name, options = ::ROM::EMPTY_HASH)
  plugin = ::ROM.plugin_registry[:schema].fetch(plugin_name, adapter)
  app_plugin(plugin, options)
end