Skip to content

Commit

Permalink
Add query methods for superclass_delegating_reader
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9156 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed Mar 31, 2008
1 parent 181378d commit 2cf72ad
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Expand Up @@ -19,6 +19,12 @@ def self.#{name}
def #{name} def #{name}
self.class.#{name} self.class.#{name}
end end
def self.#{name}?
!!#{name}
end
def #{name}?
!!#{name}
end
EOS EOS
end end
end end
Expand All @@ -37,4 +43,4 @@ def superclass_delegating_accessor(*names)
superclass_delegating_reader(*names) superclass_delegating_reader(*names)
superclass_delegating_writer(*names) superclass_delegating_writer(*names)
end end
end end
18 changes: 14 additions & 4 deletions activesupport/test/core_ext/class/delegating_attributes_test.rb
Expand Up @@ -25,7 +25,9 @@ def test_simple_reader_declaration
# The class and instance should have an accessor, but there # The class and instance should have an accessor, but there
# should be no mutator # should be no mutator
assert single_class.respond_to?(:only_reader) assert single_class.respond_to?(:only_reader)
assert single_class.respond_to?(:only_reader?)
assert single_class.public_instance_methods.map(&:to_s).include?("only_reader") assert single_class.public_instance_methods.map(&:to_s).include?("only_reader")
assert single_class.public_instance_methods.map(&:to_s).include?("only_reader?")
assert !single_class.respond_to?(:only_reader=) assert !single_class.respond_to?(:only_reader=)
end end


Expand All @@ -51,9 +53,17 @@ def test_simple_accessor_declaration


def test_working_with_simple_attributes def test_working_with_simple_attributes
single_class.superclass_delegating_accessor :both single_class.superclass_delegating_accessor :both
single_class.both= "HMMM"
single_class.both = "HMMM"

assert_equal "HMMM", single_class.both assert_equal "HMMM", single_class.both
assert_equal true, single_class.both?

assert_equal "HMMM", single_class.new.both assert_equal "HMMM", single_class.new.both
assert_equal true, single_class.new.both?

single_class.both = false
assert_equal false, single_class.both?
end end


def test_working_with_accessors def test_working_with_accessors
Expand All @@ -73,14 +83,14 @@ def test_child_class_delegates_to_parent_but_can_be_overridden
parent = Class.new parent = Class.new
parent.superclass_delegating_accessor :both parent.superclass_delegating_accessor :both
child = Class.new(parent) child = Class.new(parent)
parent.both= "1" parent.both = "1"
assert_equal "1", child.both assert_equal "1", child.both


child.both="2" child.both = "2"
assert_equal "1", parent.both assert_equal "1", parent.both
assert_equal "2", child.both assert_equal "2", child.both


parent.both="3" parent.both = "3"
assert_equal "3", parent.both assert_equal "3", parent.both
assert_equal "2", child.both assert_equal "2", child.both
end end
Expand Down

0 comments on commit 2cf72ad

Please sign in to comment.