diff options
author | Xavier Noria <fxn@hashref.com> | 2016-08-08 15:45:30 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2016-08-08 16:01:37 +0200 |
commit | 996a27ec3adeee5bcda49dcf4ceb168da981080e (patch) | |
tree | a8cd62d5de3751748616a6ef180a9950e5797ecf /activesupport/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb | |
parent | e9852c92bfa5815c02a33b52f69f12b99578fe29 (diff) | |
download | rails-996a27ec3adeee5bcda49dcf4ceb168da981080e.tar.gz rails-996a27ec3adeee5bcda49dcf4ceb168da981080e.tar.bz2 rails-996a27ec3adeee5bcda49dcf4ceb168da981080e.zip |
let instance thread_mattr_* methods delegate to the class-level ones
This code has too much duplication and the rationale for the concatenation
may not be obvious to the reader. You define the ones at class-level, explain
why does the code concatenates there, and then the convenience ones at
instance-level just delegate.
Diffstat (limited to 'activesupport/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb')
-rw-r--r-- | activesupport/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb | 14 |
1 files changed, 10 insertions, 4 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 a40f0df9e2..4c16b26138 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 @@ -39,16 +39,19 @@ class Module syms.each do |sym| raise NameError.new("invalid attribute name: #{sym}") unless /^[_A-Za-z]\w*$/.match?(sym) + + # The following generated method concatenates `name` because we want it + # to work with inheritance via polymorphism. class_eval(<<-EOS, __FILE__, __LINE__ + 1) def self.#{sym} - Thread.current["attr_"+ name + "_#{sym}"] + Thread.current["attr_" + name + "_#{sym}"] end EOS unless options[:instance_reader] == false || options[:instance_accessor] == false class_eval(<<-EOS, __FILE__, __LINE__ + 1) def #{sym} - Thread.current["attr_"+ self.class.name + "_#{sym}"] + self.class.#{sym} end EOS end @@ -78,16 +81,19 @@ class Module options = syms.extract_options! syms.each do |sym| raise NameError.new("invalid attribute name: #{sym}") unless /^[_A-Za-z]\w*$/.match?(sym) + + # The following generated method concatenates `name` because we want it + # to work with inheritance via polymorphism. class_eval(<<-EOS, __FILE__, __LINE__ + 1) def self.#{sym}=(obj) - Thread.current["attr_"+ name + "_#{sym}"] = obj + Thread.current["attr_" + name + "_#{sym}"] = obj end EOS unless options[:instance_writer] == false || options[:instance_accessor] == false class_eval(<<-EOS, __FILE__, __LINE__ + 1) def #{sym}=(obj) - Thread.current["attr_"+ self.class.name + "_#{sym}"] = obj + self.class.#{sym} = obj end EOS end |