Module: Rtml::InheritedInstanceVariables

Defined in:
lib/rtml/inherited_instance_variables.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/rtml/inherited_instance_variables.rb', line 25

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

Instance Method Details

#copy_ivars_from(source) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/rtml/inherited_instance_variables.rb', line 2

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

#initialize_with_inherited_instance_variables(*a, &b) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/rtml/inherited_instance_variables.rb', line 17

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