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