instance method thread_mattr_accessor

Ruby on Rails edge

Since v5.0.7.2

Available in: v5.0.7.2 v5.1.7 v5.2.8.1 v6.0.6.1 v6.1.7.10 v7.0.10 v7.1.6 v7.2.4 v8.0.5.1 v8.1.4 edge

Signature

thread_mattr_accessor(*syms, instance_reader: true, instance_writer: true, instance_accessor: true, default: nil)

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” “‘

Unlike mattr_accessor, values are not shared with subclasses or parent classes.

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

“‘ class Customer < Account end

Account.user # => “DHH” Customer.user # => nil Customer.user = “Rafael” Customer.user # => “Rafael” Account.user # => “DHH” “‘

To omit the instance writer method, pass ‘instance_writer: false`.

To omit 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 omit both instance methods.

“‘ class Current

thread_mattr_accessor :user, instance_accessor: false

end

Current.new.user = “DHH” # => NoMethodError Current.new.user # => NoMethodError “‘

A default value may be specified using the :default option. Because

multiple threads can access the default value, non-frozen default values will be ‘dup`ed and frozen.

Parameters

syms rest
instance_reader key = true
instance_writer key = true
instance_accessor key = true
default key = nil
Source
# File activesupport/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb, line 195
  def thread_mattr_accessor(*syms, instance_reader: true, instance_writer: true, instance_accessor: true, default: nil)
    thread_mattr_reader(*syms, instance_reader: instance_reader, instance_accessor: instance_accessor, default: default)
    thread_mattr_writer(*syms, instance_writer: instance_writer, instance_accessor: instance_accessor)
  end

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

Defined in Module

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close