Class: ROM::Schema::DSL Deprecated

Inherits:
Object
  • Object
show all
Extended by:
Initializer
Defined in:
lib/rom/compat/schema/dsl.rb

Overview

Deprecated.

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

Instance Attribute Summary collapse

Class Method 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



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

option :adapter

#attr_classClass (readonly)

Returns Attribute class that should be used.

Returns:

  • (Class)

    Attribute class that should be used



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

option :attr_class, default: -> { Attribute }

#attributesHash<Symbol, Hash> (readonly)

keys and attribute representations as values.

Returns:

  • (Hash<Symbol, Hash>)

    A hash with attribute names as

See Also:

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


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

option :attributes, default: -> { EMPTY_HASH.dup }

#definitionClass (readonly)

Returns An optional block that will be evaluated as part of this DSL.

Returns:

  • (Class)

    An optional block that will be evaluated as part of this DSL



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

option :definition, type: Types.Instance(Proc), default: -> { proc {} }

#inflectorDry::Inflector (readonly)

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 String inflector.

Returns:

  • (Dry::Inflector)

    String inflector



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

option :inflector, default: -> { Inflector }

#pluginsArray<Plugin> (readonly)

Returns:



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

option :plugins, default: -> { EMPTY_ARRAY }

#relationRelation::Name (readonly)

Returns The name of the schema's relation.

Returns:



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

option :relation

Class Method Details

.new(**options, &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.



60
61
62
63
64
65
66
# File 'lib/rom/compat/schema/dsl.rb', line 60

def self.new(**options, &block)
  if block
    super(definition: block, **options)
  else
    super
  end
end

Instance Method Details

#__assoc_dsl__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.



138
139
140
# File 'lib/rom/compat/schema/dsl.rb', line 138

def __assoc_dsl__
  @__assoc_dsl__ ||= AssociationsDSL.new(relation, inflector)
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)


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

def associations(&block)
  if block
    __assoc_dsl__.instance_eval(&block)
  else
    __assoc_dsl__.registry.values
  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 a schema inferrer, which will populate the type.

See Also:



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rom/compat/schema/dsl.rb', line 77

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

  build_attribute_info(name, type_or_options, options).tap do |attr_info|
    attributes[name] = attr_info
  end
end

#callObject

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.



164
165
166
# File 'lib/rom/compat/schema/dsl.rb', line 164

def call
  schema_class.define(relation, **config)
end

#configObject

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.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/rom/compat/schema/dsl.rb', line 169

def config
  @config ||=
    begin
      # Enable available plugin's
      plugins.each do |plugin|
        next unless plugin.type == :schema
        plugin.enable(self) unless plugin.enabled?
      end

      # Apply custom definition block if it exists
      instance_eval(&definition) if definition

      # Apply plugin defaults
      plugins.each do |plugin|
        next unless plugin.type == :schema
        plugin.__send__(:apply_to, self)
      end

      attributes.freeze
      associations.freeze

      opts.freeze
    end
end

#inspectObject



195
196
197
# File 'lib/rom/compat/schema/dsl.rb', line 195

def inspect
  %(<##{self.class} relation=#{relation} attributes=#{attributes} plugins=#{plugins}>)
end

#plugin(name, **options) ⇒ Object



157
158
159
160
161
# File 'lib/rom/compat/schema/dsl.rb', line 157

def plugin(name, **options)
  plugin = plugins.detect { |pl| pl.name == name }
  plugin.config.update(options) unless options.empty?
  plugin
end

#primary_key(*names) ⇒ Object

Specify which key(s) should be the primary key



93
94
95
96
97
98
# File 'lib/rom/compat/schema/dsl.rb', line 93

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

#use(name, **options) ⇒ Object

Enable a plugin in the schema DSL

Parameters:

  • name (Symbol)

    Plugin name

  • options (Hash)

    Plugin options



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

def use(name, **options)
  plugin = ::ROM.plugins[:schema].fetch(name, adapter).configure do |config|
    config.update(options)
  end
  plugins << plugin.enable(self)
  self
end