Class: JsonRpcObjects::Utils::Hash
- Inherits:
-
Object
- Object
- JsonRpcObjects::Utils::Hash
- Defined in:
- lib/json-rpc-objects/utils/hash.rb
Overview
Hash utility functions.
Class Method Summary collapse
-
.create(default = nil, hash = nil, &block) ⇒ Hash
Creates hash by setting default settings in one call.
-
.define(values = { }, default = nil, &block) ⇒ Hash
Defines hash by setting the default value or an
Proc
and content. -
.keys_to_sym(hash) ⇒ Hash
Converts all
String
keys to symbols. -
.keys_to_sym!(hash) ⇒ Hash
Emulates #keys_to_sym on place.
-
.map_keys(hash, &block) ⇒ Hash
Returns a new hash with the results of running block once for every key in
hash
. -
.map_keys!(hash, &block) ⇒ Hash
Emulates #map_keys on place.
-
.map_pairs(hash, &block) ⇒ Hash
Returns a new hash with the results of running block once for every pair in
hash
. -
.recreate(hash) ⇒ Hash
Recreates the hash, so creates empty one and assigns the same default values.
-
.remove!(hash, &block) ⇒ Hash
Moves selected pairs outside the hash, so returns them.
Class Method Details
.create(default = nil, hash = nil, &block) ⇒ Hash
Creates hash by setting default settings in one call.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/json-rpc-objects/utils/hash.rb', line 94 def self.create(default = nil, hash = nil, &block) if Ruby::Version >= "1.9" if not hash.nil? hash = hash.dup else hash = { } end hash.default = default hash.default_proc = block if not block.nil? return hash else if not hash.nil? hash = hash.dup else hash = { } end if not block.nil? _new = ::Hash::new(&block) _new.update(hash) if not hash.empty? hash = _new else hash.default = default end return hash end end |
.define(values = { }, default = nil, &block) ⇒ Hash
Defines hash by setting the default value or an Proc
and content.
190 191 192 193 |
# File 'lib/json-rpc-objects/utils/hash.rb', line 190 def self.define(values = { }, default = nil, &block) hash = ::Hash[values] self.create(default, hash, &block) end |
.keys_to_sym(hash) ⇒ Hash
Converts all String
keys to symbols.
131 132 133 134 135 136 137 138 139 |
# File 'lib/json-rpc-objects/utils/hash.rb', line 131 def self.keys_to_sym(hash) self.map_keys(hash) do |k| if k.kind_of? ::String k.to_sym else k end end end |
.keys_to_sym!(hash) ⇒ Hash
Emulates #keys_to_sym on place. In fact, replaces old hash by new one.
149 150 151 |
# File 'lib/json-rpc-objects/utils/hash.rb', line 149 def self.keys_to_sym!(hash) hash.replace(self.keys_to_sym(hash)) end |
.map_keys(hash, &block) ⇒ Hash
Returns a new hash with the results of running block once for every key in hash
.
34 35 36 37 38 |
# File 'lib/json-rpc-objects/utils/hash.rb', line 34 def self.map_keys(hash, &block) self.map_pairs(hash) do |k, v| [block.call(k), v] end end |
.map_keys!(hash, &block) ⇒ Hash
Emulates #map_keys on place. In fact, replaces old hash by new one.
49 50 51 |
# File 'lib/json-rpc-objects/utils/hash.rb', line 49 def self.map_keys!(hash, &block) hash.replace(self.map_keys(hash, &block)) end |
.map_pairs(hash, &block) ⇒ Hash
Returns a new hash with the results of running block once for every pair in hash
.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/json-rpc-objects/utils/hash.rb', line 62 def self.map_pairs(hash, &block) _new = self.recreate(hash) hash.each_pair do |k, v| new_k, new_v = block.call(k, v) _new[new_k] = new_v end return _new end |
.recreate(hash) ⇒ Hash
Recreates the hash, so creates empty one and assigns the same default values.
81 82 83 |
# File 'lib/json-rpc-objects/utils/hash.rb', line 81 def self.recreate(hash) self.create(hash.default, &hash.default_proc) end |
.remove!(hash, &block) ⇒ Hash
Moves selected pairs outside the hash, so returns them. Output hash has the same default settings.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/json-rpc-objects/utils/hash.rb', line 162 def self.remove!(hash, &block) result = self.recreate(hash) delete = [ ] hash.each_pair do |k, v| if block.call(k, v) result[k] = v delete << k end end delete.each do |k| hash.delete(k) end return result end |