Sha256: 43038e362c022b103e58edf998577677d0ee21162802a7bddeeeec17bfbf5096

Contents?: true

Size: 1.15 KB

Versions: 54

Compression:

Stored size: 1.15 KB

Contents

#This module allows for an attribute to be defined on a superclass and carry down into sub-classes with its default.
#Taken from http://railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/

module ClassLevelInheritableAttributes
  def self.included(base)
    base.extend(ClassMethods)    
  end
  
  module ClassMethods
    def inheritable_attributes(*args)
      #for some reason, in rails, @inheritable_attributes is set to be an empty hash here...
      #check for this strange case and account for it.
      @inheritable_attributes = [:inheritable_attributes] if @inheritable_attributes == {} ||
                                                             @inheritable_attributes == nil
      @inheritable_attributes += args
      args.each do |arg|
        class_eval %(
          class << self; attr_accessor :#{arg} end
        )
      end
      @inheritable_attributes
    end
    
    def inherited(subclass)
      @inheritable_attributes.each do |inheritable_attribute|
        instance_var = "@#{inheritable_attribute}"
        subclass.instance_variable_set(instance_var, instance_variable_get(instance_var))
      end
    end
  end
end

Version data entries

54 entries across 54 versions & 2 rubygems

Version Path
medea-0.7.2 lib/medea/inheritable_attributes.rb
medea-0.7.1 lib/medea/inheritable_attributes.rb
medea-0.7.0 lib/medea/inheritable_attributes.rb
medea-0.6.9 lib/medea/inheritable_attributes.rb
medea-0.6.8 lib/medea/inheritable_attributes.rb
medea-0.6.7 lib/medea/inheritable_attributes.rb
medea-0.6.6 lib/medea/inheritable_attributes.rb
medea-0.6.5 lib/medea/inheritable_attributes.rb
medea-0.6.4 lib/medea/inheritable_attributes.rb
medea-0.6.3 lib/medea/inheritable_attributes.rb
medea-0.6.1 lib/medea/inheritable_attributes.rb
medea-0.6.0 lib/medea/inheritable_attributes.rb
medea-0.5.4 lib/medea/inheritable_attributes.rb
medea-0.5.3 lib/medea/inheritable_attributes.rb
medea-0.5.2 lib/medea/inheritable_attributes.rb
medea-0.5.1 lib/medea/inheritable_attributes.rb
medea-0.5.0 lib/medea/inheritable_attributes.rb
medea-0.4.0 lib/medea/inheritable_attributes.rb
medea-0.3.7 lib/medea/inheritable_attributes.rb
medea-0.3.6 lib/medea/inheritable_attributes.rb