Sha256: ec4d44656f676c9c7acc88e1cb3376721f59e2056c3ba9260aa76d91db978809

Contents?: true

Size: 1.18 KB

Versions: 27

Compression:

Stored size: 1.18 KB

Contents

# These class attributes behave something like the class
# inheritable accessors.  But instead of copying the hash over at
# the time the subclass is first defined,  the accessors simply
# delegate to their superclass unless they have been given a 
# specific value.  This stops the strange situation where values 
# set after class definition don't get applied to subclasses.
class Class
  def superclass_delegating_reader(*names)
    class_name_to_stop_searching_on = self.superclass.name.blank? ? "Object" : self.superclass.name
    names.each do |name|
      class_eval <<-EOS
      def self.#{name}
        if defined?(@#{name})
          @#{name}
        elsif superclass < #{class_name_to_stop_searching_on} && superclass.respond_to?(:#{name})
          superclass.#{name}
        end
      end
      def #{name}
        self.class.#{name}
      end
      EOS
    end
  end

  def superclass_delegating_writer(*names)
    names.each do |name|
      class_eval <<-EOS
        def self.#{name}=(value)
          @#{name} = value
        end
      EOS
    end
  end

  def superclass_delegating_accessor(*names)
    superclass_delegating_reader(*names)
    superclass_delegating_writer(*names)
  end
end

Version data entries

27 entries across 21 versions & 4 rubygems

Version Path
activesupport-2.0.0 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-2.0.1 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-2.0.4 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-2.0.5 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-2.0.2 lib/active_support/core_ext/class/delegating_attributes.rb
radiant-0.6.5.1 vendor/plugins/scenarios/lib/scenarios/extensions/delegating_attributes.rb
radiant-0.6.5.1 vendor/rails/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
radiant-0.6.5 vendor/rails/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
radiant-0.6.5 vendor/plugins/scenarios/lib/scenarios/extensions/delegating_attributes.rb
radiant-0.6.6 vendor/plugins/scenarios/lib/scenarios/extensions/delegating_attributes.rb
radiant-0.6.6 vendor/rails/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
radiant-0.6.7 vendor/plugins/scenarios/lib/scenarios/extensions/delegating_attributes.rb
radiant-0.6.7 vendor/rails/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
radiant-0.6.9 vendor/rails/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
radiant-0.6.8 vendor/plugins/scenarios/lib/scenarios/extensions/delegating_attributes.rb
radiant-0.6.8 vendor/rails/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
radiant-0.6.9 vendor/plugins/scenarios/lib/scenarios/extensions/delegating_attributes.rb
spree-0.0.9 vendor/rails/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
spree-0.2.0 vendor/rails/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
swivel-0.0.149 vendor/activesupport-2.0.2-/lib/active_support/core_ext/class/delegating_attributes.rb