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

Makes has_key? and key? behave consistently in AF::AssociationHash #932

Merged
merged 1 commit into from
Oct 28, 2015
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
5 changes: 1 addition & 4 deletions lib/active_fedora/association_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,12 @@ def key?(key)
keys.include?(key) || keys.map(&:to_s).include?(key)
end
alias_method :include?, :key?
alias_method :has_key?, :key?

def values
keys.map { |k| self[k] }
end

def has_key?(key)
keys.include?(key)
end

delegate :size, to: :keys

delegate :empty?, to: :reflections
Expand Down
78 changes: 78 additions & 0 deletions spec/unit/association_hash_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require 'spec_helper'

describe ActiveFedora::AssociationHash do
subject { described_class.new(model, reflections) }

let(:model) { double(association: nil) }
let(:reflections) { double(keys: [:foo]) }
let(:reader) { double("reader") }
let(:writer) { double("writer") }
let(:association) { double(reader: reader, writer: writer) }

describe "key reader" do
describe "when the association exists" do
before do
allow(subject).to receive(:association).with("foo") { association }
end
it "calls the association reader" do
expect(subject["foo"]).to eq(reader)
end
end
describe "when the association does not exist" do
before do
allow(subject).to receive(:association).with("foo") { nil }
end
it "returns nil" do
expect(subject["foo"]).to be_nil
end
end
end

describe "key setter" do
let(:obj) { double }
before do
allow(association).to receive(:writer).with(obj) { writer }
end
describe "when the association exists" do
before do
allow(subject).to receive(:association).with("foo") { association }
end
it "calls the association writer" do
expect(association).to receive(:writer).with(obj)
subject["foo"] = obj
end
end
describe "when the association does not exist" do
before do
allow(subject).to receive(:association).with("foo") { nil }
end
it "doesn't call the association writer" do
expect(association).not_to receive(:writer).with(obj)
subject["foo"] = obj
end
end
end

describe "#association" do
before do
allow(model).to receive(:association).with(:foo) { association }
end
it "works with a string key" do
expect(subject.association("foo")).to eq(association)
end
it "works with a symbol key" do
expect(subject.association(:foo)).to eq(association)
end
end

describe "#key?" do
it "works with a string" do
expect(subject.key?("foo")).to be true
expect(subject.key?("bar")).to be false
end
it "works with a symbol" do
expect(subject.key?(:foo)).to be true
expect(subject.key?(:bar)).to be false
end
end
end