aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
authorGuilherme Mansur <guilherme.mansur@shopify.com>2019-05-21 14:45:04 -0400
committerKasper Timm Hansen <kaspth@gmail.com>2019-08-05 02:31:36 +0200
commit2bd2d853da78c248b1d50add92ce21f43430abe9 (patch)
treedea198daa021157fb4cdd72b4ab1a09f95277366 /activesupport/lib
parentcebbf6c6cfc88ed2bd64853bad8d0c9adf420fd9 (diff)
downloadrails-2bd2d853da78c248b1d50add92ce21f43430abe9.tar.gz
rails-2bd2d853da78c248b1d50add92ce21f43430abe9.tar.bz2
rails-2bd2d853da78c248b1d50add92ce21f43430abe9.zip
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`.
Diffstat (limited to 'activesupport/lib')
-rw-r--r--activesupport/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb16
1 files changed, 11 insertions, 5 deletions
diff --git a/activesupport/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb b/activesupport/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb
index a6e87aeb68..452a456a52 100644
--- a/activesupport/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb
+++ b/activesupport/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb
@@ -33,7 +33,7 @@ class Module
# end
#
# 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|
raise NameError.new("invalid attribute name: #{sym}") unless /^[_A-Za-z]\w*$/.match?(sym)
@@ -52,6 +52,9 @@ class Module
end
EOS
end
+
+ default_val = (block_given? && default.nil?) ? yield : default
+ Thread.current["attr_" + name + "_#{sym}"] = default_val unless default_val.nil?
end
end
alias :thread_cattr_reader :thread_mattr_reader
@@ -74,7 +77,7 @@ class Module
# end
#
# 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|
raise NameError.new("invalid attribute name: #{sym}") unless /^[_A-Za-z]\w*$/.match?(sym)
@@ -93,6 +96,9 @@ class Module
end
EOS
end
+
+ default_val = (block_given? && default.nil?) ? yield : default
+ Thread.current["attr_" + name + "_#{sym}"] = default_val unless default_val.nil?
end
end
alias :thread_cattr_writer :thread_mattr_writer
@@ -136,9 +142,9 @@ class Module
#
# Current.new.user = "DHH" # => NoMethodError
# Current.new.user # => NoMethodError
- def thread_mattr_accessor(*syms, instance_reader: true, instance_writer: true, instance_accessor: true)
- thread_mattr_reader(*syms, instance_reader: instance_reader, instance_accessor: instance_accessor)
- thread_mattr_writer(*syms, instance_writer: instance_writer, instance_accessor: instance_accessor)
+ 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, default: default, &blk)
+ thread_mattr_writer(*syms, instance_writer: instance_writer, instance_accessor: instance_accessor, default: default)
end
alias :thread_cattr_accessor :thread_mattr_accessor
end