class Module
def thread_mattr_accessor(*syms, instance_reader: true, instance_writer: true, instance_accessor: true, default: nil)
Current.new.user = "DHH" # => NoMethodError
end
thread_mattr_accessor :user, instance_accessor: false
class Current
Or pass instance_accessor: false, to omit both instance methods.
Current.new.user # => NoMethodError
Current.new.user = "DHH" # => NoMethodError
end
thread_mattr_accessor :user, instance_writer: false, instance_reader: false
class Current
To omit the instance reader method, pass instance_reader: false.
To omit the instance writer method, pass instance_writer: false.
Account.user # => "DHH"
Customer.user # => "Rafael"
Customer.user = "Rafael"
Customer.user # => nil
Account.user # => "DHH"
end
class Customer < Account
If the parent class changes the value, the value of subclasses is not changed.
If a subclass changes the value, the parent class' value is not changed.
Unlike +mattr_accessor+, values are *not* shared with subclasses or parent classes.
Account.new.user # => "DHH"
Account.user # => "DHH"
Account.user = "DHH"
end
thread_mattr_accessor :user
class Account
Defines both class and instance accessors for class attributes.
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