Class: ROM::Attribute
- Inherits:
-
Object
- Object
- ROM::Attribute
- 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
-
#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
-
#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.
-
#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.
-
#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.
-
#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.
410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'core/lib/rom/attribute.rb', line 410 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
#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
55 |
# File 'core/lib/rom/attribute.rb', line 55 option :name, optional: true, type: Types::Strict::Symbol |
#type ⇒ Symbol? (readonly)
Returns Alias to use instead of attribute name.
29 |
# File 'core/lib/rom/attribute.rb', line 29 param :type |
Instance Method Details
#aliased(name) ⇒ Attribute Also known as: as
Return new attribute type with provided alias
234 235 236 |
# File 'core/lib/rom/attribute.rb', line 234 def aliased(name) with(alias: name) end |
#aliased? ⇒ TrueClass, FalseClass
Return true if this attribute has a configured alias
133 134 135 |
# File 'core/lib/rom/attribute.rb', line 133 def aliased? !self.alias.nil? end |
#eql?(other) ⇒ TrueClass, FalseClass
Check if the attribute type is equal to another
332 333 334 |
# File 'core/lib/rom/attribute.rb', line 332 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
110 111 112 |
# File 'core/lib/rom/attribute.rb', line 110 def foreign_key? [:foreign_key].equal?(true) end |
#inspect ⇒ String Also known as: pretty_inspect
Return string representation of the attribute type
318 319 320 321 322 |
# File 'core/lib/rom/attribute.rb', line 318 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
204 205 206 |
# File 'core/lib/rom/attribute.rb', line 204 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
305 306 307 308 309 310 311 |
# File 'core/lib/rom/attribute.rb', line 305 def (opts = nil) if opts self.class.new(type.(opts), **) else type. end end |
#optional ⇒ Attribute
Return nullable attribute
369 370 371 372 |
# File 'core/lib/rom/attribute.rb', line 369 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
270 271 272 |
# File 'core/lib/rom/attribute.rb', line 270 def prefixed(prefix = source.dataset) aliased(:"#{prefix}_#{name}") end |
#primary_key? ⇒ TrueClass, FalseClass
Return true if this attribute type is a primary key
87 88 89 |
# File 'core/lib/rom/attribute.rb', line 87 def primary_key? [:primary_key].equal?(true) end |
#source ⇒ Symbol, Relation::Name
Return source relation of this attribute type
156 157 158 |
# File 'core/lib/rom/attribute.rb', line 156 def source [:source] end |
#target ⇒ NilClass, ...
Return target relation of this attribute type
179 180 181 |
# File 'core/lib/rom/attribute.rb', line 179 def target [:target] end |
#to_ast ⇒ Array
Return AST for the type
384 385 386 |
# File 'core/lib/rom/attribute.rb', line 384 def to_ast [:attribute, [name, type.to_ast(meta: false), ]] end |
#to_read_ast ⇒ Array
Return AST for the read type
393 394 395 |
# File 'core/lib/rom/attribute.rb', line 393 def to_read_ast [:attribute, [name, to_read_type.to_ast(meta: false), ]] end |
#wrapped(name = source.dataset) ⇒ Attribute
Return attribute type wrapped for the specified relation name
292 293 294 |
# File 'core/lib/rom/attribute.rb', line 292 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.
281 282 283 |
# File 'core/lib/rom/attribute.rb', line 281 def wrapped? [:wrapped].equal?(true) end |