Sha256: 0bc2f4a32020865630849c4658bc40952fd9e6196ef89ba84dda5ea236e25047

Contents?: true

Size: 1.79 KB

Versions: 14

Compression:

Stored size: 1.79 KB

Contents

class Module
  def mattr_reader(*syms)
    receiver = self
    options = syms.extract_options!
    syms.each do |sym|
      raise NameError.new('invalid attribute name') unless sym =~ /^[_A-Za-z]\w*$/
      class_exec do
        unless class_variable_defined?("@@#{sym}")
          class_variable_set("@@#{sym}", nil)
        end

        define_singleton_method sym do
          class_variable_get("@@#{sym}")
        end
      end

      unless options[:instance_reader] == false || options[:instance_accessor] == false
        class_exec do
          define_method sym do
            receiver.class_variable_get("@@#{sym}")
          end
        end
      end
    end
  end

  def mattr_writer(*syms)
    receiver = self
    options = syms.extract_options!
    syms.each do |sym|
      raise NameError.new('invalid attribute name') unless sym =~ /^[_A-Za-z]\w*$/
      class_exec do
        define_singleton_method "#{sym}=" do |obj|
          class_variable_set("@@#{sym}", obj)
        end
      end

      unless options[:instance_writer] == false || options[:instance_accessor] == false
        class_exec do
          define_method "#{sym}=" do |obj|
            receiver.class_variable_set("@@#{sym}", obj)
          end
        end
      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!"
  def mattr_accessor(*syms)
    mattr_reader(*syms)
    mattr_writer(*syms)
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
motion-support-1.2.1 motion/core_ext/module/attribute_accessors.rb
motion-support-1.1.1 motion/core_ext/module/attribute_accessors.rb
motion-support-1.2.0 motion/core_ext/module/attribute_accessors.rb
motion-support-1.1.0 motion/core_ext/module/attribute_accessors.rb
motion-support-1.0.0 motion/core_ext/module/attribute_accessors.rb
motion-support-0.3.0 motion/core_ext/module/attribute_accessors.rb
motion_blender-support-0.2.8 motion/core_ext/module/attribute_accessors.rb
motion_blender-support-0.2.7 motion/core_ext/module/attribute_accessors.rb
motion-support-0.2.6 motion/core_ext/module/attribute_accessors.rb
motion-support-0.2.5 motion/core_ext/module/attribute_accessors.rb
motion-support-0.2.4 motion/core_ext/module/attribute_accessors.rb
motion-support-0.2.3 motion/core_ext/module/attribute_accessors.rb
motion-support-0.2.2 motion/core_ext/module/attribute_accessors.rb
motion-support-0.2.0 motion/core_ext/module/attribute_accessors.rb