Sha256: de970bd8f2bf3a97d25c6180846b674e1676dec3e407b0546e6329a64cc75796

Contents?: true

Size: 1.25 KB

Versions: 42

Compression:

Stored size: 1.25 KB

Contents

# Extends the module object with module and instance accessors for class attributes, 
# just like the native attr* accessors for instance attributes.
#
#  module AppConfiguration
#    mattr_accessor :google_api_key
#    self.google_api_key = "123456789"
#
#    mattr_accessor :paypal_url
#    self.paypal_url = "www.sandbox.paypal.com"
#  end
#
#  AppConfiguration.google_api_key = "overriding the api key!"
class Module
  def mattr_reader(*syms)
    syms.each do |sym|
      next if sym.is_a?(Hash)
      class_eval(<<-EOS, __FILE__, __LINE__)
        unless defined? @@#{sym}
          @@#{sym} = nil
        end
        
        def self.#{sym}
          @@#{sym}
        end

        def #{sym}
          @@#{sym}
        end
      EOS
    end
  end
  
  def mattr_writer(*syms)
    options = syms.extract_options!
    syms.each do |sym|
      class_eval(<<-EOS, __FILE__, __LINE__)
        unless defined? @@#{sym}
          @@#{sym} = nil
        end
        
        def self.#{sym}=(obj)
          @@#{sym} = obj
        end
        
        #{"
        def #{sym}=(obj)
          @@#{sym} = obj
        end
        " unless options[:instance_writer] == false }
      EOS
    end
  end
  
  def mattr_accessor(*syms)
    mattr_reader(*syms)
    mattr_writer(*syms)
  end
end

Version data entries

42 entries across 41 versions & 15 rubygems

Version Path
3mix-castronaut-0.5.0.2 vendor/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
masover-castronaut-0.4.4.4 vendor/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
masover-castronaut-0.4.4.5 vendor/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
masover-castronaut-0.5.0.1 vendor/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
p8-castronaut-0.6.1.1 vendor/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
relevance-castronaut-0.4.1 vendor/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
relevance-castronaut-0.4.2 vendor/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
relevance-castronaut-0.4.3 vendor/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
relevance-castronaut-0.4.4 vendor/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
relevance-castronaut-0.4.5 vendor/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
relevance-castronaut-0.4.6 vendor/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
relevance-castronaut-0.5.0 vendor/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
relevance-castronaut-0.5.1 vendor/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
relevance-castronaut-0.5.2 vendor/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
relevance-castronaut-0.5.3 vendor/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
relevance-castronaut-0.5.4 vendor/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
relevance-castronaut-0.6.0 vendor/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
relevance-castronaut-0.6.1 vendor/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
relevance-castronaut-0.7.4 vendor/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
relevance-castronaut-0.7.5 vendor/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb