Class: Chicago::Schema::NamedElementCollection
- Inherits:
-
Object
- Object
- Chicago::Schema::NamedElementCollection
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/chicago/schema/named_element_collection.rb
Overview
Stores named elements in a Set-like collection.
Elements must respond to the method name
. Elements will be unique across the collection by name, and can be accessed by name. Elements are not ordered.
Elements can be added, but not removed from the collection.
Instance Method Summary collapse
-
#add(element) ⇒ Object
(also: #<<)
Adds an element to this collection.
-
#contain?(element) ⇒ Boolean
Returns true if an element named element.name is in the collection.
-
#each { ... } ⇒ Object
Iterate over the elements.
-
#initialize(*args) ⇒ NamedElementCollection
constructor
Creates a new collection.
-
#to_a ⇒ Array
Converts this collection to an array.
Constructor Details
#initialize(*args) ⇒ NamedElementCollection
Creates a new collection.
Optionally takes a series of named elements.
29 30 31 32 |
# File 'lib/chicago/schema/named_element_collection.rb', line 29 def initialize(*args) @elements = {} args.each {|e| add(e) } end |
Instance Method Details
#add(element) ⇒ Object Also known as: <<
Adds an element to this collection.
44 45 46 47 |
# File 'lib/chicago/schema/named_element_collection.rb', line 44 def add(element) @elements[element.name] = element element end |
#contain?(element) ⇒ Boolean
Returns true if an element named element.name is in the collection.
52 53 54 |
# File 'lib/chicago/schema/named_element_collection.rb', line 52 def contain?(element) @elements.has_key?(element.name) end |
#each { ... } ⇒ Object
Iterate over the elements.
37 38 39 |
# File 'lib/chicago/schema/named_element_collection.rb', line 37 def each @elements.each {|_,e| yield e } end |
#to_a ⇒ Array
Converts this collection to an array
59 60 61 |
# File 'lib/chicago/schema/named_element_collection.rb', line 59 def to_a @elements.values end |