Class: Golem::Util::ElementCollection
- Inherits:
-
Object
- Object
- Golem::Util::ElementCollection
- Includes:
- Enumerable
- Defined in:
- lib/golem/util/element_collection.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(restricted_to_type = nil) ⇒ ElementCollection
constructor
A new instance of ElementCollection.
- #values ⇒ Object
Constructor Details
#initialize(restricted_to_type = nil) ⇒ ElementCollection
Returns a new instance of ElementCollection.
6 7 8 9 |
# File 'lib/golem/util/element_collection.rb', line 6 def initialize(restricted_to_type = nil) @collection = {} @restricted_to_type = restricted_to_type end |
Instance Method Details
#[](key) ⇒ Object
11 12 13 14 15 |
# File 'lib/golem/util/element_collection.rb', line 11 def [](key) return nil if key.nil? key = key.name if key.respond_to?(:name) @collection[key.to_sym] end |
#[]=(key, value) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/golem/util/element_collection.rb', line 17 def []=(key, value) key = key.name if key.respond_to?(:name) raise ArgumentError, "Value must be a #{@restricted_to_type.name.inspect} but is a #{value.class.name.inspect}!" if @restricted_to_type && !value.kind_of?(@restricted_to_type) @collection[key.to_sym] = value end |
#each ⇒ Object
24 25 26 |
# File 'lib/golem/util/element_collection.rb', line 24 def each @collection.values.each{|v| yield v} end |
#empty? ⇒ Boolean
28 29 30 |
# File 'lib/golem/util/element_collection.rb', line 28 def empty? @collection.empty? end |
#values ⇒ Object
32 33 34 |
# File 'lib/golem/util/element_collection.rb', line 32 def values @collection.values end |