Sha256: 824c9f89b11ac28b18a5a868b944f4e2b9277c4a0686fcdbb370625d367ca8a9

Contents?: true

Size: 1.22 KB

Versions: 31

Compression:

Stored size: 1.22 KB

Contents

# Extends the module object with module and instance accessors for class attributes, 
# just like the native attr* accessors for instance attributes.
class Module # :nodoc:
  def mattr_reader(*syms)
    syms.each do |sym|
      class_eval <<-EOS
        if ! defined? @@#{sym.id2name}
          @@#{sym.id2name} = nil
        end
        
        def self.#{sym.id2name}
          @@#{sym}
        end

        def #{sym.id2name}
          @@#{sym}
        end

        def call_#{sym.id2name}
          case @@#{sym.id2name}
            when Symbol then send(@@#{sym})
            when Proc   then @@#{sym}.call(self)
            when String then @@#{sym}
            else nil
          end
        end
      EOS
    end
  end
  
  def mattr_writer(*syms)
    syms.each do |sym|
      class_eval <<-EOS
        if ! defined? @@#{sym.id2name}
          @@#{sym.id2name} = nil
        end
        
        def self.#{sym.id2name}=(obj)
          @@#{sym.id2name} = obj
        end

        def self.set_#{sym.id2name}(obj)
          @@#{sym.id2name} = obj
        end

        def #{sym.id2name}=(obj)
          @@#{sym} = obj
        end
      EOS
    end
  end
  
  def mattr_accessor(*syms)
    mattr_reader(*syms)
    mattr_writer(*syms)
  end
end

Version data entries

31 entries across 31 versions & 5 rubygems

Version Path
devise_sociable-0.1.0 vendor/bundle/gems/actionpack-1.4.0/lib/action_controller/support/module_attribute_accessors.rb
actionpack-1.2.0 lib/action_controller/support/module_attribute_accessors.rb
actionpack-1.3.1 lib/action_controller/support/module_attribute_accessors.rb
actionpack-1.3.0 lib/action_controller/support/module_attribute_accessors.rb
actionpack-1.4.0 lib/action_controller/support/module_attribute_accessors.rb
activerecord-1.4.0 lib/active_record/support/module_attribute_accessors.rb
activerecord-1.5.0 lib/active_record/support/module_attribute_accessors.rb
activerecord-1.6.0 lib/active_record/support/module_attribute_accessors.rb
activerecord-1.5.1 lib/active_record/support/module_attribute_accessors.rb
activesupport-1.0.3 lib/active_support/module_attribute_accessors.rb
activesupport-1.0.1 lib/active_support/module_attribute_accessors.rb
activesupport-1.0.2 lib/active_support/module_attribute_accessors.rb
activesupport-1.0.4 lib/active_support/module_attribute_accessors.rb
activesupport-1.0.0 lib/active_support/module_attribute_accessors.rb
activesupport-1.2.1 lib/active_support/module_attribute_accessors.rb
activesupport-1.2.2 lib/active_support/module_attribute_accessors.rb
activesupport-1.2.4 lib/active_support/module_attribute_accessors.rb
activesupport-1.1.1 lib/active_support/module_attribute_accessors.rb
activesupport-1.1.0 lib/active_support/module_attribute_accessors.rb
activesupport-1.2.3 lib/active_support/module_attribute_accessors.rb