Sha256: 5e0e2521b610d23cf88765fe162d3ef7c3cc168a11a0fde6830b5c75d4cc319b

Contents?: true

Size: 1.94 KB

Versions: 30

Compression:

Stored size: 1.94 KB

Contents

require 'active_support/core_ext/array/extract_options'

class Module
  def mattr_reader(*syms)
    options = syms.extract_options!
    syms.each do |sym|
      raise NameError.new('invalid attribute name') unless sym =~ /^[_A-Za-z]\w*$/
      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|
      raise NameError.new('invalid attribute name') unless sym =~ /^[_A-Za-z]\w*$/
      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"
  #   end
  #
  #   AppConfiguration.google_api_key # => "123456789"
  #   AppConfiguration.google_api_key = "overriding the api key!"
  #   AppConfiguration.google_api_key # => "overriding the api key!"
  #
  # To opt out of the instance writer method, pass <tt>instance_writer: false</tt>.
  # To opt out of the instance reader method, pass <tt>instance_reader: false</tt>.
  # To opt out of both instance methods, pass <tt>instance_accessor: false</tt>.
  def mattr_accessor(*syms)
    mattr_reader(*syms)
    mattr_writer(*syms)
  end
end

Version data entries

30 entries across 30 versions & 2 rubygems

Version Path
activesupport-4.0.13 lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-4.0.13.rc1 lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-4.0.11.1 lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-4.0.12 lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-4.0.11 lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-4.0.10 lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-4.0.10.rc2 lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-4.0.10.rc1 lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-4.0.9 lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-4.0.8 lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-4.0.7 lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-4.0.6 lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-4.0.6.rc3 lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-4.0.6.rc2 lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-4.0.6.rc1 lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-4.0.5 lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-4.0.4 lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-4.0.4.rc1 lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-4.0.3 lib/active_support/core_ext/module/attribute_accessors.rb
activesupport-4.0.2 lib/active_support/core_ext/module/attribute_accessors.rb