Class: OptionsHash
- Inherits:
-
Hash
- Object
- Hash
- OptionsHash
- Defined in:
- lib/options_hash.rb
Overview
Taken brazenly from ActiveSupport
Class Method Summary collapse
Instance Method Summary collapse
-
#[]=(key, value) ⇒ Object
Assigns a new value to the hash:.
-
#assert_valid_keys(*valid_keys) ⇒ Object
Validate all keys in a hash match *valid keys, raising ArgumentError on a mismatch.
- #default(key = nil) ⇒ Object
-
#delete(key) ⇒ Object
Removes a specified key from the hash.
-
#dup ⇒ Object
Returns an exact copy of the hash.
- #extractable_options? ⇒ Boolean
-
#fetch(key, *extras) ⇒ Object
Fetches the value for the specified key, same as doing hash.
-
#initialize(constructor = {}) ⇒ OptionsHash
constructor
A new instance of OptionsHash.
-
#key?(key) ⇒ Boolean
(also: #include?, #has_key?, #member?)
Checks the hash for a key matching the argument passed in:.
-
#merge(hash) ⇒ Object
Merges the instantized and the specified hashes together, giving precedence to the values from the second hash Does not overwrite the existing hash.
- #regular_update ⇒ Object
- #regular_writer ⇒ Object
-
#reverse_merge(other_hash) ⇒ Object
Performs the opposite of merge, with the keys and values from the first hash taking precedence over the second.
- #reverse_merge!(other_hash) ⇒ Object
-
#stringify_keys ⇒ Object
Return a new hash with all keys converted to strings.
-
#stringify_keys! ⇒ Object
Destructively convert all keys to strings.
-
#symbolize_keys ⇒ Object
Return a new hash with all keys converted to symbols, as long as they respond to
to_sym. -
#symbolize_keys! ⇒ Object
(also: #to_options!)
Destructively convert all keys to symbols, as long as they respond to
to_sym. -
#to_hash ⇒ Object
Convert to a Hash with String keys.
-
#to_options ⇒ Object
Return a new hash with all keys converted to symbols, as long as they respond to
to_sym. -
#update(other_hash) ⇒ Object
(also: #merge!)
Updates the instantized hash with values from the second:.
-
#values_at(*indices) ⇒ Object
Returns an array of the values at the specified indices:.
Constructor Details
#initialize(constructor = {}) ⇒ OptionsHash
Returns a new instance of OptionsHash.
51 52 53 54 55 56 57 58 |
# File 'lib/options_hash.rb', line 51 def initialize(constructor = {}) if constructor.is_a?(Hash) super() update(constructor) else super(constructor) end end |
Class Method Details
.new_from_hash_copying_default(hash) ⇒ Object
68 69 70 71 72 |
# File 'lib/options_hash.rb', line 68 def self.(hash) OptionsHash.new(hash).tap do |new_hash| new_hash.default = hash.default end end |
Instance Method Details
#[]=(key, value) ⇒ Object
Assigns a new value to the hash:
hash = OptionsHash.new
hash[:key] = "value"
82 83 84 |
# File 'lib/options_hash.rb', line 82 def []=(key, value) regular_writer(convert_key(key), convert_value(value)) end |
#assert_valid_keys(*valid_keys) ⇒ Object
Validate all keys in a hash match *valid keys, raising ArgumentError on a mismatch. Note that keys are NOT treated indifferently, meaning if you use strings for keys but assert symbols as keys, this will fail.
Examples
{ :name => "Rob", :years => "28" }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key(s): years"
{ :name => "Rob", :age => "28" }.assert_valid_keys("name", "age") # => raises "ArgumentError: Unknown key(s): name, age"
{ :name => "Rob", :age => "28" }.assert_valid_keys(:name, :age) # => passes, raises nothing
42 43 44 45 |
# File 'lib/options_hash.rb', line 42 def assert_valid_keys(*valid_keys) unknown_keys = keys - [valid_keys].flatten raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty? end |
#default(key = nil) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/options_hash.rb', line 60 def default(key = nil) if key.is_a?(Symbol) && include?(key = key.to_s) self[key] else super end end |
#delete(key) ⇒ Object
Removes a specified key from the hash.
156 157 158 |
# File 'lib/options_hash.rb', line 156 def delete(key) super(convert_key(key)) end |
#dup ⇒ Object
Returns an exact copy of the hash.
135 136 137 |
# File 'lib/options_hash.rb', line 135 def dup OptionsHash.new(self) end |
#extractable_options? ⇒ Boolean
47 48 49 |
# File 'lib/options_hash.rb', line 47 def true end |
#fetch(key, *extras) ⇒ Object
Fetches the value for the specified key, same as doing hash
119 120 121 |
# File 'lib/options_hash.rb', line 119 def fetch(key, *extras) super(convert_key(key), *extras) end |
#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?
Checks the hash for a key matching the argument passed in:
hash = OptionsHash.new
hash["key"] = "value"
hash.key? :key # => true
hash.key? "key" # => true
110 111 112 |
# File 'lib/options_hash.rb', line 110 def key?(key) super(convert_key(key)) end |
#merge(hash) ⇒ Object
Merges the instantized and the specified hashes together, giving precedence to the values from the second hash Does not overwrite the existing hash.
141 142 143 |
# File 'lib/options_hash.rb', line 141 def merge(hash) self.dup.update(hash) end |
#regular_update ⇒ Object
75 |
# File 'lib/options_hash.rb', line 75 alias_method :regular_update, :update |
#regular_writer ⇒ Object
74 |
# File 'lib/options_hash.rb', line 74 alias_method :regular_writer, :[]= |
#reverse_merge(other_hash) ⇒ Object
Performs the opposite of merge, with the keys and values from the first hash taking precedence over the second. This overloaded definition prevents returning a regular hash, if reverse_merge is called on a OptionsHash.
147 148 149 |
# File 'lib/options_hash.rb', line 147 def reverse_merge(other_hash) super self.class.(other_hash) end |
#reverse_merge!(other_hash) ⇒ Object
151 152 153 |
# File 'lib/options_hash.rb', line 151 def reverse_merge!(other_hash) replace(reverse_merge( other_hash )) end |
#stringify_keys ⇒ Object
Return a new hash with all keys converted to strings.
4 5 6 |
# File 'lib/options_hash.rb', line 4 def stringify_keys dup.stringify_keys! end |
#stringify_keys! ⇒ Object
Destructively convert all keys to strings.
9 10 11 12 13 14 |
# File 'lib/options_hash.rb', line 9 def stringify_keys! keys.each do |key| self[key.to_s] = delete(key) end self end |
#symbolize_keys ⇒ Object
Return a new hash with all keys converted to symbols, as long as they respond to to_sym.
18 19 20 |
# File 'lib/options_hash.rb', line 18 def symbolize_keys dup.symbolize_keys! end |
#symbolize_keys! ⇒ Object Also known as: to_options!
Destructively convert all keys to symbols, as long as they respond to to_sym.
24 25 26 27 28 29 |
# File 'lib/options_hash.rb', line 24 def symbolize_keys! keys.each do |key| self[(key.to_sym rescue key) || key] = delete(key) end self end |
#to_hash ⇒ Object
Convert to a Hash with String keys.
167 168 169 |
# File 'lib/options_hash.rb', line 167 def to_hash Hash.new(default).merge!(self) end |
#to_options ⇒ Object
Return a new hash with all keys converted to symbols, as long as they respond to to_sym.
31 32 33 |
# File 'lib/options_hash.rb', line 31 def symbolize_keys dup.symbolize_keys! end |
#update(other_hash) ⇒ Object Also known as: merge!
Updates the instantized hash with values from the second:
hash_1 = OptionsHash.new
hash_1[:key] = "value"
hash_2 = OptionsHash.new
hash_2[:key] = "New Value!"
hash_1.update(hash_2) # => {"key"=>"New Value!"}
96 97 98 99 |
# File 'lib/options_hash.rb', line 96 def update(other_hash) other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) } self end |
#values_at(*indices) ⇒ Object
Returns an array of the values at the specified indices:
hash = OptionsHash.new
hash[:a] = "x"
hash[:b] = "y"
hash.values_at("a", "b") # => ["x", "y"]
130 131 132 |
# File 'lib/options_hash.rb', line 130 def values_at(*indices) indices.collect {|key| self[convert_key(key)]} end |