Class: ROM::Schema::DSL

Inherits:
BasicObject
Extended by:
Initializer
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 block_given?].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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.



58
59
60
61
62
63
64
65
# File 'core/lib/rom/schema/dsl.rb', line 58

def initialize(*, **, &block)
  super

  @attributes = {}
  @plugins = {}

  @definition = block
end

Instance Attribute Details

#adapter ⇒ Symbol (readonly)

Returns The adapter identifier used in gateways.

Returns:

  • (Symbol) —

    The adapter identifier used in gateways



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

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

#associations_dsl ⇒ Object (readonly)



55
56
57
# File 'core/lib/rom/schema/dsl.rb', line 55

def associations_dsl
  @associations_dsl
end

#attr_class ⇒ Class (readonly)

Returns Attribute class that should be used.

Returns:

  • (Class) —

    Attribute class that should be used



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

option :attr_class, default: -> { Attribute }

#attributes ⇒ Object (readonly)



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

def attributes
  @attributes
end

#definition ⇒ Object (readonly)



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

def definition
  @definition
end

#inferrer ⇒ Inferrer (readonly)

Returns Optional attribute inferrer.

Returns:

  • (Inferrer) —

    Optional attribute inferrer



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

option :inferrer, default: -> { DEFAULT_INFERRER }

#plugins ⇒ Object (readonly)



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

def plugins
  @plugins
end

#relation ⇒ Relation::Name (readonly)

Returns The name of the schema's relation.

Returns:



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

param :relation

#schema_class ⇒ Class (readonly)

Returns Schema class that should be instantiated.

Returns:

  • (Class) —

    Schema class that should be instantiated



28
# File 'core/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.



183
184
185
186
# File 'core/lib/rom/schema/dsl.rb', line 183

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

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


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

def associations(&)
  if associations_dsl
    associations_dsl.instance_exec(&)
  else
    @associations_dsl = AssociationsDSL.new(relation, &)
  end
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:



76
77
78
79
80
81
82
83
# File 'core/lib/rom/schema/dsl.rb', line 76

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

#build_attribute_info(name, type_or_options, options = EMPTY_HASH) ⇒ Hash

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 representation of the information needed to create an attribute. It returns a hash with :type and :options keys.

Returns:

  • (Hash)

See Also:

  • ROM::Schema::DSL.[Schema[Schema.build_attribute_info]


130
131
132
133
134
135
136
137
138
139
# File 'core/lib/rom/schema/dsl.rb', line 130

def build_attribute_info(name, type_or_options, options = EMPTY_HASH)
  type, options = if type_or_options.is_a?(::Hash)
                    [nil, type_or_options]
                  else
                    [build_type(type_or_options, options), options]
                  end
  Schema.build_attribute_info(
    type, **options, name: name
  )
end

#build_type(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 base type and meta options

rubocop:disable Metrics/AbcSize

Returns:

  • (Dry::Types::Type) —

    Type instance



147
148
149
150
151
152
153
154
155
156
157
# File 'core/lib/rom/schema/dsl.rb', line 147

def build_type(type, options = EMPTY_HASH)
  if options[:read]
    type.meta(source: relation, read: options[:read])
  elsif type.optional? && type.meta[:read]
    type.meta(source: relation, read: type.meta[:read].optional)
  else
    type.meta(source: relation)
  end.meta(Attribute::META_OPTIONS.map { |opt|
             [opt, options[opt]] if options.key?(opt)
           }.compact.to_h)
end

#call ⇒ 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.



189
190
191
192
193
194
195
196
197
198
# File 'core/lib/rom/schema/dsl.rb', line 189

def call(&)
  instance_exec(&) if block_given?
  instance_exec(&definition) if definition

  schema_class.define(relation, **opts) do |schema|
    plugins.each_value do |plugin, options|
      plugin.apply_to(schema, **options)
    end
  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.



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

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

#primary_key(*names) ⇒ Object

Specify which key(s) should be the primary key



163
164
165
166
167
168
169
# File 'core/lib/rom/schema/dsl.rb', line 163

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



177
178
179
180
# File 'core/lib/rom/schema/dsl.rb', line 177

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