Class: Virtus::AttributeSet
- Inherits:
-
Object
- Object
- Virtus::AttributeSet
- Includes:
- Enumerable
- Defined in:
- lib/virtus/attribute_set.rb
Overview
A set of Attribute objects
Instance Attribute Summary collapse
-
#parent ⇒ AttributeSet?
readonly
private
Return the parent attributes.
Instance Method Summary collapse
-
#<<(attribute) ⇒ self
Adds an attribute to the set.
-
#[](name) ⇒ Attribute
Get an attribute by name.
-
#[]=(name, attribute) ⇒ Attribute
Set an attribute by name.
-
#each {|attribute| ... } ⇒ self
Iterate over each attribute in the set.
-
#initialize(parent = nil, attributes = []) ⇒ undefined
constructor
private
Initialize an AttributeSet.
-
#merge(attributes) ⇒ self
Adds the attributes to the set.
-
#reset ⇒ self
private
Reset the index when the parent is updated.
Constructor Details
#initialize(parent = nil, attributes = []) ⇒ undefined
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.
Initialize an AttributeSet
26 27 28 29 30 31 32 |
# File 'lib/virtus/attribute_set.rb', line 26 def initialize(parent = nil, attributes = []) @parent = parent @attributes = attributes.dup @index = {} @string_index = {} reset end |
Instance Attribute Details
#parent ⇒ AttributeSet? (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.
Return the parent attributes
16 17 18 |
# File 'lib/virtus/attribute_set.rb', line 16 def parent @parent end |
Instance Method Details
#<<(attribute) ⇒ self
Adds an attribute to the set
79 80 81 82 |
# File 'lib/virtus/attribute_set.rb', line 79 def <<(attribute) self[attribute.name] = attribute self end |
#[](name) ⇒ Attribute
Get an attribute by name
94 95 96 |
# File 'lib/virtus/attribute_set.rb', line 94 def [](name) @index.fetch(name) { @string_index[name] } end |
#[]=(name, attribute) ⇒ Attribute
Set an attribute by name
109 110 111 112 |
# File 'lib/virtus/attribute_set.rb', line 109 def []=(name, attribute) @attributes << attribute update_index(name, attribute) end |
#each {|attribute| ... } ⇒ self
Iterate over each attribute in the set
48 49 50 51 52 |
# File 'lib/virtus/attribute_set.rb', line 48 def each return to_enum unless block_given? @index.each_value { |attribute| yield attribute } self end |
#merge(attributes) ⇒ self
Adds the attributes to the set
64 65 66 67 |
# File 'lib/virtus/attribute_set.rb', line 64 def merge(attributes) attributes.each { |attribute| self << attribute } self end |
#reset ⇒ self
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.
Reset the index when the parent is updated
119 120 121 122 123 124 |
# File 'lib/virtus/attribute_set.rb', line 119 def reset parent = self.parent merge_attributes(parent) if parent merge_attributes(@attributes) self end |