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

Mutable drops should fallback to their own methods when a mutation isn't present #112

Merged
merged 3 commits into from
Sep 7, 2017
Merged
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
8 changes: 3 additions & 5 deletions lib/jekyll-github-metadata/metadata_drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ class MetadataDrop < Jekyll::Drops::Drop
# See https://github.com/jekyll/jekyll/pull/6338
alias_method :invoke_drop, :[]
def key?(key)
if self.class.mutable?
@mutations.key?(key)
else
!key.nil? && (respond_to?(key) || fallback_data.key?(key))
end
return false if key.nil?
return true if self.class.mutable? && @mutations.key?(key)
respond_to?(key) || fallback_data.key?(key)
end

def to_s
Expand Down
49 changes: 49 additions & 0 deletions spec/metadata_drop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,53 @@
expect(payload.keys).to match_array(expected_values.keys)
end
end

context "returning values" do
context "native methods" do
it "returns a value via #[]" do
expect(subject["url"]).to eql("http://jekyll.github.io/github-metadata")
end

it "returns a value via #invoke_drop" do
expect(subject.invoke_drop("url")).to eql("http://jekyll.github.io/github-metadata")
end

it "responds to #key?" do
expect(subject.key?("url")).to be_truthy
end
end

context "with mutated values" do
before { subject["url"] = "foo" }

it "returns the mutated value via #[]" do
expect(subject["url"]).to eql("foo")
end

it "returns the mutated via #invoke_drop" do
expect(subject.invoke_drop("url")).to eql("foo")
end

it "responds to #key?" do
expect(subject.key?("url")).to be_truthy
end
end

context "with fallback data" do
let(:fallback_data) { { "foo" => "bar" } }
before { subject.instance_variable_set("@fallback_data", fallback_data) }

it "returns the mutated value via #[]" do
expect(subject["foo"]).to eql("bar")
end

it "returns the mutated via #invoke_drop" do
expect(subject.invoke_drop("foo")).to eql("bar")
end

it "responds to #key?" do
expect(subject.key?("foo")).to be_truthy
end
end
end
end