Class: Virtus::AttributeSet
- Inherits:
-
Object
- Object
- Virtus::AttributeSet
- Includes:
- Enumerable
- Defined in:
- lib/virtus/attribute_set.rb
Overview
A set of Attribute objects
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
15 16 17 18 19 20 |
# File 'lib/virtus/attribute_set.rb', line 15 def initialize(parent = nil, attributes = []) @parent = parent @attributes = attributes.dup @index = {} reset end |
Instance Method Details
#<<(attribute) ⇒ self
Adds an attribute to the set
67 68 69 70 |
# File 'lib/virtus/attribute_set.rb', line 67 def <<(attribute) self[attribute.name] = attribute self end |
#[](name) ⇒ Attribute
Get an attribute by name
82 83 84 |
# File 'lib/virtus/attribute_set.rb', line 82 def [](name) @index[name] end |
#[]=(name, attribute) ⇒ Attribute
Set an attribute by name
97 98 99 100 |
# File 'lib/virtus/attribute_set.rb', line 97 def []=(name, attribute) @attributes << attribute update_index(name, attribute) end |
#each {|attribute| ... } ⇒ self
Iterate over each attribute in the set
36 37 38 39 40 |
# File 'lib/virtus/attribute_set.rb', line 36 def each return to_enum unless block_given? @index.values.uniq.each { |attribute| yield attribute } self end |
#merge(attributes) ⇒ self
Adds the attributes to the set
52 53 54 55 |
# File 'lib/virtus/attribute_set.rb', line 52 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
107 108 109 110 111 |
# File 'lib/virtus/attribute_set.rb', line 107 def reset merge_attributes(@parent) if @parent merge_attributes(@attributes) self end |