instance method thread_mattr_accessor

Ruby on Rails 5.2.8.1

Since v5.2.8.1

Available in: v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

thread_mattr_accessor(*syms)

Defines both class and instance accessors for class attributes.

class Account
  thread_mattr_accessor :user
end

Account.user = "DHH"
Account.user     # => "DHH"
Account.new.user # => "DHH"

If a subclass changes the value, the parent class’ value is not changed. Similarly, if the parent class changes the value, the value of subclasses is not changed.

class Customer < Account
end

Customer.user = "Rafael"
Customer.user # => "Rafael"
Account.user  # => "DHH"

To opt out of the instance writer method, pass instance_writer: false. To opt out of the instance reader method, pass instance_reader: false.

class Current
  thread_mattr_accessor :user, instance_writer: false, instance_reader: false
end

Current.new.user = "DHH"  # => NoMethodError
Current.new.user          # => NoMethodError

Or pass instance_accessor: false, to opt out both instance methods.

class Current
  mattr_accessor :user, instance_accessor: false
end

Current.new.user = "DHH"  # => NoMethodError
Current.new.user          # => NoMethodError

Parameters

syms rest
Source
# File activesupport/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb, line 145
  def thread_mattr_accessor(*syms)
    thread_mattr_reader(*syms)
    thread_mattr_writer(*syms)
  end

Defined in activesupport/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb line 145 · View on GitHub · Improve this page · Find usages on GitHub

Defined in Module

Type at least 2 characters to search.

↑↓ navigate · open · esc close