Module: Shrine::Plugins::Versions::Utils

Defined in:
lib/shrine/plugins/versions.rb

Class Method Summary collapse

Class Method Details

.deep_map(object, path = [], transform_keys:, &block) ⇒ Object


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/shrine/plugins/versions.rb', line 143

def deep_map(object, path = [], transform_keys:, &block)
  if object.is_a?(Hash)
    result = yield path, object

    return result if result

    object.inject({}) do |hash, (key, value)|
      key    = key.send(transform_keys)
      result = yield [*path, key], value

      hash.merge! key => (result || deep_map(value, [*path, key], transform_keys: transform_keys, &block))
    end
  elsif object.is_a?(Array)
    result = yield path, object

    return result if result

    object.map.with_index do |value, idx|
      result = yield [*path, idx], value

      result || deep_map(value, [*path, idx], transform_keys: transform_keys, &block)
    end
  else
    result = yield path, object
    result or fail Shrine::Error, "leaf reached"
  end
end

.each_file(object) ⇒ Object


122
123
124
125
126
127
128
129
# File 'lib/shrine/plugins/versions.rb', line 122

def each_file(object)
  return enum_for(__method__, object) unless block_given?

  map_file(object) do |path, file|
    yield path, file
    file
  end
end

.map_file(object, transform_keys: :to_sym) ⇒ Object


131
132
133
134
135
136
137
138
139
140
141
# File 'lib/shrine/plugins/versions.rb', line 131

def map_file(object, transform_keys: :to_sym)
  if object.is_a?(Hash) || object.is_a?(Array)
    deep_map(object, transform_keys: transform_keys) do |path, value|
      yield path, value unless value.is_a?(Hash) || value.is_a?(Array)
    end
  elsif object
    yield nil, object
  else
    object
  end
end