Class: Class

Inherits:
Object show all
Defined in:
lib/pancake/core_ext/class.rb

Instance Method Summary collapse

Instance Method Details

#deep_copy_class_inheritable_accessor(*ivars) ⇒ Object

self.deep_inheritable_reader



40
41
42
43
# File 'lib/pancake/core_ext/class.rb', line 40

def deep_copy_class_inheritable_accessor(*ivars)
  deep_copy_class_inheritable_reader(*ivars)
  extlib_inheritable_writer(*ivars)
end

#deep_copy_class_inheritable_reader(*ivars) ⇒ Array[#to_s]

TODO:

Do we want to block instance_reader via :instance_reader => false

TODO:

It would be preferable that we do something with a Hash passed in (error out or do the same as other methods above) instead of silently moving on). In particular, this makes the return value of this function less useful.

Taken from extlib but uses a full Marshall.dump rather than just a dup. This may not work for some data types. The data must be marshalable to use this method.

Defines class-level inheritable attribute reader. Attributes are available to subclasses, each subclass has a copy of parent’s attribute.

Parameters:

  • *syms (Array[#to_s])

    Array of attributes to define inheritable reader for.

Returns:

  • (Array[#to_s])

    Array of attributes converted into inheritable_readers.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pancake/core_ext/class.rb', line 18

def deep_copy_class_inheritable_reader(*ivars)
  instance_reader = ivars.pop[:reader] if ivars.last.is_a?(Hash)

  ivars.each do |ivar|
    self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
      def self.#{ivar}
        return @#{ivar} if self == #{self} || defined?(@#{ivar})
        ivar = superclass.#{ivar}
        return nil if ivar.nil? && !#{self}.instance_variable_defined?("@#{ivar}")
        @#{ivar} = ivar && !ivar.is_a?(Module) && !ivar.is_a?(Numeric) ? Marshal.load(Marshal.dump(ivar)) : ivar
      end
    RUBY
    unless instance_reader == false
      self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
        def #{ivar}
          self.class.#{ivar}
        end
      RUBY
    end # unless
  end # ivars.each
end