Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Find the key using the instance

  • Loading branch information...
commit f4fe1b626e84050458a422ad58d0df7c57a79c70 1 parent 2320daf
@natedavisolds authored
View
4 lib/identitee/identifiables.rb
@@ -22,6 +22,10 @@ def find key, *default_values, &block
end
end
+ def find_key instance
+ instances.invert.fetch(instance, nil)
+ end
+
def all
instances.values
end
View
4 lib/identitee/identify.rb
@@ -18,6 +18,10 @@ def find_identifiable candidate_id
end
end
+ def find_identifiable_key instance
+ identifiables.find_key instance
+ end
+
def all_identifiables
load_all_identifiables
identifiables.all
View
7 spec/lib/identitee/identifiables_spec.rb
@@ -47,5 +47,12 @@ module Identitee
identifiables.all.should == [instance]
end
end
+
+ context "when find the key" do
+ it "gives the string identifier for the instance" do
+ identifiables.register :foo, instance
+ identifiables.find_key(instance).should eq 'foo'
+ end
+ end
end
end
View
12 spec/lib/identitee/identify_spec.rb
@@ -62,7 +62,7 @@ def set_attrib str
TestAddingFind.should respond_to :find
end
- it "doesn't symplifies find_identifiable when find exists" do
+ it "doesn't symplify find_identifiable when find exists" do
TestNotReplacingFind = Class.new do
def self.find key
key
@@ -76,4 +76,14 @@ def self.find key
TestNotReplacingFind.find("something").should == "something"
TestNotReplacingFind.find_identifiable("something").should == "Unknown"
end
+
+ it "finds the key for an identifiable" do
+ TestFindIdentiteeKey = Class.new do
+ include Identitee
+ end
+
+ instance = TestFindIdentiteeKey.identify :testing_key
+
+ TestFindIdentiteeKey.find_identifiable_key(instance).should eq 'testing_key'
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.