8000 Merge pull request #112 from jekyll/fallback-to-mutable-methods · inntran/github-metadata@f18d6dd · GitHub
[go: up one dir, main page]

Skip to content

Commit f18d6dd

Browse files
authored
Merge pull request jekyll#112 from jekyll/fallback-to-mutable-methods
Mutable drops should fallback to their own methods when a mutation isn't present
2 parents a88c865 + 4b46ef8 commit f18d6dd

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

lib/jekyll-github-metadata/metadata_drop.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ class MetadataDrop < Jekyll::Drops::Drop
1111
# See https://github.com/jekyll/jekyll/pull/6338
1212
alias_method :invoke_drop, :[]
1313
def key?(key)
14-
if self.class.mutable?
15-
@mutations.key?(key)
16-
else
17-
!key.nil? && (respond_to?(key) || fallback_data.key?(key))
18-
end
14+
return false if key.nil?
15+
return true if self.class.mutable? && @mutations.key?(key)
16+
respond_to?(key) || fallback_data.key?(key)
1917
end
2018

2119
def to_s

spec/metadata_drop_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,53 @@
5050
expect(payload.keys).to match_array(expected_values.keys)
5151
end
5252
end
53+
54+
context "returning values" do
55+
context "native methods" do
56+
it "returns a value via #[]" do
57+
expect(subject["url"]).to eql("http://jekyll.github.io/github-metadata")
58+
end
59+
60+
it "returns a value via #invoke_drop" do
61+
expect(subject.invoke_drop("url")).to eql("http://jekyll.github.io/github-metadata")
62+
end
63+
64+
it "responds to #key?" do
65+
expect(subject.key?("url")).to be_truthy
66+
end
67+
end
68+
69+
context "with mutated values" do
70+
before { subject["url"] = "foo" }
71+
72+
it "returns the mutated value via #[]" do
73+
expect(subject["url"]).to eql("foo")
74+
end
75+
76+
it "returns the mutated via #invoke_drop" do
77+
expect(subject.invoke_drop("url")).to eql("foo")
78+
end
79+
80+
it "responds to #key?" do
81+
expect(subject.key?("url")).to be_truthy
82+
end
83+
end
84+
85+
context "with fallback data" do
86+
let(:fallback_data) { { "foo" => "bar" } }
87+
before { subject.instance_variable_set("@fallback_data", fallback_data) }
88+
89+
it "returns the mutated value via #[]" do
90+
expect(subject["foo"]).to eql("bar")
91+
end
92+
93+
it "returns the mutated via #invoke_drop" do
94+
expect(subject.invoke_drop("foo")).to eql("bar")
95+
end
96+
97+
it "responds to #key?" do
98+
expect(subject.key?("foo")).to be_truthy
99+
end
100+
end
101+
end
53102
end

0 commit comments

Comments
 (0)
0