Sha256: a5b74d665a8cd6a2982e3a15092017e9f18fac052f356e673688c7b4a40f47e1

Contents?: true

Size: 948 Bytes

Versions: 2

Compression:

Stored size: 948 Bytes

Contents

# frozen_string_literal: true

require_relative '../patches/core_ext/hash/flatten_keys'
require_relative './instruction'

class ActiveSet
  class Instructions
    include Enumerable

    def initialize(hash)
      @instructions = hash.flatten_keys.map do |keypath, value|
        Instruction.new(keypath, value)
      end
    end

    def each(&block)
      @instructions.each(&block)
    end

    # :nocov:
    def method_missing(method_name, *args, &block)
      @instructions.send(method_name, *args, &block) || super
    end
    # :nocov:

    # :nocov:
    def respond_to_missing?(method_name, include_private = false)
      @instructions.respond_to?(method_name) || super
    end
    # :nocov:

    def associations_hash
      @instructions.reduce({}) { |h, i| h.merge(i.associations_hash) }
    end

    def get(key)
      @instructions.find do |instruction|
        instruction.attribute.to_s == key.to_s
      end&.value
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activeset-0.6.5 lib/active_set/instructions.rb
activeset-0.6.4 lib/active_set/instructions.rb