ensurance 0.1.15 → 0.1.16
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 +4 -4
- data/lib/ensurance.rb +13 -3
- data/lib/ensurance/hash_ensure.rb +2 -2
- data/lib/ensurance/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7675cd410c9fa7bcb7dc42b2f5a133e87025b52afef3741eebae60cf071def52
|
4
|
+
data.tar.gz: bca8136a4278bbdd298cfd4e1de29594c5431ad79388b1a17ef89edf9b0a426d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2092f81cb9d3dc738a6ac4c8ca99970a313a81a6c6dfe7262aafb00215a751932c66f3a815c21a638d995ddfaa07045c82b7373f0cb35989daec864818177056
|
7
|
+
data.tar.gz: 2ff2921bbb02f2c32aacabacde7febe85da921c177e1c3ede7a9835c7a198e979787f52ec9e1970eaa5e1380c4b5cc9e879ee0e8bd9ffe5810d125be17702a07
|
data/lib/ensurance.rb
CHANGED
@@ -10,9 +10,16 @@ module Ensurance
|
|
10
10
|
extend ActiveSupport::Concern
|
11
11
|
|
12
12
|
class_methods do
|
13
|
-
|
13
|
+
@_additional_ensure_by = []
|
14
|
+
@_ensure_order = nil
|
15
|
+
@_ensure_by = []
|
16
|
+
|
17
|
+
def ensure_by(*args, order: nil)
|
14
18
|
@_additional_ensure_by = args
|
15
|
-
@
|
19
|
+
@_ensure_order = (order || primary_key).to_s
|
20
|
+
@_ensure_by = [@_additional_ensure_by || primary_key].flatten.compact.uniq
|
21
|
+
# ap "Ensure By: #{self}.#{@_ensure_by} Order: #{self}.#{@_ensure_order}"
|
22
|
+
raise ArgumentError.new("#{self} does not have column[#{@_ensure_order}] to sort by") unless self.column_names.include?(@_ensure_order)
|
16
23
|
end
|
17
24
|
|
18
25
|
def ensure(thing = nil)
|
@@ -29,6 +36,7 @@ module Ensurance
|
|
29
36
|
end
|
30
37
|
|
31
38
|
@_ensure_by ||= [@_additional_ensure_by || primary_key].flatten.compact.uniq
|
39
|
+
@_ensure_order ||= primary_key
|
32
40
|
|
33
41
|
found = []
|
34
42
|
things = [thing].flatten
|
@@ -42,7 +50,9 @@ module Ensurance
|
|
42
50
|
if ensure_field.to_sym == :id
|
43
51
|
begin
|
44
52
|
# Always return the most recent record that matches
|
45
|
-
|
53
|
+
query = where(ensure_field => value)
|
54
|
+
query = query.order("#{@_ensure_order}" => 'desc')
|
55
|
+
record = query.first
|
46
56
|
rescue ActiveRecord::RecordNotFound
|
47
57
|
nil
|
48
58
|
end
|
@@ -12,12 +12,12 @@ module Ensurance
|
|
12
12
|
module ClassMethods
|
13
13
|
def ensure(thing)
|
14
14
|
case thing.class.name
|
15
|
+
when 'NilClass'
|
16
|
+
nil
|
15
17
|
when 'Hash', 'HashWithIndifferentAccess'
|
16
18
|
thing
|
17
19
|
when 'String'
|
18
20
|
JSON.parse(thing)
|
19
|
-
when 'NilClass'
|
20
|
-
nil
|
21
21
|
when 'ActionController::UnfilteredParameters', 'ActionController::Parameters'
|
22
22
|
thing.permit!.to_h
|
23
23
|
else
|
data/lib/ensurance/version.rb
CHANGED
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.
|
4
|
+
version: 0.1.16
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Sharpe
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-10-
|
11
|
+
date: 2018-10-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|