Class: Class

Inherits:
Object
  • Object
show all
Defined in:
lib/databasedotcom/core_extensions/class_extensions.rb

Overview

This extends Class to be able to use cattr_accessor if active_support is not being used.

Instance Method Summary collapse

Instance Method Details

#cattr_accessor(*syms, &blk) ⇒ Object



36
37
38
39
# File 'lib/databasedotcom/core_extensions/class_extensions.rb', line 36

def cattr_accessor(*syms, &blk)
  cattr_reader(*syms)
  cattr_writer(*syms, &blk)
end

#cattr_reader(sym) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/databasedotcom/core_extensions/class_extensions.rb', line 4

def cattr_reader(sym)
  class_eval(<<-EOS, __FILE__, __LINE__ + 1)
    unless defined? @@#{sym}
      @@#{sym} = nil
    end

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

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

#cattr_writer(sym) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/databasedotcom/core_extensions/class_extensions.rb', line 20

def cattr_writer(sym)
  class_eval(<<-EOS, __FILE__, __LINE__ + 1)
    unless defined? @@#{sym}
      @@#{sym} = nil
    end

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

    def #{sym}=(obj)
      @@#{sym} = obj
    end
  EOS
end