Class: Hashery::PropertyHash
- Defined in:
- lib/hashery/property_hash.rb
Overview
A PropertyHash is the same as a regular Hash except it strictly limits the allowed keys.
There are two ways to use it.
1) As an object in itself.
h = PropertyHash.new(:a=>1, :b=>2)
h[:a] #=> 1
h[:a] = 3
h[:a] #=> 3
But if we try to set key that was not fixed, then we will get an error.
h[:x] = 5 #=> ArgumentError
2) As a superclass.
class MyPropertyHash < PropertyHash
property :a, :default => 1
property :b, :default => 2
end
h = MyPropertyHash.new
h[:a] #=> 1
h[:a] = 3
h[:a] #=> 3
Again, if we try to set key that was not fixed, then we will get an error.
h[:x] = 5 #=> ArgumentError
Constant Summary
Constants inherited from CRUDHash
Class Method Summary collapse
-
.properties ⇒ Hash
Get a list of properties with default values.
-
.property(key, opts = {}) ⇒ Object
Define a property.
Instance Method Summary collapse
-
#assert_key!(key) ⇒ Object
private
Asserta that a key is a defined property.
-
#initialize(properties = {}, &default_proc) ⇒ PropertyHash
constructor
Initialize new instance of PropertyHash.
-
#property(key, opts = {}) ⇒ Object
Create a new property, on-the-fly.
-
#store(key, value) ⇒ Object
Store key value pair, ensuring the key is a valid property first.
-
#store! ⇒ Object
private
Alias original #store method and make private.
Methods inherited from CRUDHash
#<<, #[], [], #[]=, auto, #cast, #cast_key, #default_proc, #delete, #each, #fetch, #key?, #key_proc, #key_proc=, #merge, #read, #replace, #retrieve, #to_hash, #update, #values_at
Methods inherited from Hash
create, #rekey, #rekey!, #retrieve, #to_hash, #to_stash
Constructor Details
#initialize(properties = {}, &default_proc) ⇒ PropertyHash
Initialize new instance of PropertyHash.
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/hashery/property_hash.rb', line 76 def initialize(properties={}, &default_proc) if properties.respond_to?(:each_pair) super(&default_proc) fixed = self.class.properties.merge(properties) fixed.each_pair do |key, value| store!(key, value) end else super(*[properties].compact, &default_proc) end end |
Class Method Details
.properties ⇒ Hash
Get a list of properties with default values.
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/hashery/property_hash.rb', line 44 def self.properties @properties ||= ( parent = ancestors[1] if parent.respond_to?(:properties) parent.properties else {} end ) end |
.property(key, opts = {}) ⇒ Object
Define a property.
64 65 66 |
# File 'lib/hashery/property_hash.rb', line 64 def self.property(key, opts={}) properties[key] = opts[:default] end |
Instance Method Details
#assert_key!(key) ⇒ Object (private)
Asserta that a key is a defined property.
153 154 155 156 157 |
# File 'lib/hashery/property_hash.rb', line 153 def assert_key!(key) unless key?(key) raise ArgumentError, "property is not defined -- #{key.inspect}" end end |
#property(key, opts = {}) ⇒ Object
Create a new property, on-the-fly.
101 102 103 104 105 106 107 |
# File 'lib/hashery/property_hash.rb', line 101 def property(key, opts={}) if opts[:default] store!(key, opts[:default]) else store!(key, retrieve(key)) end end |
#store(key, value) ⇒ Object
Store key value pair, ensuring the key is a valid property first.
119 120 121 122 |
# File 'lib/hashery/property_hash.rb', line 119 def store(key, value) assert_key!(key) super(key, value) end |
#store! ⇒ Object (private)
Alias original #store method and make private.
89 |
# File 'lib/hashery/property_hash.rb', line 89 alias :store! :store |