Class: ROM::Attribute

Inherits:
Object
  • Object
show all
Extended by:
Initializer
Includes:
Memoizable
Defined in:
core/lib/rom/attribute.rb

Overview

Schema attributes provide meta information about types and an API for additional operations. This class can be extended by adapters to provide database-specific features. In example rom-sql provides SQL::Attribute with more features like creating SQL expressions for queries.

Schema attributes are accessible through canonical relation schemas and instance-level schemas.

Constant Summary collapse

META_OPTIONS =
%i[primary_key foreign_key source target relation].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth) ⇒ Object (private)

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.



391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'core/lib/rom/attribute.rb', line 391

def method_missing(meth, ...)
  if type.respond_to?(meth)
    response = type.__send__(meth, ...)

    if response.is_a?(type.class)
      self.class.new(response, **options)
    else
      response
    end
  else
    super
  end
end

Instance Attribute Details

#__memoized__ ⇒ Object (readonly) Originally defined in module Memoizable

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.

#name ⇒ Symbol (readonly)

Return the canonical name of this attribute name

This always returns the name that is used in the datastore, even when an attribute is aliased

Examples:

class Users < ROM::Relation[:memory]
  schema do
    attribute :user_id, Types::Integer, alias: :id
    attribute :email, Types::String
  end
end

users[:user_id].name
# => :user_id

users[:email].name
# => :email

Returns:

  • (Symbol)


54
# File 'core/lib/rom/attribute.rb', line 54

option :name, optional: true, type: Types::Strict::Symbol

#type ⇒ Symbol? (readonly)

Returns Alias to use instead of attribute name.

Returns:

  • (Symbol, nil) —

    Alias to use instead of attribute name



28
# File 'core/lib/rom/attribute.rb', line 28

param :type

Instance Method Details

#[](value = Undefined) ⇒ 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.



61
62
63
# File 'core/lib/rom/attribute.rb', line 61

def [](value = Undefined)
  type[value]
end

#aliased(name) ⇒ Attribute Also known as: as

Return new attribute type with provided alias

Examples:

class Tasks < ROM::Relation[:memory]
  schema do
    attribute :user_id, Types::Integer
    attribute :name, Types::String
  end
end

aliased_user_id = Users.schema[:user_id].aliased(:id)

aliased_user_id.aliased?
# => true

aliased_user_id.name
# => :user_id

aliased_user_id.alias
# => :id

Parameters:

  • name (Symbol) —

    The alias

Returns:



221
# File 'core/lib/rom/attribute.rb', line 221

def aliased(name) = with(alias: name)

#aliased? ⇒ TrueClass, FalseClass

Return true if this attribute has a configured alias

Examples:

class Tasks < ROM::Relation[:memory]
  schema do
    attribute :user_id, Types::Integer, alias: :id
    attribute :name, Types::String
  end
end

Users.schema[:user_id].aliased?
# => true

Users.schema[:name].aliased?
# => false

Returns:

  • (TrueClass, FalseClass)


128
# File 'core/lib/rom/attribute.rb', line 128

def aliased? = !self.alias.nil?

#eql?(other) ⇒ TrueClass, FalseClass

Check if the attribute type is equal to another

Parameters:

Returns:

  • (TrueClass, FalseClass)


315
316
317
# File 'core/lib/rom/attribute.rb', line 315

def eql?(other)
  other.is_a?(self.class) ? super : type.eql?(other)
end

#foreign_key? ⇒ TrueClass, FalseClass

Return true if this attribute type is a foreign key

Examples:

class Tasks < ROM::Relation[:memory]
  schema do
    attribute :id, Types::Integer
    attribute :user_id, Types.ForeignKey(:users)
  end
end

Users.schema[:user_id].foreign_key?
# => true

Users.schema[:id].foreign_key?
# => false

Returns:

  • (TrueClass, FalseClass)


107
# File 'core/lib/rom/attribute.rb', line 107

def foreign_key? = meta[:foreign_key].equal?(true)

#inspect ⇒ String Also known as: pretty_inspect

Return string representation of the attribute type

Returns:

  • (String)


301
302
303
304
305
# File 'core/lib/rom/attribute.rb', line 301

