class Module
Extends the module object with class/module and instance accessors for class/module attributes, just like the native attr* accessors for instance attributes.
Public Instance Methods
mattr_accessor(*syms, &blk)
click to toggle source
Defines both class and instance accessors for class attributes.
# File lib/accessor_extender/module.rb, line 43 def mattr_accessor(*syms, &blk) mattr_reader(*syms, &blk) mattr_writer(*syms, &blk) end
Also aliased as: cattr_accessor
mattr_reader(*syms) { || ... }
click to toggle source
Defines a class attribute and creates a class and instance reader methods. The underlying the class variable is set to nil
, if it is not previously defined.
# File lib/accessor_extender/module.rb, line 10 def mattr_reader(*syms) syms.each do |sym| raise NameError.new("invalid attribute name: #{sym}") unless sym =~ /^[_A-Za-z]\w*$/ class_eval(<<-EOS, __FILE__, __LINE__ + 1) @@#{sym} = nil unless defined? @@#{sym} def self.#{sym} @@#{sym} end EOS class_variable_set("@@#{sym}", yield) if block_given? end end
Also aliased as: cattr_reader
mattr_writer(*syms) { || ... }
click to toggle source
Defines a class attribute and creates a class and instance writer methods to allow assignment to the attribute.
# File lib/accessor_extender/module.rb, line 27 def mattr_writer(*syms) syms.each do |sym| raise NameError.new("invalid attribute name: #{sym}") unless sym =~ /^[_A-Za-z]\w*$/ class_eval(<<-EOS, __FILE__, __LINE__ + 1) @@#{sym} = nil unless defined? @@#{sym} def self.#{sym}=(obj) @@#{sym} = obj end EOS send("#{sym}=", yield) if block_given? end end
Also aliased as: cattr_writer