lib/configurable/delegate_hash.rb in configurable-0.3.0 vs lib/configurable/delegate_hash.rb in configurable-0.4.0

- old
+ new

@@ -56,12 +56,12 @@ @delegates = delegates @receiver = receiver end # Binds self to the specified receiver. Delegate values are removed from - # store and sent to their writer method on receiver. If the store has no - # value for a delegate key, the delegate default value will be used. + # store and sent to their writer on receiver. If the store has no value + # for a delegate key, the delegate default value will be used. def bind(receiver) raise ArgumentError, "receiver cannot be nil" if receiver == nil if bound? if @receiver == receiver @@ -87,38 +87,36 @@ unmap(store) @receiver = nil self end - # Retrieves the value corresponding to the key. When bound, delegates with - # readers pull values from the receiver; otherwise the value in store will - # be returned. When unbound, if the store has no value for a delegate, the - # delgate default value will be returned. + # Retrieves the value corresponding to the key. When bound, delegates pull + # values from the receiver using the delegate.reader method; otherwise the + # value in store will be returned. When unbound, if the store has no value + # for a delegate, the delgate default value will be returned. def [](key) return store[key] unless delegate = delegates[key] case - when bound? && delegate.reader + when bound? receiver.send(delegate.reader) when store.has_key?(key) store[key] else store[key] = delegate.default end end - # Stores a value for the key. When bound, delegates with writers send the - # value to the receiver; otherwise values are stored in store. + # Stores a value for the key. When bound, delegates set the value in the + # receiver using the delegate.writer method; otherwise values are stored in + # store. def []=(key, value) if bound? && delegate = delegates[key] - if delegate.writer - receiver.send(delegate.writer, value) - return - end + receiver.send(delegate.writer, value) + else + store[key] = value end - - store[key] = value end # Returns the union of delegate and store keys. def keys delegates.keys | store.keys @@ -179,30 +177,27 @@ protected # helper to map delegate values from source to the receiver def map(source) # :nodoc: delegates.each_pair do |key, delegate| - next unless writer = delegate.writer - # map the value; if no value is set in the source then use the # delegate default. if map_default is false, then simply skip... # this ensures each config is initialized to a value when bound # UNLESS map_default is set (indicating manual initialization) value = case when source.has_key?(key) then source.delete(key) when delegate[:map_default, true] then delegate.default else next end - receiver.send(writer, value) + receiver.send(delegate.writer, value) end end # helper to unmap delegates from the receiver to a target hash def unmap(target) # :nodoc: delegates.each_pair do |key, delegate| - next unless reader = delegate.reader - target[key] = receiver.send(reader) + target[key] = receiver.send(delegate.reader) end end end end \ No newline at end of file