Sha256: 0a7f892a2cb01ca830a84b982ea2d5668126b1258589ced9443eff783b57ef1f

Contents?: true

Size: 1.72 KB

Versions: 215

Compression:

Stored size: 1.72 KB

Contents

require 'active_support/core_ext/array/extract_options'

class Module
  def mattr_reader(*syms)
    options = syms.extract_options!
    syms.each do |sym|
      class_eval(<<-EOS, __FILE__, __LINE__ + 1)
        @@#{sym} = nil unless defined? @@#{sym}

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

      unless options[:instance_reader] == false || options[:instance_accessor] == false
        class_eval(<<-EOS, __FILE__, __LINE__ + 1)
          def #{sym}
            @@#{sym}
          end
        EOS
      end
    end
  end

  def mattr_writer(*syms)
    options = syms.extract_options!
    syms.each do |sym|
      class_eval(<<-EOS, __FILE__, __LINE__ + 1)
        def self.#{sym}=(obj)
          @@#{sym} = obj
        end
      EOS

      unless options[:instance_writer] == false || options[:instance_accessor] == false
        class_eval(<<-EOS, __FILE__, __LINE__ + 1)
          def #{sym}=(obj)
            @@#{sym} = obj
          end
        EOS
      end
    end
  end

  # 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!"
  #
  # To opt out of the instance writer method, pass :instance_writer => false.
  # To opt out of the instance reader method, pass :instance_reader => false.
  # To opt out of both instance methods, pass :instance_accessor => false.
  def mattr_accessor(*syms)
    mattr_reader(*syms)
    mattr_writer(*syms)
  end
end

Version data entries

215 entries across 171 versions & 24 rubygems

Version Path
challah-0.6.0 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/module/attribute_accessors.rb
challah-0.6.0 vendor/bundle/gems/activesupport-3.2.1/lib/active_support/core_ext/module/attribute_accessors.rb
challah-0.6.0 vendor/bundle/gems/activesupport-3.2.2/lib/active_support/core_ext/module/attribute_accessors.rb
challah-0.5.4 vendor/bundle/gems/activesupport-3.2.2/lib/active_support/core_ext/module/attribute_accessors.rb
challah-0.5.4 vendor/bundle/gems/activesupport-3.2.1/lib/active_support/core_ext/module/attribute_accessors.rb
classicCMS-0.2.3 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/module/attribute_accessors.rb
classicCMS-0.2.2 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/module/attribute_accessors.rb
classicCMS-0.2.1 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/module/attribute_accessors.rb
classicCMS-0.2.0 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/module/attribute_accessors.rb
classicCMS-0.1.2 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-3.2.3 lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-3.2.3.rc2 lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-3.2.3.rc1 lib/active_support/core_ext/module/attribute_accessors.rb
challah-0.5.3 vendor/bundle/gems/activesupport-3.2.2/lib/active_support/core_ext/module/attribute_accessors.rb
challah-0.5.3 vendor/bundle/gems/activesupport-3.2.1/lib/active_support/core_ext/module/attribute_accessors.rb
challah-0.5.2 vendor/bundle/gems/activesupport-3.2.1/lib/active_support/core_ext/module/attribute_accessors.rb
challah-0.5.2 vendor/bundle/gems/activesupport-3.2.2/lib/active_support/core_ext/module/attribute_accessors.rb
challah-0.5.1 vendor/bundle/gems/activesupport-3.2.2/lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-3.2.2 lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-3.2.2.rc1 lib/active_support/core_ext/module/attribute_accessors.rb