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("      unless defined? @@\#{sym}\n        @@\#{sym} = nil\n      end\n\n      def self.\#{sym}\n        @@\#{sym}\n      end\n\n      def \#{sym}\n        @@\#{sym}\n      end\n    EOS\n  end\nend\n", __FILE__, __LINE__)

#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("      unless defined? @@\#{sym}\n        @@\#{sym} = nil\n      end\n\n      def self.\#{sym}=(obj)\n        @@\#{sym} = obj\n      end\n\n      \#{\"\n      def \#{sym}=(obj)\n        @@\#{sym} = obj\n      end\n      \" unless options[:instance_writer] == false }\n    EOS\n  end\nend\n", __FILE__, __LINE__)