Class: InheritableAccessors::InheritableHash

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/inheritable_accessors/inheritable_hash.rb

Constant Summary collapse

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 }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prototype = nil) ⇒ InheritableHash

Returns a new instance of InheritableHash.



28
29
30
31
32
# File 'lib/inheritable_accessors/inheritable_hash.rb', line 28

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

Instance Attribute Details

#__deleted_keys__Object (readonly)

Returns the value of attribute deleted_keys.



8
9
10
# File 'lib/inheritable_accessors/inheritable_hash.rb', line 8

def __deleted_keys__
  @__deleted_keys__
end

#__local_values__Object

Returns the value of attribute local_values.



6
7
8
# File 'lib/inheritable_accessors/inheritable_hash.rb', line 6

def __local_values__
  @__local_values__
end

#__parent__Object (readonly)

Returns the value of attribute __parent__.



7
8
9
# File 'lib/inheritable_accessors/inheritable_hash.rb', line 7

def __parent__
  @__parent__
end

Instance Method Details

#delete(key) ⇒ Object



34
35
36
37
# File 'lib/inheritable_accessors/inheritable_hash.rb', line 34

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

#inherit_copyObject Also known as: initialize_copy



51
52
53
# File 'lib/inheritable_accessors/inheritable_hash.rb', line 51

def inherit_copy
  InheritableHash.new(self)
end

#to_hashObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/inheritable_accessors/inheritable_hash.rb', line 39

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