Class: Golem::Util::ElementCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/golem/util/element_collection.rb

Instance Method Summary collapse

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

Raises:

  • (ArgumentError)


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

#eachObject



24
25
26
# File 'lib/golem/util/element_collection.rb', line 24

def each
  @collection.values.each{|v| yield v}
end

#empty?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/golem/util/element_collection.rb', line 28

def empty?
  @collection.empty?
end

#valuesObject



32
33
34
# File 'lib/golem/util/element_collection.rb', line 32

def values
  @collection.values
end