Sha256: a81146e15328191170d8b7e458d56a7b345c6d598ac21542313f49739f417ec0

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

require 'forwardable'
require 'set'

module InheritableAccessors
  class InheritableHash
    attr_accessor :__local_values__
    attr_reader   :__parent__
    attr_reader   :__deleted_keys__
    extend Forwardable

    WRITE_METHODS = %w{
      []= initialize merge! store
    }

    DELETE_METHODS = %w{
      delete
    }

    READ_METHODS = %w{
      == [] each each_pair each_value empty? eql? fetch flatten
      has_key? has_value? hash include? index inspect invert key key? keys
      length member? merge pretty_print rehash select size to_a to_h to_s
      value? values }

    def_delegators :@__local_values__, *WRITE_METHODS
    delegate READ_METHODS => :to_hash

    def initialize(prototype=nil)
      @__local_values__ = Hash.new
      @__parent__ = prototype
      @__deleted_keys__ = prototype ? prototype.__deleted_keys__.dup : Set.new
    end

    def delete(key)
      @__deleted_keys__.merge [key]
      __local_values__.delete(key)
    end

    def to_hash
      if !!__parent__
        hash = __parent__.to_hash
        hash.delete_if do |key, _|
          __deleted_keys__.include?(key)
        end
        hash.merge(__local_values__)
      else
        __local_values__.clone
      end
    end

    def inherit_copy
      InheritableHash.new(self)
    end

    alias :initialize_copy :inherit_copy
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
inheritable_accessors-0.1.2 lib/inheritable_accessors/inheritable_hash.rb