hugg-orm 1.0.1 → 1.1.0

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: c07587f06381239099ab2d8b2747e8a40d75bd4e
4
- data.tar.gz: 468e1b2fe025bf0fd024664bd228b5c01b19966a
3
+ metadata.gz: f02a26d1a25e02eadcf890ea57f34aa326fff71e
4
+ data.tar.gz: 1f57e959f8f038e9a742eca9bb58d3de5b9089fb
5
5
  SHA512:
6
- metadata.gz: a9bd1a1a66940c573b458235c7174777aafdc92c6065d1e4e54f26be9a47251f37864e484e083014a80bf85004f80f8a776bb75cfdc526f4bba71eb6a6b85f34
7
- data.tar.gz: 1837c273d13659779155c47220c269eaca9e081f1c39ed47aa725c066a6b1eff44395f8170e76539e881e9cd8fcef8f8b2a51c6405e2b4309c379d82450b2e38
6
+ metadata.gz: 9d738cb17628d732da74012a9c12d39fe81ccbc04e7e6529f21b190ca29ec0c7095f271a90b58b9849db68709c5f4d78fcbd98e8092f6d10d90a741f483807c3
7
+ data.tar.gz: 5886ae7a88c7142d43749bc5e8f87c91f7ad7ad9551b1b26e91c31f5c0581062124d3f86708f171ed2b9704a0d1c958b02034b3b5adda8594f01b5db42fea853
@@ -17,7 +17,7 @@ module HuggORM
17
17
 
18
18
  def primary_key(primary_key = nil)
19
19
  self._primary_key = primary_key unless primary_key.nil?
20
- self._primary_key
20
+ self._primary_key ||= "data->>'#{key_field}'"
21
21
  end
22
22
 
23
23
  def key_field(key_field = nil)
@@ -36,7 +36,12 @@ module HuggORM
36
36
  end
37
37
 
38
38
  def find(id)
39
- where("#{primary_key} = ?", [id]).select.first
39
+ result = where("#{primary_key} = ?", [id]).select.first
40
+ new(result) if result
41
+ end
42
+
43
+ def find!(id)
44
+ find(id) || raise(RecordNotFound.new(id))
40
45
  end
41
46
 
42
47
  def all
@@ -44,6 +49,10 @@ module HuggORM
44
49
  new_collection(results)
45
50
  end
46
51
 
52
+ def delete_all
53
+ new_query.delete
54
+ end
55
+
47
56
  def select(query = nil, where: nil, order: nil, limit: nil, offset: nil)
48
57
  return new_collection(query.select) if query
49
58
 
@@ -65,10 +74,18 @@ module HuggORM
65
74
  new(results.first) unless results.empty?
66
75
  end
67
76
 
77
+ def update!(id, data)
78
+ update(id, data) || raise(RecordNotFound.new(id))
79
+ end
80
+
68
81
  def destroy(id)
69
82
  where("#{primary_key} = ?", [id]).delete > 0
70
83
  end
71
84
 
85
+ def destroy!(id)
86
+ destroy(id) || raise(RecordNotFound.new(id))
87
+ end
88
+
72
89
  def count
73
90
  new_query.count
74
91
  end
@@ -158,7 +175,7 @@ module HuggORM
158
175
  updated_data = self.class.where("#{self.class.primary_key} = ?", [key]).update(@data, true)
159
176
 
160
177
  unless updated_data.empty?
161
- @data = updated_data
178
+ @data = updated_data.first
162
179
  @state = :updated
163
180
  else
164
181
  self.class.new_query.insert(@data)
@@ -201,4 +218,6 @@ module HuggORM
201
218
  end
202
219
  end
203
220
  end
221
+
222
+ class RecordNotFound < StandardError; end
204
223
  end
@@ -1,3 +1,3 @@
1
1
  module HuggORM
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hugg-orm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Olle Johansson
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-11-01 00:00:00.000000000 Z
13
+ date: 2016-11-04 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: pg