ensurance 0.1.3 → 0.1.4

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
  SHA1:
3
- metadata.gz: 01f514ba5f0eef829157408e664c9e5156b2058e
4
- data.tar.gz: 491475ddd8df0c8fe9f6044015ec4bb08953f47a
3
+ metadata.gz: 4f356dd97f450da7e5d90f5f2dd000ae500754f1
4
+ data.tar.gz: 96489236b803cc22158f1a9a51a68edb6a4e4262
5
5
  SHA512:
6
- metadata.gz: 771842cca4e6ca4ffb2801d62bef28dc494278f580e52131051e93bab6e55675453986c81c6b2ad8419cdf3485f5c98fcb248ad28a8ff222e522ab623c7fdcc1
7
- data.tar.gz: a7868bd6816f1719de2cf547bc0584ed5eff89137eb089359dff8dd54bc096f9952598920e60d48a02f2b43b18dbb606a754b2c24efd1d7c8ca50d008d711b98
6
+ metadata.gz: 269feab585502a890af303f69ff32f4e4623ab524318faa186ec8d81c032f7b3a39927d686be1d3af07a126a6975fc7aa25f641b40274fbee6c73cda552a934f
7
+ data.tar.gz: '093ec632805684e2134361e8643cb4e1c575d1e586cb79e3b97e2bf74cc34620a45134a1e8dba3853761fba6fd0258724d71668cb66b86e899a0d525bcba1b29'
data/.gitignore CHANGED
@@ -9,4 +9,4 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
- *.gem
12
+ *.gem
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ensurance (0.1.2)
4
+ ensurance (0.1.3)
5
5
  activesupport (>= 3, < 6)
6
6
 
7
7
  GEM
@@ -19,6 +19,7 @@ GEM
19
19
  minitest (~> 5.1)
20
20
  tzinfo (~> 1.1)
21
21
  arel (8.0.0)
22
+ awesome_print (1.8.0)
22
23
  concurrent-ruby (1.0.5)
23
24
  diff-lcs (1.3)
24
25
  globalid (0.4.1)
@@ -50,6 +51,7 @@ PLATFORMS
50
51
 
51
52
  DEPENDENCIES
52
53
  activerecord (>= 3, < 6)
54
+ awesome_print (>= 1.0, < 2)
53
55
  bundler (~> 1.16)
54
56
  ensurance!
55
57
  globalid (>= 0.3.6, < 2)
data/README.md CHANGED
@@ -6,7 +6,7 @@ Allows you to ensure you have the class you expect... it's similar to
6
6
  result = value.is_a?(Person) ? value : Person.find(value)
7
7
  ```
8
8
 
9
- You can add fields to "ensure_by" (:id is included always)
9
+ You can add fields to "ensure_by" (`self.primary_key` is the default)
10
10
  e.g.
11
11
 
12
12
  if you add `ensure_by :token` to the User class
@@ -38,5 +38,6 @@ Gem::Specification.new do |spec|
38
38
  spec.add_development_dependency "activerecord", '>= 3', '< 6'
39
39
  spec.add_development_dependency "globalid", '>= 0.3.6', '< 2'
40
40
  spec.add_development_dependency "sqlite3", '>= 1.3.0', '< 4'
41
+ spec.add_development_dependency "awesome_print", '>= 1.0', '< 2'
41
42
  end
42
43
 
@@ -16,9 +16,12 @@ module Ensurance
16
16
 
17
17
  def ensure(thing = nil)
18
18
  return nil unless thing.present?
19
+ found = nil
19
20
 
20
21
  if thing.is_a?(self)
21
22
  return thing
23
+ elsif thing.is_a?(Array)
24
+ raise ArgumentError.new("Cannot Ensure #{self.name} with an Array: #{thing.inspect}")
22
25
  elsif thing.is_a?(GlobalID)
23
26
  return GlobalID::Locator.locate(thing)
24
27
  elsif thing.is_a?(Hash) && thing['_aj_globalid'] && (found = GlobalID::Locator.locate(thing['_aj_globalid']))
@@ -26,12 +29,14 @@ module Ensurance
26
29
  elsif thing.is_a?(String) && found = GlobalID::Locator.locate(thing)
27
30
  return found
28
31
  end
29
- found = nil
30
32
 
31
- @_ensure_by ||= [self.primary_key, @_additional_ensure_by].flatten.compact.uniq
33
+ @_ensure_by ||= [@_additional_ensure_by || self.primary_key].flatten.compact.uniq
32
34
  @_ensure_by.each do |ensure_field|
33
- value = thing.try(:fetch, ensure_field.to_sym, nil) || thing.try(:fetch, ensure_field.to_s, nil) || thing
34
- found = self.find_by(ensure_field => value)
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)
38
+ end
39
+ found = self.find_by(ensure_field => value) if value.present? && !value.is_a?(Hash)
35
40
  break if found.is_a?(self)
36
41
  end
37
42
 
@@ -1,3 +1,3 @@
1
1
  module Ensurance
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ensurance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Sharpe
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-01 00:00:00.000000000 Z
11
+ date: 2017-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -132,6 +132,26 @@ dependencies:
132
132
  - - "<"
133
133
  - !ruby/object:Gem::Version
134
134
  version: '4'
135
+ - !ruby/object:Gem::Dependency
136
+ name: awesome_print
137
+ requirement: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '1.0'
142
+ - - "<"
143
+ - !ruby/object:Gem::Version
144
+ version: '2'
145
+ type: :development
146
+ prerelease: false
147
+ version_requirements: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '1.0'
152
+ - - "<"
153
+ - !ruby/object:Gem::Version
154
+ version: '2'
135
155
  description: 'A handy shortcut for user = user.is_a?(User) ? user : User.find(user)'
136
156
  email:
137
157
  - bsharpe@gmail.com
@@ -149,7 +169,6 @@ files:
149
169
  - Rakefile
150
170
  - bin/console
151
171
  - bin/setup
152
- - ensurance-0.1.0.gem
153
172
  - ensurance.gemspec
154
173
  - lib/ensurance.rb
155
174
  - lib/ensurance/date_ensure.rb