Skip to content

Commit bdb7f20

Browse files
authored
Merge pull request #450 from seanpdoyle/method-missing-regex
Base: read from `known_attributes` in `respond_to_missing?`
2 parents 7b4b2b0 + ebcc2a5 commit bdb7f20

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

lib/active_resource/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,7 @@ def respond_to_missing?(method, include_priv = false)
16681668
super
16691669
elsif known_attributes.include?(method_name)
16701670
true
1671-
elsif method_name =~ /(?:=|\?)$/ && attributes.include?($`)
1671+
elsif method_name =~ /(?:=|\?)$/ && known_attributes.include?($`)
16721672
true
16731673
else
16741674
# super must be called at the end of the method, because the inherited respond_to?

test/cases/base_test.rb

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,52 @@ def test_respond_to
960960
assert_respond_to matz, :name
961961
assert_respond_to matz, :name=
962962
assert_respond_to matz, :name?
963-
assert_not matz.respond_to?(:super_scalable_stuff)
963+
assert_not_respond_to matz, :super_scalable_stuff
964+
assert_not_respond_to matz, :super_scalable_stuff=
965+
end
966+
967+
def test_respond_to_known_attributes
968+
previous_schema = Person.schema
969+
Person.schema = { name: "string" }
970+
971+
person = Person.new
972+
973+
assert_respond_to person, :name
974+
assert_respond_to person, :name=
975+
assert_not_respond_to person, :super_scalable_stuff
976+
assert_not_respond_to person, :super_scalable_stuff=
977+
ensure
978+
Person.schema = previous_schema
979+
end
980+
981+
def test_reading_an_unknown_attribute_raises_NoMethodError
982+
assert_raises NoMethodError, match: "unknown_attribute" do
983+
Post.new.unknown_attribute
984+
end
985+
end
986+
987+
def test_writing_an_unknown_attribute_assigns_a_value_that_can_be_read
988+
post = Post.new
989+
990+
post.unknown_attribute = "assigned"
991+
992+
assert_respond_to post, :unknown_attribute
993+
assert_equal "assigned", post.unknown_attribute
994+
end
995+
996+
def test_writing_nil_to_an_existing_attribute_can_be_read
997+
post = Post.new unknown_attribute: "assigned"
998+
999+
post.unknown_attribute = nil
1000+
1001+
assert_nil post.unknown_attribute
1002+
assert_respond_to post, :unknown_attribute
1003+
end
1004+
1005+
def test_predicate_for_an_unknown_attribute_returns_nil
1006+
post = Post.new
1007+
1008+
assert_not_predicate post, :unknown_attribute?
9641009
end
9651010

9661011
def test_custom_header

0 commit comments

Comments
 (0)