Module: Sparsify::UtilityMethods
- Included in:
- Sparsify
- Defined in:
- lib/sparsify/utility_methods.rb
Overview
The Utility Methods provide a significant (~4x) performance increase over extend-ing instance methods everywhere we need them.
Instance Method Summary collapse
-
#expand(sparse_hsh, sparse_key, options = {}, &block) ⇒ Object
Given a sparse hash, unsparsify a subset by address, returning a *modified copy* of the original sparse hash.
-
#expand!(sparse_hsh, *args) ⇒ Object
Given a sparse hash, unsparsify a subset by address *in place* (@see Sparsify::UtilityMethods#expand).
-
#sparse(hsh, options = {}) ⇒ Hash<String,Object>
Returns a sparse version of the given hash.
-
#sparse_each(hsh, options = {}, &block) ⇒ Object
Provides a way to iterate through a deeply-nested hash as if it were a sparse-hash.
-
#sparse_fetch(hsh, sparse_key, *args, &block) ⇒ Object
Fetch a sparse key from the given deeply-nested hash.
-
#sparse_get(hsh, sparse_key, options = {}) ⇒ Object
Get a sparse key from the given deeply-nested hash, or return nil if key not found.
-
#unsparse(hsh, options = {}) ⇒ Hash<String,Object>
Returns a deeply-nested version of the given sparse hash.
Instance Method Details
#expand(sparse_hsh, sparse_key, options = {}, &block) ⇒ Object
Given a sparse hash, unsparsify a subset by address, returning a *modified copy* of the original sparse hash.
~~~ ruby sparse = => 2, ‘a.c.d’ => 4, ‘a.c.e’ => 3, ‘b.f’ => 4 Sparsify::expand(sparse, ‘a.c’) # => => 2, ‘a.c’ => {‘d’ => 4, ‘e’ => 3, ‘b.f’ => 4} ~~~
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/sparsify/utility_methods.rb', line 146 def (sparse_hsh, sparse_key, *args) # if sparse_hsh includes our key, its value is already expanded. return sparse_hsh if sparse_hsh.include?(sparse_key) = (args.last.kind_of?(Hash) ? args.pop : {}) separator = .fetch(:separator, DEFAULT_SEPARATOR) pattern = /\A#{Regexp.escape(sparse_key)}#{Regexp.escape(separator)}/i match = {} unmatch = {} sparse_hsh.each do |k, v| if pattern =~ k sk = k.gsub(pattern, '') match[sk] = v else unmatch[k] = v end end unmatch.update(sparse_key => unsparse(match, )) unless match.empty? unmatch end |
#expand!(sparse_hsh, *args) ⇒ Object
Given a sparse hash, unsparsify a subset by address *in place* (@see Sparsify::UtilityMethods#expand)
171 172 173 |
# File 'lib/sparsify/utility_methods.rb', line 171 def (sparse_hsh, *args) sparse_hsh.replace (sparse_hsh, *args) end |
#sparse(hsh, options = {}) ⇒ Hash<String,Object>
Returns a sparse version of the given hash
47 48 49 50 51 52 |
# File 'lib/sparsify/utility_methods.rb', line 47 def sparse(hsh, = {}) enum = sparse_each(hsh, ) enum.each_with_object(Hash.new) do |(key, value), memo| memo[key] = value end end |
#sparse_each(hsh, options = {}) {|| ... } ⇒ void #sparse_each(hsh, options = {}) ⇒ Enumerator<(sparse_key,value)>
Provides a way to iterate through a deeply-nested hash as if it were a sparse-hash. Used internally for generating and deconstructing sparse hashes.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/sparsify/utility_methods.rb', line 22 def sparse_each(hsh, = {}, &block) return enum_for(:sparse_each, hsh, ) unless block_given? inherited_prefix = .fetch(:prefix, nil) separator = .fetch(:separator, DEFAULT_SEPARATOR) sparse_array = .fetch(:sparse_array, false) hsh.each do |partial_key, value| key = escaped_join(inherited_prefix, partial_key.to_s, separator) if value.kind_of?(Hash) && !value.empty? sparse_each(value, .merge(prefix: key), &block) elsif sparse_array && value.kind_of?(Array) && !value.empty? zps = (sparse_array == :zero_pad ? "%0#{value.count.to_s.size}d" : '%d')# zero-pad string sparse_each(value.count.times.map(&zps.method(:%)).zip(value), .merge(prefix: key), &block) else yield key, value end end end |
#sparse_fetch(hsh, sparse_key, default, options = {}) ⇒ Object #sparse_fetch(hsh, sparse_key, options = {}, &block) ⇒ Object #sparse_fetch(hsh, sparse_key, options = {}) ⇒ Object
Fetch a sparse key from the given deeply-nested hash.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/sparsify/utility_methods.rb', line 98 def sparse_fetch(hsh, sparse_key, *args, &block) = ( args.last.kind_of?(Hash) ? args.pop : {}) default = args.pop separator = .fetch(:separator, DEFAULT_SEPARATOR) escaped_split(sparse_key, separator).reduce(hsh) do |memo, kp| if memo.kind_of?(Hash) and memo.has_key?(kp) memo.fetch(kp) elsif default return default elsif block_given? return yield else raise KeyError, sparse_key end end end |
#sparse_get(hsh, sparse_key, options = {}) ⇒ Object
Get a sparse key from the given deeply-nested hash, or return nil if key not found.
Worth noting is that Hash#default_proc is not used, as the intricacies of implementation would lead to all sorts of terrible surprises.
127 128 129 |
# File 'lib/sparsify/utility_methods.rb', line 127 def sparse_get(hsh, sparse_key, = {}) sparse_fetch(hsh, sparse_key, ) { nil } end |
#unsparse(hsh, options = {}) ⇒ Hash<String,Object>
Returns a deeply-nested version of the given sparse hash
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/sparsify/utility_methods.rb', line 58 def unsparse(hsh, = {}) separator = .fetch(:separator, DEFAULT_SEPARATOR) hsh.each_with_object({}) do |(k, v), memo| current = memo key = escaped_split(k, separator) up_next = partial = key.shift until key.size.zero? up_next = key.shift up_next = up_next.to_i if (up_next =~ /\A[0-9]+\Z/) current = (current[partial] ||= (up_next.kind_of?(Integer) ? [] : {})) case up_next when Integer then raise KeyError unless current.kind_of?(Array) else raise KeyError unless current.kind_of?(Hash) end partial = up_next end current[up_next] = v end end |