ensurance 0.1.8 → 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -1
- data/lib/ensurance.rb +17 -12
- data/lib/ensurance/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d6b9215002c70ca7691296be27ad819cee6da1793db1fe8042296a9cce75aa51
|
4
|
+
data.tar.gz: 43558c3ab86ee99eb6d3d427bebe4458e865b29e77b9d2c0e2feb41d308b3a88
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a8eb8c8fe99768cbd5f9739d9e56f777f62d151af12a50dbf74e3888f0398a73f4af55b79308375f6b480f5d3775936c1f47751b6ad4150e0e8c00982cd31234
|
7
|
+
data.tar.gz: 942f536bcfb7e3a563732616dee9dd48091ef4f3ffac951ea87688658d1d4090cb27fbd79d7db80ab1a1fe912eaca6c5e078f48bccadb9973a58227f0f003d97
|
data/README.md
CHANGED
@@ -88,7 +88,8 @@ User.ensure(<globalid>) == GlobalID::Locator.locate(<globalid>)
|
|
88
88
|
User.ensure(<globalid string>) == GlobalID::Locator.locate(<globalid string>)
|
89
89
|
User.ensure(<some token>) == User.where(token: <some token>).first
|
90
90
|
User.ensure(nil) -> nil
|
91
|
-
User.
|
91
|
+
User.ensure!(nil) -> nil
|
92
|
+
User.ensure!(<unknown_id>) -> ActiveRecord::RecordNotFound
|
92
93
|
```
|
93
94
|
|
94
95
|
## Contributing
|
data/lib/ensurance.rb
CHANGED
@@ -16,37 +16,42 @@ module Ensurance
|
|
16
16
|
|
17
17
|
def ensure(thing = nil)
|
18
18
|
return nil unless thing.present?
|
19
|
-
found = nil
|
20
19
|
|
21
20
|
if thing.is_a?(self)
|
22
21
|
return thing
|
23
|
-
elsif thing.is_a?(Array)
|
24
|
-
raise ArgumentError.new("Cannot Ensure #{self.name} with an Array: #{thing.inspect}")
|
25
22
|
elsif thing.is_a?(GlobalID)
|
26
23
|
return GlobalID::Locator.locate(thing)
|
27
24
|
elsif thing.is_a?(Hash) && thing['_aj_globalid'] && (found = GlobalID::Locator.locate(thing['_aj_globalid']))
|
28
25
|
return found
|
29
|
-
elsif thing.is_a?(String) && found = GlobalID::Locator.locate(thing)
|
26
|
+
elsif thing.is_a?(String) && (found = GlobalID::Locator.locate(thing))
|
30
27
|
return found
|
31
28
|
end
|
32
29
|
|
33
30
|
@_ensure_by ||= [@_additional_ensure_by || self.primary_key].flatten.compact.uniq
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
31
|
+
|
32
|
+
found = []
|
33
|
+
things = Array(thing)
|
34
|
+
things.each do |thing|
|
35
|
+
record = nil
|
36
|
+
@_ensure_by.each do |ensure_field|
|
37
|
+
value = thing
|
38
|
+
if thing.is_a?(Hash)
|
39
|
+
value = thing.fetch(ensure_field.to_sym, nil) || thing.fetch(ensure_field.to_s, nil)
|
40
|
+
end
|
41
|
+
record = find_by(ensure_field => value) if value.present? && !value.is_a?(Hash)
|
42
|
+
break if record.is_a?(self)
|
38
43
|
end
|
39
|
-
found
|
40
|
-
break if found.is_a?(self)
|
44
|
+
found << record
|
41
45
|
end
|
46
|
+
found.compact!
|
42
47
|
|
43
|
-
found
|
48
|
+
thing.is_a?(Array) ? found : found.first
|
44
49
|
end
|
45
50
|
|
46
51
|
def ensure!(thing = nil)
|
47
52
|
return nil unless thing.present?
|
48
53
|
result = self.ensure(thing)
|
49
|
-
raise ::ActiveRecord::RecordNotFound.new("#{self}
|
54
|
+
raise ::ActiveRecord::RecordNotFound.new("#{self} not found") unless result
|
50
55
|
result
|
51
56
|
end
|
52
57
|
|
data/lib/ensurance/version.rb
CHANGED