Sha256: a2db79b150ebcc78033d3746dc7f4833f93120c09c289e84eb407f52a31f9934

Contents?: true

Size: 797 Bytes

Versions: 7

Compression:

Stored size: 797 Bytes

Contents

# frozen_string_literal: true

require 'active_support/core_ext/hash/keys'

require_relative './entry'

class ActiveSet
  class Instructions
    attr_reader :hash

    def initialize(hash)
      @hash = hash
      @flattened_hash = hash.flatten_keys.transform_keys { |k| k.map(&:to_s) }
    end

    def process_adapter(set:, adapter:)
      @flattened_hash.reduce(set) do |inner_set, (keypath, value)|
        instruction = Entry.new(keypath, value)
        output = adapter.new(inner_set, instruction).process
        remove_from_instruction_set(key: instruction.path) if output[:processed]

        output[:set]
      end
    end

    def get(keypath)
      @hash.dig(*keypath)
    end

    private

    def remove_from_instruction_set(key:)
      @flattened_hash.delete(key)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
activeset-0.5.3 lib/active_set/instructions/base.rb
activeset-0.5.2 lib/active_set/instructions/base.rb
activeset-0.5.1 lib/active_set/instructions/base.rb
activeset-0.5.0 lib/active_set/instructions/base.rb
activeset-0.4.4 lib/active_set/instructions/base.rb
activeset-0.4.3 lib/active_set/instructions/base.rb
activeset-0.4.2 lib/active_set/instructions/base.rb