module Shrine::Plugins::Versions::Utils

  1. lib/shrine/plugins/versions.rb

Methods

Public Instance

  1. deep_map
  2. each_file
  3. map_file

Public Instance methods

deep_map(object, path = [], transform_keys:, &block)
[show source]
    # File lib/shrine/plugins/versions.rb
143 def deep_map(object, path = [], transform_keys:, &block)
144   if object.is_a?(Hash)
145     result = yield path, object
146 
147     return result if result
148 
149     object.inject({}) do |hash, (key, value)|
150       key    = key.send(transform_keys)
151       result = yield [*path, key], value
152 
153       hash.merge! key => (result || deep_map(value, [*path, key], transform_keys: transform_keys, &block))
154     end
155   elsif object.is_a?(Array)
156     result = yield path, object
157 
158     return result if result
159 
160     object.map.with_index do |value, idx|
161       result = yield [*path, idx], value
162 
163       result || deep_map(value, [*path, idx], transform_keys: transform_keys, &block)
164     end
165   else
166     result = yield path, object
167     result or fail Shrine::Error, "leaf reached"
168   end
169 end
each_file(object)
[show source]
    # File lib/shrine/plugins/versions.rb
122 def each_file(object)
123   return enum_for(__method__, object) unless block_given?
124 
125   map_file(object) do |path, file|
126     yield path, file
127     file
128   end
129 end
map_file(object, transform_keys: :to_sym)
[show source]
    # File lib/shrine/plugins/versions.rb
131 def map_file(object, transform_keys: :to_sym)
132   if object.is_a?(Hash) || object.is_a?(Array)
133     deep_map(object, transform_keys: transform_keys) do |path, value|
134       yield path, value unless value.is_a?(Hash) || value.is_a?(Array)
135     end
136   elsif object
137     yield nil, object
138   else
139     object
140   end
141 end