Sha256: 931dfb91c7b07c3adcfcd4463772dacd673e3de83c5dd009533c64a58458ac99

Contents?: true

Size: 1.01 KB

Versions: 7

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

class Lenjador
  module Preprocessors
    module Strategies
      class Prune
        def initialize(trie)
          @trie = trie
        end

        def process(data, pointer = '')
          return nil unless @trie.include?(pointer)

          case data
          when Hash
            process_hash(data, pointer)

          when Array
            process_array(data, pointer)

          else
            data
          end
        end

        private

        def process_hash(data, parent_pointer)
          data.each_with_object({}) do |(key, value), result|
            path = "#{parent_pointer}/#{key}"

            result[key] = process(value, path) if @trie.include?(path)
          end
        end

        def process_array(data, parent_pointer)
          data.each_with_index.each_with_object([]) do |(value, index), result|
            path = "#{parent_pointer}/#{index}"

            result << process(value, path) if @trie.include?(path)
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
lenjador-2.2.1 lib/lenjador/preprocessors/strategies/prune.rb
lenjador-2.2.0 lib/lenjador/preprocessors/strategies/prune.rb
lenjador-2.1.0 lib/lenjador/preprocessors/strategies/prune.rb
lenjador-2.0.1 lib/lenjador/preprocessors/strategies/prune.rb
lenjador-2.0.0 lib/lenjador/preprocessors/strategies/prune.rb
lenjador-1.4.0 lib/lenjador/preprocessors/strategies/prune.rb
lenjador-1.3.0 lib/lenjador/preprocessors/strategies/prune.rb