Class: Module
- Inherits:
-
Object
- Object
- Module
- Defined in:
- lib/defensor/cattr.rb
Instance Method Summary collapse
-
#cattr(*syms) ⇒ Object
Creates a class-variable attribute that can be accessed both on an instance and class level.
-
#cattr_accessor(*syms) ⇒ Object
Creates a class-variable attr_accessor that can be accessed both on an instance and class level.
-
#cattr_reader(*syms) ⇒ Object
Creates a class-variable attr_reader that can be accessed both on an instance and class level.
-
#cattr_writer(*syms) ⇒ Object
Creates a class-variable attr_writer that can be accessed both on an instance and class level.
-
#mattr(*syms) ⇒ Object
Creates a class-variable attribute that can be accessed both on an instance and class level.
-
#mattr_accessor(*syms) ⇒ Object
Creates a class-variable attr_accessor that can be accessed both on an instance and class level.
-
#mattr_reader(*syms) ⇒ Object
Creates a class-variable attr_reader that can be accessed both on an instance and class level.
-
#mattr_writer(*syms) ⇒ Object
Creates a class-variable attr_writer that can be accessed both on an instance and class level.
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 method is not a common core extension and is not loaded automatically when using require 'facets'
.
CREDIT: David Heinemeier Hansson
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/defensor/cattr.rb', line 22 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 method is not a common core extension and is not loaded automatically when using require 'facets'
.
CREDIT: David Heinemeier Hansson
133 134 135 |
# File 'lib/defensor/cattr.rb', line 133 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 method is not a common core extension and is not loaded automatically when using require 'facets'
.
CREDIT: David Heinemeier Hansson
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/defensor/cattr.rb', line 52 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 method is not a common core extension and is not loaded automatically when using require 'facets'
.
CREDIT: David Heinemeier Hansson
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/defensor/cattr.rb', line 94 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 methiod is not a common core extension and is not loaded automatically when using require 'facets'
.
CREDIT: David Heinemeier Hansson
162 163 164 165 166 167 168 169 170 171 |
# File 'lib/defensor/cattr.rb', line 162 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 methiod is not a common core extension and is not loaded automatically when using require 'facets'
.
CREDIT: David Heinemeier Hansson
275 276 277 |
# File 'lib/defensor/cattr.rb', line 275 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 methiod is not a common core extension and is not loaded automatically when using require 'facets'
.
CREDIT: David Heinemeier Hansson
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/defensor/cattr.rb', line 192 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 methiod is not a common core extension and is not loaded automatically when using require 'facets'
.
CREDIT: David Heinemeier Hansson
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/defensor/cattr.rb', line 235 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 |