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"
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.
Rails plugin: undefined method 'mattr_accessor' for Sentry:Module
I'm afraid this resolved itself while I was working on other issues. I'm not sure exactly what the problem was, but I believe it had to do with outdated/conflicting versions of plugins and gems.
Rails: thread_mattr_accessor randomly becoming `nil` in development
Module Test
module Test
thread_mattr_accessor :max_installments
self.max_installments = 12
end
Normal use
puts "max_installments is: #{Test.max_installments.inspect}"
# max_installments is: 12
In Thread use
Thread.new { puts "max_installments in thread is #{Test.max_installments.inspect}" }
# max_installments in thread is nil
Set value inside the thread
Thread.new do
Test.max_installments = 99
puts "max_installments in thread now is #{Test.max_installments.inspect}"
end
# max_installments in thread now is 99
Thread.new { puts "max_installments in another thread is #{Test.max_installments.inspect}" }
# max_installments in another thread is nil
puts "max_installments is: #{Test.max_installments}"
# max_installments is: 12
ASAP this commit must be released and we can set a default value to use in threads like this
module Test
thread_mattr_accessor :max_installments, default: 12
self.max_installments = 12
end
For now, if you go use inside threads, you need set value before to avoid get nil.
Install generator won't see a method I defined
Turns out when you try requiring mongoid_forums in pry, you'll see that an error involving decorators occurs. The issue is fixed here in my pull request to decorators: parndt/decorators#13
It's because of the way files are required and how load!
is called over there.
Waiting on PR status, that is the same version that radar/forem uses as well.
Related Topics
Rmagick Installation: Can't Find Magickwand.H
Tutorials or Screencasts on Building a Rest Web Service on Rails
Is Require File.Expand_Path(..., _File_) the Best Practice
Why Do I Need to Work Harder to Make My Rails Application Fit into a Restful Architecture
How to Access Url Helper from Rails Module
Ruby Check If Nil Before Calling Method
Set Path for Original Images Using Paperclip in Rails
How to List Included Modules in a Ruby Class
How to Step Out of a Loop with Ruby Pry
Cannot Access Local Sinatra Server from Another Computer on Same Network
Ruby Way to Generate a Hmac-Sha1 Signature for Oauth
Using Htaccess Password Protection on Rails
Check If a Hash's Keys Include All of a Set of Keys
Cannot Connect to Remote Db Using Ssh Tunnel and Activerecord