Sha256: fcebb7fe6d03e7f89ed2a4c80f48cd921446d06b032e6a862d607b1d49c9bcfa

Contents?: true

Size: 1.28 KB

Versions: 39

Compression:

Stored size: 1.28 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
      def self.#{name}?
        !!#{name}
      end
      def #{name}?
        !!#{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

39 entries across 38 versions & 13 rubygems

Version Path
radiant-0.7.2 vendor/rails/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
vibes-bj-1.2.2 spec/rails_root/vendor/rails/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
vibes-bj-1.2.1 spec/rails_root/vendor/rails/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
nbudin-castronaut-0.7.5 vendor/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
usher-0.7.0 spec/rails2_2/vendor/rails/vendor/rails/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
usher-0.7.0 spec/rails2_2/vendor/rails/vendor/rails/activesupport/pkg/activesupport-2.2.2/lib/active_support/core_ext/class/delegating_attributes.rb
factorylabs-castronaut-0.7.5 vendor/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-2.2.3 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-2.1.0 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-2.2.2 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-2.1.1 lib/active_support/core_ext/class/delegating_attributes.rb
activesupport-2.1.2 lib/active_support/core_ext/class/delegating_attributes.rb
antfarm-0.3.0 rails/vendor/rails/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
antfarm-0.4.0 rails/vendor/rails/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
mack-active_record-0.8.2 lib/gems/activesupport-2.2.2/lib/active_support/core_ext/class/delegating_attributes.rb
mack-facets-0.8.3.1 lib/gems/activesupport-2.2.2/lib/active_support/core_ext/class/delegating_attributes.rb
mack-facets-0.8.3 lib/gems/activesupport-2.2.2/lib/active_support/core_ext/class/delegating_attributes.rb
radiant-0.7.0 vendor/rails/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
radiant-0.7.1 vendor/rails/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb