class Properties

Inspired by steve-yegge.blogspot.com/2008/10/universal-design-pattern.html :undefined is equivalent to undefined in JavaScript. See tests for details of behavior, which is similar to JavaScript’s.

Constants

BASE

Attributes

mapping[RW]
prototype[RW]

Public Class Methods

new(prototype=BASE, mapping=Hash.new) click to toggle source
# File lib/properties.rb, line 42
def initialize(prototype=BASE, mapping=Hash.new)
  @prototype = prototype
  @mapping = mapping
end

Public Instance Methods

get(key) click to toggle source
# File lib/properties.rb, line 17
def get(key)
  if chain_has? key
    prototype.get key
  elsif has? key
    @mapping[key]
  else
    :undefined
  end
end
has?(key) click to toggle source
# File lib/properties.rb, line 31
def has?(key)
  @mapping.has_key?(key) ||
     chain_has?(key)
end
inherit(new_properties) click to toggle source
# File lib/properties.rb, line 13
def inherit(new_properties)
  Properties.new self, @mapping.merge(new_properties)
end
put(key, value) click to toggle source
# File lib/properties.rb, line 27
def put(key, value)
  @mapping[key] = value
end
remove(key) click to toggle source
# File lib/properties.rb, line 36
def remove(key)
  @mapping.reject! { |k, _| k == key }
end

Private Instance Methods

chain_has?(key) click to toggle source
# File lib/properties.rb, line 47
def chain_has?(key)
  prototype != self && prototype.has?(key)
end