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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 213c97b8eac33db98c540e1a0246a6e6f7bed4fd24c5f2e612ef7b6d99cdefcf
4
- data.tar.gz: 820a45e1e7ae286072eca822062a3ebb7ac9ad3218f7450b814cde996c48c07f
3
+ metadata.gz: d6b9215002c70ca7691296be27ad819cee6da1793db1fe8042296a9cce75aa51
4
+ data.tar.gz: 43558c3ab86ee99eb6d3d427bebe4458e865b29e77b9d2c0e2feb41d308b3a88
5
5
  SHA512:
6
- metadata.gz: 5ad24f5a10abfe820e7896dc88feacc1b6bd592b5f05c30142471594d1170f6d0a4a2db632039436ee0f383878cf00ccb7c717ee28122896b7e59d96e15e315b
7
- data.tar.gz: af6e3aa358afe08f58476d06c8847c6079222864c647e9092c5a840684b100b75ed700e0962e4488a524b121037242331743d2420311a216f4e0b7bc557ac8cd
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.ensuer!(nil) -> ActiveRecord::RecordNotFound
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
- @_ensure_by.each do |ensure_field|
35
- value = thing
36
- if thing.is_a?(Hash)
37
- value = thing.fetch(ensure_field.to_sym, nil) || thing.fetch(ensure_field.to_s, nil)
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 = self.find_by(ensure_field => value) if value.present? && !value.is_a?(Hash)
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}[#{thing ? thing : 'nil'}]") unless result
54
+ raise ::ActiveRecord::RecordNotFound.new("#{self} not found") unless result
50
55
  result
51
56
  end
52
57
 
@@ -1,3 +1,3 @@
1
1
  module Ensurance
2
- VERSION = "0.1.8"
2
+ VERSION = "0.1.9"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ensurance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Sharpe