def inspect
  opts = options.reject { |k| %i[type name].include?(k) }
  meta_and_opts = meta.merge(opts).map { |k, v| "#{k}=#{v.inspect}" }
  %(#<#{self.class}[#{type.name}] name=#{name.inspect} #{meta_and_opts.join(' ')}>)
end

#key ⇒ Symbol

Return tuple key

When schemas are projected with aliased attributes, we need a simple access to tuple keys

Examples:

class Tasks < ROM::Relation[:memory]
  schema do
    attribute :user_id, Types::Integer, alias: :id
    attribute :name, Types::String
  end
end

Users.schema[:id].key
# :id

Users.schema.project(Users.schema[:id].aliased(:user_id)).key
# :user_id

Returns:

  • (Symbol)


193
# File 'core/lib/rom/attribute.rb', line 193

def key = self.alias || name

#meta(opts = nil) ⇒ Attribute

Return attribute type with additional meta information

Return meta information hash if no opts are provided

Parameters:

  • opts (Hash) (defaults to: nil) —

    The meta options

Returns:



288
289
290
291
292
293
294
# File 'core/lib/rom/attribute.rb', line 288

def meta(opts = nil)
  if opts
    self.class.new(type.meta(opts), **options)
  else
    type.meta
  end
end

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



379
380
381
382
383
384
# File 'core/lib/rom/attribute.rb', line 379

def meta_options_ast
  keys = %i[wrapped primary_key alias]
  ast = meta.merge(options).select { |k, _| keys.include?(k) }
  ast[:source] = source.to_sym if source
  ast
end

#optional ⇒ Attribute

Return nullable attribute

Returns:



346
347
348
349
350
351
352
353
# File 'core/lib/rom/attribute.rb', line 346

def optional
  sum = self.class.new(super, **options)
  if read?
    sum.meta(read: meta[:read].optional)
  else
    sum
  end
end

#prefixed(prefix = source.dataset) ⇒ Attribute

Return new attribute type with an alias using provided prefix

Examples:

class Users < ROM::Relation[:memory]
  schema do
    attribute :id, Types::Integer
    attribute :name, Types::String
  end
end

prefixed_id = Users.schema[:id].prefixed

prefixed_id.aliased?
# => true

prefixed_id.name
# => :id

prefixed_id.alias
# => :users_id

prefixed_id = Users.schema[:id].prefixed(:user)

prefixed_id.alias
# => :user_id

Parameters:

  • prefix (Symbol) (defaults to: source.dataset) —

    The prefix (defaults to source.dataset)

Returns:



255
256
257
# File 'core/lib/rom/attribute.rb', line 255

def prefixed(prefix = source.dataset)
  aliased(:"#{prefix}_#{name}")
end

#primary_key? ⇒ TrueClass, FalseClass

Return true if this attribute type is a primary key

Examples:

class Users < ROM::Relation[:memory]
  schema do
    attribute :id, Types::Integer
    attribute :name, Types::String

    primary_key :id
  end
end

Users.schema[:id].primary_key?
# => true

Users.schema[:name].primary_key?
# => false

Returns:

  • (TrueClass, FalseClass)


86
# File 'core/lib/rom/attribute.rb', line 86

def primary_key? = meta[:primary_key].equal?(true)

#read? ⇒ TrueClass, FalseClass

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.

Return if this attribute type has additional attribute type for reading tuple values

Returns:

  • (TrueClass, FalseClass)


325
# File 'core/lib/rom/attribute.rb', line 325

def read? = !meta[:read].nil?

#respond_to_missing?(name, include_private = false) ⇒ Boolean

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:

  • (Boolean)


356
357
358
# File 'core/lib/rom/attribute.rb', line 356

def respond_to_missing?(name, include_private = false)
  type.respond_to?(name) || super
end

#source ⇒ Symbol, Relation::Name

Return source relation of this attribute type

Examples:

class Tasks < ROM::Relation[:memory]
  schema do
    attribute :id, Types::Integer
    attribute :user_id, Types.ForeignKey(:users)
  end
end

Users.schema[:id].source
# => :tasks

Users.schema[:user_id].source
# => :tasks

Returns:



149
# File 'core/lib/rom/attribute.rb', line 149

def source = meta[:source]

#target ⇒ NilClass, ...

Return target relation of this attribute type

Examples:

class Tasks < ROM::Relation[:memory]
  schema do
    attribute :id, Types::Integer
    attribute :user_id, Types.ForeignKey(:users)
  end
end

Users.schema[:id].target
# => nil

Users.schema[:user_id].target
# => :users

Returns:



170
# File 'core/lib/rom/attribute.rb', line 170

def target = meta[:target]

#to_ast ⇒ Array

Return AST for the type

Returns:

  • (Array)


365
366
367
# File 'core/lib/rom/attribute.rb', line 365

def to_ast
  [:attribute, [name, type.to_ast(meta: false), meta_options_ast]]
end

#to_read_ast ⇒ Array

Return AST for the read type

Returns:

  • (Array)


374
375
376
# File 'core/lib/rom/attribute.rb', line 374

def to_read_ast
  [:attribute, [name, to_read_type.to_ast(meta: false), meta_options_ast]]
end

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

Return read type

Returns:

  • (Dry::Types::Type)


332
# File 'core/lib/rom/attribute.rb', line 332

def to_read_type = read? ? meta[:read] : type

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

Return write type

Returns:

  • (Dry::Types::Type)


339
# File 'core/lib/rom/attribute.rb', line 339

def to_write_type = type

#wrapped(name = source.dataset) ⇒ Attribute

Return attribute type wrapped for the specified relation name

Parameters:

  • name (Symbol) (defaults to: source.dataset) —

    The name of the source relation (defaults to source.dataset)

Returns:



275
276
277
# File 'core/lib/rom/attribute.rb', line 275

def wrapped(name = source.dataset)
  prefixed(name).meta(wrapped: true)
end

#wrapped? ⇒ Boolean

Return if the attribute type is from a wrapped relation

Wrapped attributes are used when two schemas from different relations are merged together. This way we can identify them easily and handle correctly in places like auto-mapping.

Returns:

  • (Boolean)


266
# File 'core/lib/rom/attribute.rb', line 266

def wrapped? = meta[:wrapped].equal?(true)