Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored class_attribute. #8435

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,6 @@

* Optimize log subscribers to check log level before doing any processing. *Brian Durand*

* Refactored class_attribute, also now accepts an option of persist_when_inherited: true/false. *Jason Waldrip*

Please check [3-2-stable](https://github.com/rails/rails/blob/3-2-stable/activesupport/CHANGELOG.md) for previous changes.
74 changes: 39 additions & 35 deletions activesupport/lib/active_support/core_ext/class/attribute.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'active_support/core_ext/kernel/singleton_class'
require 'active_support/core_ext/module/remove_method'
require 'active_support/core_ext/array/extract_options'
require 'active_support/core_ext/hash/keys'

class Class
# Declare a class-level attribute whose value is inheritable by subclasses.
Expand Down Expand Up @@ -67,51 +68,54 @@ class Class
# object.setting = false # => NoMethodError
#
# To opt out of both instance methods, pass <tt>instance_accessor: false</tt>.
#
# To opt out of values being passed on inheritance, pass <tt>persist_when_inherited: false</tt>.

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)
attrs.each { |attr| define_class_attribute(attr, options) }
end

attrs.each do |name|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def self.#{name}() nil end
def self.#{name}?() !!#{name} end
def define_class_attribute(attr, options={})
options.assert_valid_keys :instance_accessor, :instance_writer, :instance_reader, :persist_when_inherited
instance_reader = options.fetch(:instance_accessor, true) && options.fetch(:instance_reader, true)
instance_writer = options.fetch(:instance_accessor, true) && options.fetch(:instance_writer, true)
persist_when_inherited = options.fetch(:persist_when_inherited, true)

def self.#{name}=(val)
singleton_class.class_eval do
remove_possible_method(:#{name})
define_method(:#{name}) { val }
end
define_singleton_method "#{attr}" do
if instance_variable_defined?(:"@#{attr}")
instance_variable_get(:"@#{attr}")
elsif persist_when_inherited && superclass.respond_to?("#{attr}")
superclass.send "#{attr}"
else
nil
end
end

if singleton_class?
class_eval do
remove_possible_method(:#{name})
def #{name}
defined?(@#{name}) ? @#{name} : singleton_class.#{name}
end
end
end
val
end
define_singleton_method "#{attr}?" do
!!send(attr)
end

if instance_reader
remove_possible_method :#{name}
def #{name}
defined?(@#{name}) ? @#{name} : self.class.#{name}
end
define_singleton_method "#{attr}=" do |value|
instance_variable_set(:"@#{attr}", value)
end

def #{name}?
!!#{name}
end
if instance_reader
define_method "#{attr}" do
if instance_variable_defined?(:"@#{attr}")
instance_variable_get(:"@#{attr}")
else
self.singleton_class.send attr
end
RUBY
end

attr_writer name if instance_writer
define_method "#{attr}?" do
!!send(attr)
end
end

attr_writer attr if instance_writer

end

private
def singleton_class?
ancestors.first != self
end
end
7 changes: 7 additions & 0 deletions activesupport/test/core_ext/class/attribute_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ def setup
assert_raise(NoMethodError) { object.setting = 'boom' }
end

test 'disabling inherited persistence' do
base_class = Class.new { class_attribute :setting, :persist_when_inherited => false ; self.setting = "value" }
inherited_class = Class.new(base_class)
assert_equal "value", base_class.setting
assert_equal nil, inherited_class.setting
end

test 'works well with singleton classes' do
object = @klass.new
object.singleton_class.setting = 'foo'
Expand Down