Class: Module

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

Instance Method Summary collapse

Instance Method Details

#cattr(*syms) ⇒ Object

Creates a class-variable attribute that can be accessed both on an instance and class level.

class CARExample
  @@a = 10
  cattr :a
end

CARExample.a           #=> 10
CARExample.new.a       #=> 10

NOTE: This is not (presently) a common core extension and is not loaded automatically when using require 'facets'.

CREDIT: David Heinemeier Hansson



18
19
20
21
22
23
24
25
26
27
# File 'lib/oa_test/cattr.rb', line 18

def cattr(*syms)
  writers, readers = syms.flatten.partition{ |a| a.to_s =~ /=$/ }
  writers = writers.map{ |e| e.to_s.chomp('=').to_sym }
  ##readers.concat( writers ) # writers also get readers

  cattr_reader(*readers)
  cattr_writer(*writers)

  return readers + writers
end

#cattr_accessor(*syms) ⇒ Object

Creates a class-variable attr_accessor that can be accessed both on an instance and class level.

class CAAExample
  cattr_accessor :a
end

CAAExample.a = 10
CAAExample.a           #=> 10
mc = CAAExample.new
mc.a                   #=> 10

NOTE: This is not (presently) a common core extension and is not loaded automatically when using require 'facets'.

CREDIT: David Heinemeier Hansson



117
118
119
# File 'lib/oa_test/cattr.rb', line 117

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

#cattr_reader(*syms) ⇒ Object

Creates a class-variable attr_reader that can be accessed both on an instance and class level.

class CARExample
  @@a = 10
  cattr_reader :a
end

CARExample.a           #=> 10
CARExample.new.a       #=> 10

NOTE: This is not (presently) a common core extension and is not loaded automatically when using require 'facets'.

CREDIT: David Heinemeier Hansson



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/oa_test/cattr.rb', line 44

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

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

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

#cattr_writer(*syms) ⇒ Object

Creates a class-variable attr_writer that can be accessed both on an instance and class level.

class CAWExample
  cattr_writer :a
  def self.a
    @@a
  end
end

CAWExample.a = 10
CAWExample.a            #=> 10
CAWExample.new.a = 29
CAWExample.a            #=> 29

NOTE: This is not (presently) a common core extension and is not loaded automatically when using require 'facets'.

CREDIT: David Heinemeier Hansson



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/oa_test/cattr.rb', line 82

def cattr_writer(*syms)
  syms.flatten.each do |sym|
    module_eval(<<-EOS, __FILE__, __LINE__)
      unless defined? @@#{sym}
        @@#{sym} = nil
      end

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

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

#mattr(*syms) ⇒ Object

Creates a class-variable attribute that can be accessed both on an instance and class level.

c = Class.new do
  mattr :a
  def initialize
    @@a = 10
  end
end

c.new.a       #=> 10
c.a           #=> 10

NOTE: The #mattr methods may not be as useful for modules as the #cattr methods are for classes, becuase class-level methods are not “inherited” across the metaclass for included modules.

NOTE: This is not (presently) a common core extension and is not loaded automatically when using require 'facets'.

CREDIT: David Heinemeier Hansson



142
143
144
145
146
147
148
149
150
151
# File 'lib/oa_test/cattr.rb', line 142

def mattr(*syms)
  writers, readers = syms.flatten.partition{ |a| a.to_s =~ /=$/ }
  writers = writers.collect{ |e| e.to_s.chomp('=').to_sym }
  ##readers.concat( writers ) # writers also get readers

  mattr_writer( *writers )
  mattr_reader( *readers )

  return readers + writers
end

#mattr_accessor(*syms) ⇒ Object

Creates a class-variable attr_accessor that can be accessed both on an instance and class level.

c = Class.new do
  mattr_accessor :a
end

c.a = 10
c.a           #=> 10

x = c.new
x.a           #=> 10

NOTE: This is not (presently) a common core extension and is not loaded automatically when using require 'facets'.

CREDIT: David Heinemeier Hansson



243
244
245
# File 'lib/oa_test/cattr.rb', line 243

def mattr_accessor(*syms)
  mattr_reader(*syms) + mattr_writer(*syms)
end

#mattr_reader(*syms) ⇒ Object

Creates a class-variable attr_reader that can be accessed both on an instance and class level.

c = Class.new do
  @@a = 10
  mattr_reader :a
end

c.a           #=> 10
c.new.a       #=> 10

NOTE: This is not (presently) a common core extension and is not loaded automatically when using require 'facets'.

CREDIT: David Heinemeier Hansson



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/oa_test/cattr.rb', line 168

def mattr_reader( *syms )
  syms.flatten.each do |sym|
    module_eval(<<-EOS, __FILE__, __LINE__)
      unless defined? @@#{sym}
        @@#{sym} = nil
      end

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

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

#mattr_writer(*syms) ⇒ Object

Creates a class-variable attr_writer that can be accessed both on an instance and class level.

c = Class.new do
  mattr_writer :a
  def self.a
    @@a
  end
end

c.a = 10
c.a            #=> 10

c.new.a = 29
c.a            #=> 29

NOTE: This is not (presently) a common core extension and is not loaded automatically when using require 'facets'.

CREDIT: David Heinemeier Hansson



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/oa_test/cattr.rb', line 207

def mattr_writer(*syms)
  syms.flatten.each do |sym|
    module_eval(<<-EOS, __FILE__, __LINE__)
      unless defined? @@#{sym}
        @@#{sym} = nil
      end

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

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