Class: ROM::Header::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/rom/header/attribute.rb

Overview

An attribute provides information about a specific attribute in a tuple

This may include information about how an attribute should be renamed, or how its value should coerced.

More complex attributes describe how an attribute should be transformed.

Direct Known Subclasses

Embedded

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, meta) ⇒ Attribute

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 a new instance of Attribute.

API:

  • private



67
68
69
70
71
72
# File 'lib/rom/header/attribute.rb', line 67

def initialize(name, meta)
  @name = name
  @meta = meta
  @key = meta.fetch(:from) { name }
  @type = meta.fetch(:type)
end

Instance Attribute Details

#keySymbol (readonly)

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 key of an attribute that corresponds to tuple attribute.

Returns:

  • key of an attribute that corresponds to tuple attribute

API:

  • private



22
23
24
# File 'lib/rom/header/attribute.rb', line 22

def key
  @key
end

#metaHash (readonly)

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 additional meta information.

Returns:

  • additional meta information

API:

  • private



32
33
34
# File 'lib/rom/header/attribute.rb', line 32

def meta
  @meta
end

#nameSymbol (readonly)

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 name of an attribute.

Returns:

  • name of an attribute

API:

  • private



17
18
19
# File 'lib/rom/header/attribute.rb', line 17

def name
  @name
end

#typeSymbol (readonly)

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 type identifier (defaults to :object).

Returns:

  • type identifier (defaults to :object)

API:

  • private



27
28
29
# File 'lib/rom/header/attribute.rb', line 27

def type
  @type
end

Class Method Details

.[](meta) ⇒ Class

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 attribute class for a given meta hash

Parameters:

  • hash with type information and optional transformation info

Returns:

API:

  • private



41
42
43
44
# File 'lib/rom/header/attribute.rb', line 41

def self.[](meta)
  key = (meta.keys & TYPE_MAP.keys).first
  TYPE_MAP.fetch(key || meta[:type], self)
end

.coerce(input) ⇒ Attribute

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.

Coerce an array with attribute meta-data into an attribute object

Parameters:

  • attribute name/options pair

Returns:

API:

  • private



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rom/header/attribute.rb', line 53

def self.coerce(input)
  name = input[0]
  meta = (input[1] || {}).dup

  meta[:type] ||= :object

  if meta.key?(:header)
    meta[:header] = Header.coerce(meta[:header], model: meta[:model])
  end

  self[meta].new(name, meta)
end

Instance Method Details

#aliased?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.

Return if an attribute should be aliased

Returns:

API:

  • private



84
85
86
# File 'lib/rom/header/attribute.rb', line 84

def aliased?
  key != name
end

#mappingHash

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 :key-to-:name mapping hash

Returns:

API:

  • private



93
94
95
# File 'lib/rom/header/attribute.rb', line 93

def mapping
  { key => name }
end

#typed?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.

Return if an attribute has a specific type identifier

Returns:

API:

  • private



77
78
79
# File 'lib/rom/header/attribute.rb', line 77

def typed?
  type != :object
end

#union?Boolean

Returns:



97
98
99
# File 'lib/rom/header/attribute.rb', line 97

def union?
  key.is_a? ::Array
end