Class: Woyo::Attributes::AttributesHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/woyo/world/attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAttributesHash

Returns a new instance of AttributesHash.



18
19
20
# File 'lib/woyo/world/attributes.rb', line 18

def initialize
  @listeners = {}
end

Instance Attribute Details

#listenersObject (readonly)

Returns the value of attribute listeners.



16
17
18
# File 'lib/woyo/world/attributes.rb', line 16

def listeners
  @listeners
end

Instance Method Details

#[]=(attr, value) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/woyo/world/attributes.rb', line 27

def []= attr, value
  old_value = self[attr]
  super
  if value != old_value
    @listeners[attr].each { |listener| listener.notify attr, value } if @listeners[attr]  # attribute listeners (groups, etc..)
    @listeners[:*].each   { |listener| listener.notify attr, value } if @listeners[:*]    # wildcard listeners (trackers)
  end
end

#add_listener(attr, listener) ⇒ Object



22
23
24
25
# File 'lib/woyo/world/attributes.rb', line 22

def add_listener attr, listener
  @listeners[attr] ||= []
  @listeners[attr] << listener
end

#setObject



14
# File 'lib/woyo/world/attributes.rb', line 14

alias_method :set, :[]=