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



126
127
128
129
# File 'lib/attribute_accessors.rb', line 126

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

#cattr_reader(*syms) ⇒ Object

:nodoc:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/attribute_accessors.rb', line 86

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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/attribute_accessors.rb', line 105

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