Class: ROM::Attribute
- Inherits:
-
Object
- Object
- ROM::Attribute
- 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
- #__memoized__ ⇒ Object included from Memoizable readonly private
-
#name ⇒ Symbol
readonly
Return the canonical name of this attribute name.
-
#type ⇒ Symbol?
readonly
Alias to use instead of attribute name.
Instance Method Summary collapse
- #[](value = Undefined) ⇒ Object private
-
#aliased(name) ⇒ Attribute
(also: #as)
Return new attribute type with provided alias.
-
#aliased? ⇒ TrueClass, FalseClass
Return true if this attribute has a configured alias.
-
#eql?(other) ⇒ TrueClass, FalseClass
Check if the attribute type is equal to another.
-
#foreign_key? ⇒ TrueClass, FalseClass
Return true if this attribute type is a foreign key.
-
#inspect ⇒ String
(also: #pretty_inspect)
Return string representation of the attribute type.
-
#key ⇒ Symbol
Return tuple key.
-
#meta(opts = nil) ⇒ Attribute
Return attribute type with additional meta information.
- #meta_options_ast ⇒ Object private
-
#optional ⇒ Attribute
Return nullable attribute.
-
#prefixed(prefix = source.dataset) ⇒ Attribute
Return new attribute type with an alias using provided prefix.
-
#primary_key? ⇒ TrueClass, FalseClass
Return true if this attribute type is a primary key.
-
#read? ⇒ TrueClass, FalseClass
private
Return if this attribute type has additional attribute type for reading tuple values.
- #respond_to_missing?(name, include_private = false) ⇒ Boolean private
-
#source ⇒ Symbol, Relation::Name
Return source relation of this attribute type.
-
#target ⇒ NilClass, ...
Return target relation of this attribute type.
-
#to_ast ⇒ Array
Return AST for the type.
-
#to_read_ast ⇒ Array
Return AST for the read type.
-
#to_read_type ⇒ Dry::Types::Type
private
Return read type.
-
#to_write_type ⇒ Dry::Types::Type
private
Return write type.
-
#wrapped(name = source.dataset) ⇒ Attribute
Return attribute type wrapped for the specified relation name.
-
#wrapped? ⇒ Boolean
Return if the attribute type is from a wrapped relation.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ 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.
408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'core/lib/rom/attribute.rb', line 408 def method_missing(meth, *args, &block) if type.respond_to?(meth) response = type.__send__(meth, *args, &block) if response.is_a?(type.class) self.class.new(response, **) 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
53 |
# File 'core/lib/rom/attribute.rb', line 53 option :name, optional: true, type: Types::Strict::Symbol |
#type ⇒ Symbol? (readonly)
Returns Alias to use instead of attribute name.
27 |
# File 'core/lib/rom/attribute.rb', line 27 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.
60 61 62 |
# File 'core/lib/rom/attribute.rb', line 60 def [](value = Undefined) type[value] end |
#aliased(name) ⇒ Attribute Also known as: as
Return new attribute type with provided alias
232 233 234 |
# File 'core/lib/rom/attribute.rb', line 232 def aliased(name) with(alias: name) end |
#aliased? ⇒ TrueClass, FalseClass
Return true if this attribute has a configured alias
131 132 133 |
# File 'core/lib/rom/attribute.rb', line 131 def aliased? !self.alias.nil? end |
#eql?(other) ⇒ TrueClass, FalseClass
Check if the attribute type is equal to another
330 331 332 |
# File 'core/lib/rom/attribute.rb', line 330 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
108 109 110 |
# File 'core/lib/rom/attribute.rb', line 108 def foreign_key? [:foreign_key].equal?(true) end |
#inspect ⇒ String Also known as: pretty_inspect
Return string representation of the attribute type
316 317 318 319 320 |
# File 'core/lib/rom/attribute.rb', line 316 def inspect opts = .reject { |k| %i[type name].include?(k) } = .merge(opts).map { |k, v| "#{k}=#{v.inspect}" } %(#<#{self.class}[#{type.name}] name=#{name.inspect} #{.join(' ')}>) end |
#key ⇒ Symbol
Return tuple key
When schemas are projected with aliased attributes, we need a simple access to tuple keys
202 203 204 |
# File 'core/lib/rom/attribute.rb', line 202 def key self.alias || name end |
#meta(opts = nil) ⇒ Attribute
Return attribute type with additional meta information
Return meta information hash if no opts are provided
303 304 305 306 307 308 309 |
# File 'core/lib/rom/attribute.rb', line 303 def (opts = nil) if opts self.class.new(type.(opts), **) else type. 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.
396 397 398 399 400 401 |
# File 'core/lib/rom/attribute.rb', line 396 def keys = %i[wrapped primary_key alias] ast = .merge().select { |k, _| keys.include?(k) } ast[:source] = source.to_sym if source ast end |
#optional ⇒ Attribute
Return nullable attribute
367 368 369 370 |
# File 'core/lib/rom/attribute.rb', line 367 def optional sum = self.class.new(super, **) read? ? sum.(read: [:read].optional) : sum end |
#prefixed(prefix = source.dataset) ⇒ Attribute
Return new attribute type with an alias using provided prefix
268 269 270 |
# File 'core/lib/rom/attribute.rb', line 268 def prefixed(prefix = source.dataset) aliased(:"#{prefix}_#{name}") end |
#primary_key? ⇒ TrueClass, FalseClass
Return true if this attribute type is a primary key
85 86 87 |
# File 'core/lib/rom/attribute.rb', line 85 def primary_key? [:primary_key].equal?(true) end |
#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
340 341 342 |
# File 'core/lib/rom/attribute.rb', line 340 def read? ![:read].nil? end |
#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.
373 374 375 |
# File 'core/lib/rom/attribute.rb', line 373 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
154 155 156 |
# File 'core/lib/rom/attribute.rb', line 154 def source [:source] end |
#target ⇒ NilClass, ...
Return target relation of this attribute type
177 178 179 |
# File 'core/lib/rom/attribute.rb', line 177 def target [:target] end |
#to_ast ⇒ Array
Return AST for the type
382 383 384 |
# File 'core/lib/rom/attribute.rb', line 382 def to_ast [:attribute, [name, type.to_ast(meta: false), ]] end |
#to_read_ast ⇒ Array
Return AST for the read type
391 392 393 |
# File 'core/lib/rom/attribute.rb', line 391 def to_read_ast [:attribute, [name, to_read_type.to_ast(meta: false), ]] 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
349 350 351 |
# File 'core/lib/rom/attribute.rb', line 349 def to_read_type read? ? [:read] : type end |
#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
358 359 360 |
# File 'core/lib/rom/attribute.rb', line 358 def to_write_type type end |
#wrapped(name = source.dataset) ⇒ Attribute
Return attribute type wrapped for the specified relation name
290 291 292 |
# File 'core/lib/rom/attribute.rb', line 290 def wrapped(name = source.dataset) prefixed(name).(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.
279 280 281 |
# File 'core/lib/rom/attribute.rb', line 279 def wrapped? [:wrapped].equal?(true) end |