diff options
author | Dylan Thacker-Smith <dylan.smith@shopify.com> | 2019-03-02 22:21:46 -0500 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2019-03-03 12:21:46 +0900 |
commit | dfa439eefc7bfaa2f00bc8d2bd712d115dd0ef33 (patch) | |
tree | 0fe7b185cdd4a32610aacb22a4fb6bb7901fc28b /activesupport | |
parent | f4a30d2a0706f278a20c63a3d99288de79b52e5f (diff) | |
download | rails-dfa439eefc7bfaa2f00bc8d2bd712d115dd0ef33.tar.gz rails-dfa439eefc7bfaa2f00bc8d2bd712d115dd0ef33.tar.bz2 rails-dfa439eefc7bfaa2f00bc8d2bd712d115dd0ef33.zip |
activesupport: Simplify class_attribute implementation (#35454)
* activesupport(class_attribute): Use redefine_singleton_method
* activesupport(class_attribute): Use keyword arguments
* activesupport(class_attribute): Avoid unnecessary redefinition for default
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/class/attribute.rb | 25 |
1 files changed, 10 insertions, 15 deletions
diff --git a/activesupport/lib/active_support/core_ext/class/attribute.rb b/activesupport/lib/active_support/core_ext/class/attribute.rb index fa33ff945f..255cbee55c 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute.rb @@ -84,16 +84,17 @@ class Class # To set a default value for the attribute, pass <tt>default:</tt>, like so: # # class_attribute :settings, default: {} - def class_attribute(*attrs) - options = attrs.extract_options! - instance_reader = options.fetch(:instance_accessor, true) && options.fetch(:instance_reader, true) - instance_writer = options.fetch(:instance_accessor, true) && options.fetch(:instance_writer, true) - instance_predicate = options.fetch(:instance_predicate, true) - default_value = options.fetch(:default, nil) - + def class_attribute( + *attrs, + instance_accessor: true, + instance_reader: instance_accessor, + instance_writer: instance_accessor, + instance_predicate: true, + default: nil + ) attrs.each do |name| singleton_class.silence_redefinition_of_method(name) - define_singleton_method(name) { nil } + define_singleton_method(name) { default } singleton_class.silence_redefinition_of_method("#{name}?") define_singleton_method("#{name}?") { !!public_send(name) } if instance_predicate @@ -102,9 +103,7 @@ class Class singleton_class.silence_redefinition_of_method("#{name}=") define_singleton_method("#{name}=") do |val| - singleton_class.class_eval do - redefine_method(name) { val } - end + redefine_singleton_method(name) { val } if singleton_class? class_eval do @@ -137,10 +136,6 @@ class Class instance_variable_set ivar, val end end - - unless default_value.nil? - self.send("#{name}=", default_value) - end end end end |