Class: Class

Inherits:
Object
  • Object
show all
Defined in:
lib/attribute_accessors.rb

Overview

Extends the class object with class and instance accessors for class attributes, just like the native attr* accessors for instance attributes.

Instance Method Summary collapse

Instance Method Details

#cattr_accessor(*syms) ⇒ Object



119
120
121
122
# File 'lib/attribute_accessors.rb', line 119

def cattr_accessor(*syms)
  cattr_reader(*syms)
  cattr_writer(*syms)
end

#cattr_reader(*syms) ⇒ Object

:nodoc:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/attribute_accessors.rb', line 79

def cattr_reader(*syms)
  syms.flatten.each do |sym|
    next if sym.is_a?(Hash)
    class_eval(<<-EOS, __FILE__, __LINE__)
      unless defined? @@#{sym}
        @@#{sym} = nil
      end

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

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

#cattr_writer(*syms) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/attribute_accessors.rb', line 98

def cattr_writer(*syms)
  options = syms.last.is_a?(Hash) ? syms.pop : {}
  syms.flatten.each do |sym|
    class_eval(<<-EOS, __FILE__, __LINE__)
      unless defined? @@#{sym}
        @@#{sym} = nil
      end

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

      #{"
      def #{sym}=(obj)
        @@#{sym} = obj
      end
      " unless options[:instance_writer] == false }
    EOS
  end
end