Class: Properties

Inherits:
Object
  • Object
show all
Defined in:
lib/properties.rb

Overview

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.

Constant Summary collapse

BASE =
Properties.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#mappingObject

Returns the value of attribute mapping.



7
8
9
# File 'lib/properties.rb', line 7

def mapping
  @mapping
end

#prototypeObject

Returns the value of attribute prototype.



7
8
9
# File 'lib/properties.rb', line 7

def prototype
  @prototype
end

Instance Method Details

#get(key) ⇒ Object



17
18
19
20
21
22
23
24
25
# 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) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/properties.rb', line 31

def has?(key)
  @mapping.has_key?(key) ||
     chain_has?(key)
end

#inherit(new_properties) ⇒ Object



13
14
15
# File 'lib/properties.rb', line 13

def inherit(new_properties)
  Properties.new self, @mapping.merge(new_properties)
end

#put(key, value) ⇒ Object



27
28
29
# File 'lib/properties.rb', line 27

def put(key, value)
  @mapping[key] = value
end

#remove(key) ⇒ Object



36
37
38
# File 'lib/properties.rb', line 36

def remove(key)
  @mapping.reject! { |k, _| k == key }
end