mad_id 0.3.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 640ca4a57c32063613b126cac922b1d6d88f1c17
4
- data.tar.gz: a1f46a5dc25dbba7793fe31ea1616225aa46ac9e
3
+ metadata.gz: 8a27c381fd81182c7d63f410bafe22881658ef18
4
+ data.tar.gz: ee2e7893a7c1d0b4a93a090af0564a9c59ef265d
5
5
  SHA512:
6
- metadata.gz: b2f080018fe1954dcdd9c19ac7321693f35ae3fe5b1042c49c498f56f3a5b7df41d020751fbf6fee5a1589c31e6823303260376e78f4cbfedea52d5a501ec152
7
- data.tar.gz: e77b9a62c91a27408593ac116be6b7b30e7f445e56220c91f672b313b34e839a5c728dd53ae86896bc33e929ff049b2acbe3599e154c1ce669fd634c4873f2e1
6
+ metadata.gz: 7c50472189f0a2f807b365b3c1be2fc44bfad11776a376a60f20d42c9ccd27b4e255dd50aedde2b17c232b0a3c19d3f28d1ae1635bc94b774bfa9925533b0028
7
+ data.tar.gz: eac2d8d39d5aab593d45150ad8d85ee8970d7d626948ec7fae7403c5a244d6cefd13fcc54580c0f186ea9ac3baa8d9d0530cd17a3bcc137ac91361b7ffc6d51a
data/README.md CHANGED
@@ -53,6 +53,11 @@ You can access all registered identifiers and the associated class via MadID's r
53
53
  MadID.registry
54
54
  # => { 'foo' => YourModel }
55
55
 
56
+ ### Locator
57
+
58
+ MadID.locate("pny-1312313412")
59
+ # => #Object:Pony:1312313412
60
+
56
61
  ## Contributing
57
62
 
58
63
  1. Fork it ( http://github.com/<my-github-username>/mad_id/fork )
data/lib/mad_id.rb CHANGED
@@ -15,8 +15,17 @@ module MadID
15
15
  attr_accessor :registry
16
16
 
17
17
  def locate(id)
18
- prefix, _ = id.split('-', 2)
19
- registry[prefix].find_by_mad_id!(id)
18
+ prefix, _ = id.to_s.split('-', 2)
19
+ if klass = registry[prefix]
20
+ klass.find_by_mad_id(id)
21
+ else
22
+ nil
23
+ end
24
+ end
25
+
26
+ def locate!(id)
27
+ prefix, _ = id.to_s.split('-', 2)
28
+ registry.fetch(prefix).find_by_mad_id!(id)
20
29
  end
21
30
  end
22
31
 
@@ -3,5 +3,8 @@ module MadID::FinderMethods
3
3
  def find_by_mad_id!(id)
4
4
  find_by!(identifier: id.to_s.downcase)
5
5
  end
6
+ def find_by_mad_id(id)
7
+ find_by(identifier: id.to_s.downcase)
8
+ end
6
9
 
7
10
  end
@@ -1,3 +1,3 @@
1
1
  module MadID
2
- VERSION = "0.3.0"
2
+ VERSION = "1.0.0"
3
3
  end
data/spec/mad_id_spec.rb CHANGED
@@ -81,14 +81,31 @@ describe MadID do
81
81
  let!(:little_pony) { LittlePony.create }
82
82
  let!(:great_pony) { GreatPony.create }
83
83
 
84
- it 'locate the little pony just by the identifier' do
85
- expect(MadID.locate(little_pony.identifier)).to eq(little_pony)
86
- end
84
+ describe 'locate' do
85
+ it 'locate the little pony just by the identifier' do
86
+ expect(MadID.locate(little_pony.identifier)).to eq(little_pony)
87
+ end
88
+
89
+ it 'locate the great pony just by the identifier' do
90
+ expect(MadID.locate(great_pony.identifier)).to eq(great_pony)
91
+ end
92
+
93
+ it 'returns nil if no klass is found for the identifier' do
94
+ expect(MadID.locate("noo-getting-cold")).to be_nil
95
+ end
87
96
 
88
- it 'locate the great pony just by the identifier' do
89
- binding
90
- expect(MadID.locate(great_pony.identifier)).to eq(great_pony)
97
+ it 'returns nil if nothing is found' do
98
+ expect(MadID.locate("pny-no-pony-there")).to be_nil
99
+ end
91
100
  end
101
+ describe 'locate!' do
102
+ it 'raises an error if no class is found' do
103
+ expect { MadID.locate!("noo-class-there") }.to raise_error(KeyError)
104
+ end
92
105
 
106
+ it 'raises an error if no record is found' do
107
+ expect { MadID.locate!("pny-class-there") }.to raise_error(ActiveRecord::RecordNotFound)
108
+ end
109
+ end
93
110
  end
94
111
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mad_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lars Brillert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-16 00:00:00.000000000 Z
11
+ date: 2014-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord