Class: ROM::Attribute
- Inherits:
-
Object
- Object
- ROM::Attribute
- Extended by:
- Initializer
- Includes:
- Memoizable
- Defined in:
- 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
Constants included from Memoizable
Instance Attribute Summary collapse
-
#type ⇒ Dry::Types::Definition, ...
readonly
The attribute’s type object.
Attributes included from Memoizable
Instance Method Summary collapse
- #[](input) ⇒ Object private
-
#alias ⇒ NilClass, Symbol
Return attribute’s alias.
-
#aliased(name) ⇒ Attribute
(also: #as)
Return new attribute type with provided alias.
-
#aliased? ⇒ TrueClass, FalseClass
Return true if this attribute type is a foreign key.
-
#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_ast ⇒ Object private
-
#name ⇒ Symbol
Return the canonical name of this attribute name.
-
#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.
Methods included from Initializer
Methods included from Memoizable
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.
422 423 424 425 426 427 428 429 430 431 432 433 434 |
# File 'lib/rom/attribute.rb', line 422 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
#type ⇒ Dry::Types::Definition, ... (readonly)
Returns The attribute’s type object.
24 |
# File 'lib/rom/attribute.rb', line 24 param :type |
Instance Method Details
#[](input) ⇒ 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.
27 28 29 |
# File 'lib/rom/attribute.rb', line 27 def [](input) type[input] end |
#alias ⇒ NilClass, Symbol
Return attribute’s alias
218 219 220 |
# File 'lib/rom/attribute.rb', line 218 def alias [:alias] end |
#aliased(name) ⇒ Attribute Also known as: as
Return new attribute type with provided alias
248 249 250 |
# File 'lib/rom/attribute.rb', line 248 def aliased(name) (alias: name) end |
#aliased? ⇒ TrueClass, FalseClass
Return true if this attribute type is a foreign key
98 99 100 |
# File 'lib/rom/attribute.rb', line 98 def aliased? ![:alias].nil? end |
#eql?(other) ⇒ TrueClass, FalseClass
Check if the attribute type is equal to another
344 345 346 |
# File 'lib/rom/attribute.rb', line 344 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
75 76 77 |
# File 'lib/rom/attribute.rb', line 75 def foreign_key? [:foreign_key].equal?(true) end |
#inspect ⇒ String Also known as: pretty_inspect
Return string representation of the attribute type
332 333 334 |
# File 'lib/rom/attribute.rb', line 332 def inspect %(#<#{self.class}[#{type.name}] #{.map { |k, v| "#{k}=#{v.inspect}" }.join(' ')}>) end |
#key ⇒ Symbol
Return tuple key
When schemas are projected with aliased attributes, we need a simple access to tuple keys
195 196 197 |
# File 'lib/rom/attribute.rb', line 195 def key [:alias] || name end |
#meta(opts = nil) ⇒ Attribute
Return attribute type with additional meta information
Return meta information hash if no opts are provided
319 320 321 322 323 324 325 |
# File 'lib/rom/attribute.rb', line 319 def (opts = nil) if opts self.class.new(type.(opts)) else type. end end |
#meta_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.
410 411 412 413 414 415 |
# File 'lib/rom/attribute.rb', line 410 def = %i(wrapped alias primary_key) ast = .select { |k, _| .include?(k) } ast[:source] = source.to_sym if source ast end |
#name ⇒ Symbol
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
170 171 172 |
# File 'lib/rom/attribute.rb', line 170 def name [:name] end |
#optional ⇒ Attribute
Return nullable attribute
381 382 383 384 |
# File 'lib/rom/attribute.rb', line 381 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
284 285 286 |
# File 'lib/rom/attribute.rb', line 284 def prefixed(prefix = source.dataset) aliased(:"#{prefix}_#{name}") end |
#primary_key? ⇒ TrueClass, FalseClass
Return true if this attribute type is a primary key
52 53 54 |
# File 'lib/rom/attribute.rb', line 52 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
354 355 356 |
# File 'lib/rom/attribute.rb', line 354 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.
387 388 389 |
# File 'lib/rom/attribute.rb', line 387 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
121 122 123 |
# File 'lib/rom/attribute.rb', line 121 def source [:source] end |
#target ⇒ NilClass, ...
Return target relation of this attribute type
144 145 146 |
# File 'lib/rom/attribute.rb', line 144 def target [:target] end |
#to_ast ⇒ Array
Return AST for the type
396 397 398 |
# File 'lib/rom/attribute.rb', line 396 def to_ast [:attribute, [name, type.to_ast(meta: false), ]] end |
#to_read_ast ⇒ Array
Return AST for the read type
405 406 407 |
# File 'lib/rom/attribute.rb', line 405 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
363 364 365 |
# File 'lib/rom/attribute.rb', line 363 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
372 373 374 |
# File 'lib/rom/attribute.rb', line 372 def to_write_type type end |
#wrapped(name = source.dataset) ⇒ Attribute
Return attribute type wrapped for the specified relation name
306 307 308 |
# File 'lib/rom/attribute.rb', line 306 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.
295 296 297 |
# File 'lib/rom/attribute.rb', line 295 def wrapped? [:wrapped].equal?(true) end |