Sha256: 1a27607d3acc03d25df8b47136c57802d4eb253a6c0efcfc0f2063733ba51740

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

# Original code from Rails distribution.
# http://www.rubyonrails.com

#--
# Extends the module object with module and instance accessors 
# for class attributes, just like the native attr* accessors for 
# instance attributes. Aliases for classes are also provided.
#
# Example:
#
# mattr_accessor :my_attr, 'Default value'
#
# FIXME: I think those methods do *not* work as expected.
# FIXME: I should probably use @-attributes instead of 
#        @@-attributes.
#++

class Module # :nodoc:

	# A macro that creates a reader method for class/module 
	# attributes.
		
  def mattr_reader(*params)
		default = if params.last.is_a?(Symbol) then nil else params.pop end
		
    for sym in params
			module_eval <<-"end_eval", __FILE__, __LINE__
			
        if not defined?(@@#{sym.id2name})
          @@#{sym.id2name} = #{default.inspect}
        end
        
        def self.#{sym.id2name}
          @@#{sym}
        end

      end_eval
    end
  end
  alias_method :cattr_reader, :mattr_reader

	# A macro that creates a writer method for class/module 
	# attributes.
  
  def mattr_writer(*params)
		default = if params.last.is_a?(Symbol) then nil else params.pop end
		
    for sym in params
			module_eval <<-"end_eval", __FILE__, __LINE__

        if not defined?(@@#{sym.id2name})
          @@#{sym.id2name} = #{default.inspect.inspect}
        end
        
        def self.#{sym.id2name}=(obj)
          @@#{sym.id2name} = obj
        end

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

      end_eval
    end
  end
  alias_method :cattr_writer, :cattr_writer

	# A macro that creates a reader and a writer method for 
	# class/module attributes.
  
  def mattr_accessor(*syms)
    mattr_reader(*syms)
    mattr_writer(*syms)
  end
  alias_method :cattr_accessor, :mattr_accessor
	
end

# * George Moschovitis <gm@navel.gr>

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
glue-0.20.0 lib/glue/attribute.rb