Cattr_Accessor Outside of Rails

cattr_accessor outside of rails

cattr_accessor seems to be a Rails extension that acts like attr_accessor, but is accessible on both the class and its instances.

If you want to copy the source of the cattr_accessor method, check out this documentation:

# File vendor/rails/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb, line 46
def cattr_accessor(*syms)
cattr_reader(*syms)
cattr_writer(*syms)
end

# File vendor/rails/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb, line 4
def cattr_reader(*syms)
syms.flatten.each do |sym|
next if sym.is_a?(Hash)
class_eval("unless defined? @@\#{sym}\n@@\#{sym} = nil\nend\n\ndef self.\#{sym}\n@@\#{sym}\nend\n\ndef \#{sym}\n@@\#{sym}\nend\n", __FILE__, __LINE__)
end
end

# File vendor/rails/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb, line 24
def cattr_writer(*syms)
options = syms.extract_options!
syms.flatten.each do |sym|
class_eval("unless defined? @@\#{sym}\n@@\#{sym} = nil\nend\n\ndef self.\#{sym}=(obj)\n@@\#{sym} = obj\nend\n\n\#{\"\ndef \#{sym}=(obj)\n@@\#{sym} = obj\nend\n\" unless options[:instance_writer] == false }\n", __FILE__, __LINE__)
end
end

cattr_accessor not working (outside rails) when active_support is required?

With more googling I found this answer - seems to work for me!

I'm now requiring: require 'active_support/all'

I hope this helps someone!

cattr_accessor in Rails?

It is a rails thing. Basically like the attr_* methods, but for the class level. One thing you wouldn't expect is because it uses a backing @@ variable, the value shared between the class and all instances.

class Foo
cattr_accessor :bar
end
# => [:bar]
foo1 = Foo.new
# => #<Foo:0x4874d90>
foo2 = Foo.new
# => #<Foo:0x4871d48>
foo1.bar = 'set from instance'
# => "set from instance"
foo2.bar
# => "set from instance"
Foo.bar
# => "set from instance"

Difference between mattr_accessor and cattr_accessor in ActiveSupport?

Module is the superclass of the class Class so if a suitably generic name could be thought of then the methods for defining accessors could be put on Module and it would work for modules and classes. Notice that the follow works:

class Bar
mattr_accessor :test
end

but

module Foo
cattr_accessor :test
end

wouldn't work.

Having a c prefix on the methods that should be used inside classes and an m prefix on the methods for use inside modules just helps to make your code a bit clearer.

cattr_accessor default value syntax

Isn't this just following the language binding precedence?

  • Brace form has higher precedence and will bind to the last parameter
    if invocation made w/o parens.
  • do/end form has lower precedence and
    will bind to the invocation even without parens.

If you don't want to write the do end form, you will need to put parenthesis around the call.

cattr_accessor(:is_admin) { true }

What is mattr_accessor in a Rails module?

Rails extends Ruby with both mattr_accessor (Module accessor) and cattr_accessor (as well as _reader/_writer versions). As Ruby's attr_accessor generates getter/setter methods for instances, cattr/mattr_accessor provide getter/setter methods at the class or module level. Thus:

module Config
mattr_accessor :hostname
mattr_accessor :admin_email
end

is short for:

module Config
def self.hostname
@hostname
end
def self.hostname=(hostname)
@hostname = hostname
end
def self.admin_email
@admin_email
end
def self.admin_email=(admin_email)
@admin_email = admin_email
end
end

Both versions allow you to access the module-level variables like so:

>> Config.hostname = "example.com"
>> Config.admin_email = "admin@example.com"
>> Config.hostname # => "example.com"
>> Config.admin_email # => "admin@example.com"

Why does my concern's cattr_accessor get set to the same value across all subclasses?

from reddit user ctcherry:

Instead of cattr_accessor try this:

class << self
attr_accessor :foo
end

Good information about what is going on here: http://apidock.com/rails/Class/cattr_accessor



Related Topics



Leave a reply



Submit