Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 286 Vote(s) - 3.39 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is mattr_accessor in a Rails module?

#1
I couldn't really find this in Rails documentation but it seems like **'mattr_accessor'** is the **Module** corollary for **'attr_accessor'** (getter & setter) in a normal Ruby **class**.

Eg. in a class

class User
attr_accessor :name

def set_fullname
@name = "#{self.first_name} #{self.last_name}"
end
end

Eg. in a module

module Authentication
mattr_accessor :current_user

def login
@current_user = session[:user_id] || nil
end
end

This helper method is provided by **ActiveSupport**.

Reply

#2
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 = "[email protected]"
>> Config.hostname # => "example.com"
>> Config.admin_email # => "[email protected]"
Reply

#3
[Here's the source for `cattr_accessor`](

[To see links please register here]

)

And

[Here's the source for `mattr_accessor`](

[To see links please register here]

)

As you can see, they're pretty much identical.

As to why there are two different versions? Sometimes you want to write `cattr_accessor` in a module, so you can use it for configuration info [like Avdi mentions](

[To see links please register here]

).
However, `cattr_accessor` doesn't work in a module, so they more or less copied the code over to work for modules also.

Additionally, sometimes you might want to write a class method in a module, such that whenever any class includes the module, it gets that class method as well as all the instance methods. `mattr_accessor` also lets you do this.

However, in the second scenario, it's behaviour is pretty strange. Observe the following code, particularly note the `@@mattr_in_module` bits

module MyModule
mattr_accessor :mattr_in_module
end

class MyClass
include MyModule
def self.get_mattr; @@mattr_in_module; end # directly access the class variable
end

MyModule.mattr_in_module = 'foo' # set it on the module
=> "foo"

MyClass.get_mattr # get it out of the class
=> "foo"

class SecondClass
include MyModule
def self.get_mattr; @@mattr_in_module; end # again directly access the class variable in a different class
end

SecondClass.get_mattr # get it out of the OTHER class
=> "foo"

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through