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 collapse
- META_OPTIONS =
i[primary_key foreign_key source target relation].freeze
Constants included from Memoizable
Instance Attribute Summary collapse
-
#name ⇒ Symbol
readonly
Return the canonical name of this attribute name.
-
#type ⇒ Symbol?
readonly
Alias to use instead of attribute name.
Attributes included from Memoizable
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.
Methods included from Initializer
Methods included from Memoizable
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.
387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/rom/attribute.rb', line 387 def method_missing(meth, ...) if type.respond_to?(meth) response = type.__send__(meth, ...) if response.is_a?(type.class) self.class.new(response, **) else response end else super end end |
Instance Attribute Details
#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
54 |
# File '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.
28 |
# File '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 '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
221 |
# File 'lib/rom/attribute.rb', line 221 def aliased(name) = with(alias: name) |
#aliased? ⇒ TrueClass, FalseClass
Return true if this attribute has a configured alias
128 |
# File 'lib/rom/attribute.rb', line 128 def aliased? = !self.alias.nil? |
#eql?(other) ⇒ TrueClass, FalseClass
Check if the attribute type is equal to another
315 316 317 |
# File '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
107 |
# File 'lib/rom/attribute.rb', line 107 def foreign_key? = [:foreign_key].equal?(true) |
#inspect ⇒ String Also known as: pretty_inspect
Return string representation of the attribute type
301 302 303 304 305 |
# File 'lib/rom/attribute.rb', line 301 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} #{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
193 |
# File '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
288 289 290 291 292 293 294 |
# File 'lib/rom/attribute.rb', line 288 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.
375 376 377 378 379 380 |
# File 'lib/rom/attribute.rb', line 375 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
346 347 348 349 |
# File 'lib/rom/attribute.rb', line 346 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
255 256 257 |
# File '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
86 |
# File 'lib/rom/attribute.rb', line 86 def primary_key? = [: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
325 |
# File 'lib/rom/attribute.rb', line 325 def read? = ![: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.
352 353 354 |
# File 'lib/rom/attribute.rb', line 352 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
149 |
# File 'lib/rom/attribute.rb', line 149 def source = [:source] |
#target ⇒ NilClass, ...
Return target relation of this attribute type
170 |
# File 'lib/rom/attribute.rb', line 170 def target = [:target] |
#to_ast ⇒ Array
Return AST for the type
361 362 363 |
# File 'lib/rom/attribute.rb', line 361 def to_ast [:attribute, [name, type.to_ast(meta: false), ]] end |
#to_read_ast ⇒ Array
Return AST for the read type
370 371 372 |
# File 'lib/rom/attribute.rb', line 370 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
332 |
# File 'lib/rom/attribute.rb', line 332 def to_read_type = read? ? [: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
339 |
# File '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
275 276 277 |
# File 'lib/rom/attribute.rb', line 275 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.
266 |
# File 'lib/rom/attribute.rb', line 266 def wrapped? = [:wrapped].equal?(true) |