Sha256: e83f940ce8de3d80eae2cd8f42fd2273f377f6987f8a82f4a2a5ec7a39ec6a13

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

module Rtml::InheritedInstanceVariables
  def copy_ivars_from(source)
    ivars = source.instance_variables
    ivars -= source.protected_instance_variables if source.respond_to?(:protected_instance_variables)
    ivars -= source.rtml_protected_instance_variables if source.respond_to?(:rtml_protected_instance_variables)
    ivars.each do |ivar|
      # as time consuming as this may be, it's still faster than eval("defined?(#{ivar})")
      #  -- and easier to read, too.
      if !instance_variables.include?(ivar)
        instance_variable_set(ivar, source.instance_variable_get(ivar))
      end
    end
  rescue
    # fail silently if they can't be copied
  end

  def initialize_with_inherited_instance_variables(*a, &b)
    initialize_without_inherited_instance_variables(*a, &b)

    %w(controller document parent).each do |method|
      copy_ivars_from(send(method)) if respond_to?(method) rescue nil
    end
  end

  def self.included(base)
    base.instance_eval do
      class_inheritable_array :protected_instance_variables
      read_inheritable_attribute(:protected_instance_variables) || write_inheritable_attribute(:protected_instance_variables, [])
      alias_method_chain :initialize, :inherited_instance_variables
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rtml-2.0.4 lib/rtml/inherited_instance_variables.rb