Class: ROM::Schema::DSL

Inherits:
BasicObject
Extended by:
Initializer
Defined in:
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

Methods included from Initializer

extended

Constructor Details

#initialize(&block) ⇒ DSL

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



55
56
57
58
59
60
61
62
# File 'lib/rom/schema/dsl.rb', line 55

def initialize(*, &block)
  super

  @attributes = {}
  @plugins = {}

  @definition = block
end

Instance Attribute Details

#adapterSymbol (readonly)

Returns The adapter identifier used in gateways.

Returns:

  • (Symbol)

    The adapter identifier used in gateways



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

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

#associations_dslObject (readonly)



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

def associations_dsl
  @associations_dsl
end

#attr_classClass (readonly)

Returns Attribute class that should be used.

Returns:

  • (Class)

    Attribute class that should be used



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

option :attr_class, default: -> { Attribute }

#attributesObject (readonly)



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

def attributes
  @attributes
end

#definitionObject (readonly)



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

def definition
  @definition
end

#inferrerInferrer (readonly)

Returns Optional attribute inferrer.

Returns:

  • (Inferrer)

    Optional attribute inferrer



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

option :inferrer, default: -> { DEFAULT_INFERRER }

#pluginsObject (readonly)



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

def plugins
  @plugins
end

#relationRelation::Name (readonly)

Returns The name of the schema’s relation.

Returns:



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

param :relation

#schema_classClass (readonly)

Returns Schema class that should be instantiated.

Returns:

  • (Class)

    Schema class that should be instantiated



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

option :schema_class, default: -> { Schema }

Instance Method Details

#app_plugin(plugin, options = ::ROM::EMPTY_HASH) ⇒ Object

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.



148
149
150
151
152
# File 'lib/rom/schema/dsl.rb', line 148

def app_plugin(plugin, options = ::ROM::EMPTY_HASH)
  plugin_name = ::ROM.plugin_registry.schemas.adapter(adapter).plugin_name(plugin)
  plugin.extend_dsl(self)
  @plugins[plugin_name] = [plugin, plugin.config.to_hash.merge(options)]
end

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


107
108
109
# File 'lib/rom/schema/dsl.rb', line 107

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

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

Defines a relation attribute with its type

See Also:



69
70
71
72
73
74
75
76
# File 'lib/rom/schema/dsl.rb', line 69

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

  attributes[name] = build_type(name, type, options)
end

#build_type(name, type, options = EMPTY_HASH) ⇒ Dry::Types::Type

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.

Builds a type instance from a name, options and a base type

Returns:

  • (Dry::Types::Type)

    Type instance



116
117
118
119
120
121
122
123
124
# File 'lib/rom/schema/dsl.rb', line 116

def build_type(name, type, options = EMPTY_HASH)
  if options[:read]
    type.meta(name: name, source: relation, read: options[:read])
  elsif type.optional? && !type.meta[:read] && type.right.meta[:read]
    type.meta(name: name, source: relation, read: type.right.meta[:read].optional)
  else
    type.meta(name: name, source: relation)
  end
end

#call(&block) ⇒ Object

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.



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

def call(&block)
  instance_exec(&block) if block
  instance_exec(&definition) if definition

  schema_class.define(relation, opts) do |schema|
    plugins.values.each { |(plugin, options)|
      plugin.apply_to(schema, options)
    }
  end
end

#plugin_options(plugin) ⇒ Object

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.



167
168
169
# File 'lib/rom/schema/dsl.rb', line 167

def plugin_options(plugin)
  @plugins[plugin][1]
end

#primary_key(*names) ⇒ Object

Specify which key(s) should be the primary key



129
130
131
132
133
134
# File 'lib/rom/schema/dsl.rb', line 129

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

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

Enables for the schema

Parameters:

  • plugin (Symbol)

    Plugin name

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

    Plugin options



142
143
144
145
# File 'lib/rom/schema/dsl.rb', line 142

def use(plugin, options = ::ROM::EMPTY_HASH)
  mod = ::ROM.plugin_registry.schemas.adapter(adapter).fetch(plugin)
  app_plugin(mod, options)
end