mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Able to initalize default value for thread_mattr_*
Added the ability to initialize `thread_mattr_*` methods with default values like so: ``` ruby class MyClass thread_attr_reader :foo, default: :foo thread_attr_writer :bar, default: :bar thread_attr_accessor: baz do "baz" end end ``` This is consistent with the api exposed by `mattr_accessor`.
This commit is contained in:
parent
cebbf6c6cf
commit
2bd2d853da
3 changed files with 48 additions and 18 deletions
|
@ -1,3 +1,11 @@
|
||||||
|
* Allow initializing `thread_mattr_*` attributes via `:default` option
|
||||||
|
|
||||||
|
class Scraper
|
||||||
|
thread_mattr_reader :client, default: Api::Client.new
|
||||||
|
end
|
||||||
|
|
||||||
|
*Guilherme Mansur*
|
||||||
|
|
||||||
* Add `compact_blank` for those times when you want to remove #blank? values from
|
* Add `compact_blank` for those times when you want to remove #blank? values from
|
||||||
an Enumerable (also `compact_blank!` on Hash, Array, ActionController::Parameters)
|
an Enumerable (also `compact_blank!` on Hash, Array, ActionController::Parameters)
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ class Module
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# Current.new.user # => NoMethodError
|
# Current.new.user # => NoMethodError
|
||||||
def thread_mattr_reader(*syms, instance_reader: true, instance_accessor: true) # :nodoc:
|
def thread_mattr_reader(*syms, instance_reader: true, instance_accessor: true, default: nil) # :nodoc:
|
||||||
syms.each do |sym|
|
syms.each do |sym|
|
||||||
raise NameError.new("invalid attribute name: #{sym}") unless /^[_A-Za-z]\w*$/.match?(sym)
|
raise NameError.new("invalid attribute name: #{sym}") unless /^[_A-Za-z]\w*$/.match?(sym)
|
||||||
|
|
||||||
|
@ -52,6 +52,9 @@ class Module
|
||||||
end
|
end
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
|
|
||||||
|
default_val = (block_given? && default.nil?) ? yield : default
|
||||||
|
Thread.current["attr_" + name + "_#{sym}"] = default_val unless default_val.nil?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
alias :thread_cattr_reader :thread_mattr_reader
|
alias :thread_cattr_reader :thread_mattr_reader
|
||||||
|
@ -74,7 +77,7 @@ class Module
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# Current.new.user = "DHH" # => NoMethodError
|
# Current.new.user = "DHH" # => NoMethodError
|
||||||
def thread_mattr_writer(*syms, instance_writer: true, instance_accessor: true) # :nodoc:
|
def thread_mattr_writer(*syms, instance_writer: true, instance_accessor: true, default: nil) # :nodoc:
|
||||||
syms.each do |sym|
|
syms.each do |sym|
|
||||||
raise NameError.new("invalid attribute name: #{sym}") unless /^[_A-Za-z]\w*$/.match?(sym)
|
raise NameError.new("invalid attribute name: #{sym}") unless /^[_A-Za-z]\w*$/.match?(sym)
|
||||||
|
|
||||||
|
@ -93,6 +96,9 @@ class Module
|
||||||
end
|
end
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
|
|
||||||
|
default_val = (block_given? && default.nil?) ? yield : default
|
||||||
|
Thread.current["attr_" + name + "_#{sym}"] = default_val unless default_val.nil?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
alias :thread_cattr_writer :thread_mattr_writer
|
alias :thread_cattr_writer :thread_mattr_writer
|
||||||
|
@ -136,9 +142,9 @@ class Module
|
||||||
#
|
#
|
||||||
# Current.new.user = "DHH" # => NoMethodError
|
# Current.new.user = "DHH" # => NoMethodError
|
||||||
# Current.new.user # => NoMethodError
|
# Current.new.user # => NoMethodError
|
||||||
def thread_mattr_accessor(*syms, instance_reader: true, instance_writer: true, instance_accessor: true)
|
def thread_mattr_accessor(*syms, instance_reader: true, instance_writer: true, instance_accessor: true, default: nil, &blk)
|
||||||
thread_mattr_reader(*syms, instance_reader: instance_reader, instance_accessor: instance_accessor)
|
thread_mattr_reader(*syms, instance_reader: instance_reader, instance_accessor: instance_accessor, default: default, &blk)
|
||||||
thread_mattr_writer(*syms, instance_writer: instance_writer, instance_accessor: instance_accessor)
|
thread_mattr_writer(*syms, instance_writer: instance_writer, instance_accessor: instance_accessor, default: default)
|
||||||
end
|
end
|
||||||
alias :thread_cattr_accessor :thread_mattr_accessor
|
alias :thread_cattr_accessor :thread_mattr_accessor
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,23 +4,39 @@ require "abstract_unit"
|
||||||
require "active_support/core_ext/module/attribute_accessors_per_thread"
|
require "active_support/core_ext/module/attribute_accessors_per_thread"
|
||||||
|
|
||||||
class ModuleAttributeAccessorPerThreadTest < ActiveSupport::TestCase
|
class ModuleAttributeAccessorPerThreadTest < ActiveSupport::TestCase
|
||||||
def setup
|
class MyClass
|
||||||
@class = Class.new do
|
|
||||||
thread_mattr_accessor :foo
|
thread_mattr_accessor :foo
|
||||||
thread_mattr_accessor :bar, instance_writer: false
|
thread_mattr_accessor :bar, instance_writer: false
|
||||||
thread_mattr_reader :shaq, instance_reader: false
|
thread_mattr_reader :shaq, instance_reader: false
|
||||||
thread_mattr_accessor :camp, instance_accessor: false
|
thread_mattr_accessor :camp, instance_accessor: false
|
||||||
|
|
||||||
def self.name; "MyClass" end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@subclass = Class.new(@class) do
|
class SubMyClass < MyClass
|
||||||
def self.name; "SubMyClass" end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def setup
|
||||||
|
@class = MyClass
|
||||||
|
@subclass = SubMyClass
|
||||||
@object = @class.new
|
@object = @class.new
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_can_initialize_with_default_value
|
||||||
|
Thread.new do
|
||||||
|
@class.thread_mattr_accessor :baz, default: "default_value"
|
||||||
|
assert_equal "default_value", @class.baz
|
||||||
|
end.join
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_can_initialize_with_a_block_as_default_value
|
||||||
|
Thread.new do
|
||||||
|
@class.thread_mattr_accessor :baz do
|
||||||
|
"default_value"
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal "default_value", @class.baz
|
||||||
|
end.join
|
||||||
|
end
|
||||||
|
|
||||||
def test_should_use_mattr_default
|
def test_should_use_mattr_default
|
||||||
Thread.new do
|
Thread.new do
|
||||||
assert_nil @class.foo
|
assert_nil @class.foo
|
||||||
|
|
Loading…
Reference in a new issue