Class: ProperProperties::Properties
- Inherits:
-
Hash
- Object
- Hash
- ProperProperties::Properties
- Defined in:
- lib/proper_properties/properties.rb
Overview
Simple representation of a properties file content by an extension of a Hash object a la ActiveSupport::HashWithIndifferentAccess, with underlying symbol keys
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#[]=(key, value) ⇒ Object
Assigns a new value to the hash:.
-
#delete(key) ⇒ Object
Removes the specified key from the hash.
-
#fetch(key, *extras) ⇒ Object
Same as
Hash#fetch
where the key passed as argument can be either a string or a symbol:. -
#key?(key) ⇒ Boolean
Checks the hash for a key matching the argument passed in:.
-
#values_at(*indices) ⇒ Object
Returns an array of the values at the specified indices:.
Instance Method Details
#[](key) ⇒ Object
55 56 57 |
# File 'lib/proper_properties/properties.rb', line 55 def [](key) self.fetch(key) end |
#[]=(key, value) ⇒ Object
Assigns a new value to the hash:
hash = ProperProperties::Properties.new
hash[:key] = 'value'
This value can be later fetched using either :key
or ‘key’.
12 13 14 |
# File 'lib/proper_properties/properties.rb', line 12 def []=(key, value) super(convert_key(key), value) end |
#delete(key) ⇒ Object
Removes the specified key from the hash.
51 52 53 |
# File 'lib/proper_properties/properties.rb', line 51 def delete(key) super(convert_key(key)) end |
#fetch(key, *extras) ⇒ Object
Same as Hash#fetch
where the key passed as argument can be either a string or a symbol:
counters = ProperProperties::Properties.new
counters[:foo] = 1
counters.fetch('foo') # => 1
counters.fetch(:bar, 0) # => 0
counters.fetch(:bar) {|key| 0} # => 0
counters.fetch(:zoo) # => KeyError: key not found: "zoo"
36 37 38 |
# File 'lib/proper_properties/properties.rb', line 36 def fetch(key, *extras) super(convert_key(key), *extras) end |
#key?(key) ⇒ Boolean
Checks the hash for a key matching the argument passed in:
hash = ProperProperties::Properties.new
hash['key'] = 'value'
hash.key?(:key) # => true
hash.key?('key') # => true
22 23 24 |
# File 'lib/proper_properties/properties.rb', line 22 def key?(key) super(convert_key(key)) end |
#values_at(*indices) ⇒ Object
Returns an array of the values at the specified indices:
hash = ProperProperties::Properties.new
hash[:a] = 'x'
hash[:b] = 'y'
hash.values_at('a', 'b') # => ["x", "y"]
46 47 48 |
# File 'lib/proper_properties/properties.rb', line 46 def values_at(*indices) indices.collect {|key| self[convert_key(key)]} end |