Class: Chicago::Schema::NamedElementCollection

Inherits:
Object
  • Object
show all
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

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.

Returns:

  • the element just added



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.

Returns:

  • (Boolean)


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.

Yields:

  • element



37
38
39
# File 'lib/chicago/schema/named_element_collection.rb', line 37

def each
  @elements.each {|_,e| yield e }
end

#to_aArray

Converts this collection to an array

Returns:

  • (Array)


59
60
61
# File 'lib/chicago/schema/named_element_collection.rb', line 59

def to_a
  @elements.values
end