Class: Class

Inherits:
Object show all
Defined in:
lib/rcal/util/class_attributes.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) cattr_accessor(*syms)

cattr_reader and cattr_writer in one.

Thanks, Rails!



62
63
64
65
# File 'lib/rcal/util/class_attributes.rb', line 62

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

- (Object) cattr_reader(*syms)

Defines class- and instance-level readers for each class attribute in syms. If any of syms is not yet defined, it is set to nil.

Thanks, Rails!



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rcal/util/class_attributes.rb', line 11

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

- (Object) cattr_writer(*syms)

Defines class- and instance-level writers for each class attribute in syms. If any of syms is not yet defined, it is set to nil.

If syms.last.is_a?(Hash) && syms.last[:instance_writer] == false, does not declare an instance-level writer.

Thanks, Rails!



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rcal/util/class_attributes.rb', line 38

def cattr_writer(*syms)
  options = syms.extract_options!
  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