Sha256: 4e316de3fa7ea21cdbdff25a1e706702c4ce693ba4e315f24da34f431a062b23

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

class Class # :nodoc:
  def meta_reader(*syms)
    syms.each do |sym|
      class_eval <<-EOS
        def self.#{sym}
          read_meta_attribute(:#{sym})
        end

        def #{sym}
          self.class.#{sym}
        end
      EOS
    end
  end

  def meta_writer(*syms)
    syms.each do |sym|
      class_eval <<-EOS
        def self.#{sym}=(obj)
          write_meta_attribute(:#{sym}, obj)
        end

        def #{sym}=(obj)
          self.class.#{sym} = obj
        end
      EOS
    end
  end

  def meta_accessor(*syms)
    meta_reader(*syms)
    meta_writer(*syms)
  end

  def meta_attributes
    @meta_attributes ||= {}
  end
  
  def write_meta_attribute(key, value)
    meta_attributes[key] = value
  end

  def read_meta_attribute(key)
    meta_attributes[key]
  end
  
  def reset_meta_attributes
    meta_attributes.clear
  end

  private 
    def inherited_with_meta_attributes(child)
      inherited_without_meta_attributes(child) if respond_to?(:inherited_without_meta_attributes)
      
      new_meta_attributes = meta_attributes.inject({}) do |memo, (key, value)|
        memo.update(key => (value.dup rescue value))
      end
      
      child.instance_variable_set('@meta_attributes', new_meta_attributes)
    end

    alias inherited_without_meta_attributes inherited
    alias inherited inherited_with_meta_attributes
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
merb-0.0.7 lib/merb/merb_class_extensions.rb