Sha256: fae2de246228fea9bb42282f7d04f2289e3705cb0c461d279a2ec8222a198dc2

Contents?: true

Size: 1.82 KB

Versions: 51

Compression:

Stored size: 1.82 KB

Contents

module InheritableClassAttributes
  def self.included(base)
    base.extend ClassMethods
    base.module_eval do
      class << self
        alias inherited_without_inheritable_class_attributes inherited
        alias inherited inherited_with_inheritable_class_attributes
      end
    end
  end
  
  module ClassMethods
    def inheritable_cattr_readers
      @inheritable_class_readers ||= []
    end
    
    def inheritable_cattr_writers
      @inheritable_class_writers ||= []
    end
    
    def cattr_inheritable_reader(*symbols)
      symbols.each do |symbol|
        self.inheritable_cattr_readers << symbol
        self.module_eval %{
          def self.#{symbol}
            @#{symbol}
          end 
        }
      end
      self.inheritable_cattr_readers.uniq!
    end
    
    def cattr_inheritable_writer(*symbols)
      symbols.each do |symbol|
        self.inheritable_cattr_writers << symbol
        self.module_eval %{
          def self.#{symbol}=(value)
            @#{symbol} = value
          end 
        }
      end
      self.inheritable_cattr_writers.uniq!
    end
    
    def cattr_inheritable_accessor(*symbols)
      cattr_inheritable_writer(*symbols)
      cattr_inheritable_reader(*symbols)
    end
    
    def inherited_with_inheritable_class_attributes(klass)
      inherited_without_inheritable_class_attributes(klass) if respond_to?(:inherited_without_inheritable_class_attributes)
      
      readers = inheritable_cattr_readers.dup
      writers = inheritable_cattr_writers.dup
      inheritables = [:inheritable_cattr_readers, :inheritable_cattr_writers]
      
      (readers + writers + inheritables).uniq.each do |attr|
        var = "@#{attr}"
        old_value = self.module_eval(var)
        new_value = (old_value.dup rescue old_value)
        klass.module_eval("#{var} = new_value")
      end
    end
  end
end

Version data entries

51 entries across 51 versions & 5 rubygems

Version Path
trusty-cms-1.0.0 lib/inheritable_class_attributes.rb
radiant-1.1.4 lib/inheritable_class_attributes.rb
radiant-1.1.3 lib/inheritable_class_attributes.rb
radiant-1.1.2 lib/inheritable_class_attributes.rb
radiant-1.1.1 lib/inheritable_class_attributes.rb
radiant-1.1.0 lib/inheritable_class_attributes.rb
radiant-1.1.0.rc1 lib/inheritable_class_attributes.rb
radiant-1.1.0.beta lib/inheritable_class_attributes.rb
radiant-1.0.1 lib/inheritable_class_attributes.rb
radiant-1.1.0.alpha lib/inheritable_class_attributes.rb
radiant-1.0.0 lib/inheritable_class_attributes.rb
radiant-1.0.0.rc5 lib/inheritable_class_attributes.rb
radiant-1.0.0.rc4 lib/inheritable_class_attributes.rb
radiant-1.0.0.rc3 lib/inheritable_class_attributes.rb
kajam-1.0.3.rc2 lib/inheritable_class_attributes.rb
radiant-1.0.0.rc2 lib/inheritable_class_attributes.rb
radiant-1.0.0.rc1 lib/inheritable_class_attributes.rb
radiantcms-couchrest_model-0.2.4 lib/inheritable_class_attributes.rb
radiantcms-couchrest_model-0.2.2 lib/inheritable_class_attributes.rb
radiantcms-couchrest_model-0.2.1 lib/inheritable_class_attributes.rb