class Zanzou::ArrayShadow

Public Class Methods

merge(orig_ary, modifications) click to toggle source
# File lib/zanzou.rb, line 128
def self.merge(orig_ary, modifications)
  ret = orig_ary.dup
  modifications.each{|k, v| ret[k] = v}
  ret
end
new(*args) click to toggle source
Calls superclass method Zanzou::ShadowNode::new
# File lib/zanzou.rb, line 134
def initialize(*args)
  super
  @children = {}
end

Public Instance Methods

method_missing(name, *args) click to toggle source
# File lib/zanzou.rb, line 139
def method_missing(name, *args)
  case name
  when :[]=
    handle_setter(args[0], args[1])
  when :[]
    handle_getter(args[0])
  else
    handle_destructive_method_call(name, args)
  end
end

Private Instance Methods

handle_destructive_method_call(name, args) click to toggle source
# File lib/zanzou.rb, line 172
def handle_destructive_method_call(name, args)
  modified!
  @new_obj = @orig_obj.dup

  # Apply modification to children now because the index may change by the public_send
  @children.each do |idx, child_shadow|
    @new_obj[idx] = ShadowNode.finalize(child_shadow)
  end
  # Forget about the children we've finalized
  @modifications.clear

  return @new_obj.public_send(name, *args)
end
handle_getter(idx) click to toggle source
# File lib/zanzou.rb, line 158
def handle_getter(idx)
  if @new_obj
    return @new_obj[idx]
  else
    if @children.key?(idx)
      return @children[key]
    else
      child_shadow = ShadowNode.create(@orig_obj[idx], parent: self, parent_key: idx)
      @children[idx] = child_shadow
      return child_shadow
    end
  end
end
handle_setter(key, value) click to toggle source
# File lib/zanzou.rb, line 152
def handle_setter(key, value)
  modified!
  @modifications[key] = value
  return value
end