occams-record 0.28.0 → 0.29.0
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 +5 -5
- data/README.md +2 -2
- data/lib/occams-record/errors.rb +19 -0
- data/lib/occams-record/query.rb +11 -1
- data/lib/occams-record/raw_query.rb +0 -9
- data/lib/occams-record/results.rb +15 -5
- data/lib/occams-record/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f01a553f2a0d8dcb7e2efaac70088239a05de6633375b8bf3f5c1f77a43808c8
|
4
|
+
data.tar.gz: 38095dbb827e293088f515e04bfdb907f088e1bddce0cfdc43f38455e8bafab4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c221f51d83965ef207975806a2d5d7b4c6867ba7e1f2f9f08e2c2b78320de262d7863cfc1c215b37a12d5716b99377b2ee43d5697284e7e483f81f110572581b
|
7
|
+
data.tar.gz: 37c429c2af89af793ad80bbf87fe4b8083626d540e78cfb4887ffb3eb83c6d4019a4abfc26daf438a58256a36a136d2918d04499eb390cabf62016bf8da97208
|
data/README.md
CHANGED
@@ -48,11 +48,11 @@ orders = OccamsRecord.
|
|
48
48
|
run
|
49
49
|
````
|
50
50
|
|
51
|
-
`each`, `map`, `reduce`, and
|
51
|
+
`each`, `map`, `reduce`, and other Enumerable methods may be used instead of *run*. `find_each` and `find_in_batches` are also supported. Unlike their ActiveRecord counterparts they respect *ORDER BY*. Occams Record has great support for raw SQL queries too, but we'll get to those later.
|
52
52
|
|
53
53
|
## Basic eager loading
|
54
54
|
|
55
|
-
Eager loading is similiar to ActiveRecord's `preload` (each association is loaded in a separate query). Nested associations use blocks instead of Hashes.
|
55
|
+
Eager loading is similiar to ActiveRecord's `preload` (each association is loaded in a separate query). Nested associations use blocks instead of Hashes. And if you try to use an association you didn't eager load an exception will be raised.
|
56
56
|
|
57
57
|
```ruby
|
58
58
|
orders = OccamsRecord.
|
data/lib/occams-record/errors.rb
CHANGED
@@ -31,4 +31,23 @@ module OccamsRecord
|
|
31
31
|
"Association '#{name}' is unavailable on #{model_name} because it was not eager loaded!"
|
32
32
|
end
|
33
33
|
end
|
34
|
+
|
35
|
+
# Exception when a requested record couldn't be found.
|
36
|
+
class NotFound < StandardError
|
37
|
+
# @return [String]
|
38
|
+
attr_reader :model_name
|
39
|
+
# @return [Hash]
|
40
|
+
attr_reader :attrs
|
41
|
+
|
42
|
+
# @param model_name [String]
|
43
|
+
# @param attrs [Hash]
|
44
|
+
def initialize(model_name, attrs)
|
45
|
+
@model_name, @attrs = model_name, attrs
|
46
|
+
end
|
47
|
+
|
48
|
+
# @return [String]
|
49
|
+
def message
|
50
|
+
"#{model_name} could not be found with #{attrs}!"
|
51
|
+
end
|
52
|
+
end
|
34
53
|
end
|
data/lib/occams-record/query.rb
CHANGED
@@ -100,7 +100,7 @@ module OccamsRecord
|
|
100
100
|
end
|
101
101
|
|
102
102
|
#
|
103
|
-
# Run the query and return the first result (which could be nil)
|
103
|
+
# Run the query with LIMIT 1 and return the first result (which could be nil).
|
104
104
|
#
|
105
105
|
# @return [OccamsRecord::Results::Row]
|
106
106
|
#
|
@@ -108,6 +108,16 @@ module OccamsRecord
|
|
108
108
|
run { |q| q.limit 1 }.first
|
109
109
|
end
|
110
110
|
|
111
|
+
#
|
112
|
+
# Run the query with LIMIT 1 and return the first result. If nothing is found
|
113
|
+
# an OccamsRecord::NotFound exception will be raised.
|
114
|
+
#
|
115
|
+
# @return [OccamsRecord::Results::Row]
|
116
|
+
#
|
117
|
+
def first!
|
118
|
+
first || raise(OccamsRecord::NotFound.new(model.name, scope.where_values_hash))
|
119
|
+
end
|
120
|
+
|
111
121
|
#
|
112
122
|
# If you pass a block, each result row will be yielded to it. If you don't,
|
113
123
|
# an Enumerable will be returned.
|
@@ -106,15 +106,6 @@ module OccamsRecord
|
|
106
106
|
|
107
107
|
alias_method :to_a, :run
|
108
108
|
|
109
|
-
#
|
110
|
-
# Run the query and return the first result (which could be nil). IMPORTANT you MUST add LIMIT 1 yourself!
|
111
|
-
#
|
112
|
-
# @return [OccamsRecord::Results::Row]
|
113
|
-
#
|
114
|
-
def first
|
115
|
-
run[0]
|
116
|
-
end
|
117
|
-
|
118
109
|
#
|
119
110
|
# If you pass a block, each result row will be yielded to it. If you don't,
|
120
111
|
# an Enumerable will be returned.
|
@@ -84,6 +84,16 @@ module OccamsRecord
|
|
84
84
|
@cast_values = {}
|
85
85
|
end
|
86
86
|
|
87
|
+
#
|
88
|
+
# Hash-like accessor for attributes and associations.
|
89
|
+
#
|
90
|
+
# @param attr [String|Symbol\
|
91
|
+
# @return [Object]
|
92
|
+
#
|
93
|
+
def [](attr)
|
94
|
+
respond_to?(attr) ? send(attr) : nil
|
95
|
+
end
|
96
|
+
|
87
97
|
#
|
88
98
|
# Returns true if the two objects are from the same table and have the same primary key.
|
89
99
|
#
|
@@ -99,13 +109,13 @@ module OccamsRecord
|
|
99
109
|
end
|
100
110
|
|
101
111
|
#
|
102
|
-
# Return row as a Hash
|
112
|
+
# Return row as a Hash. By default the hash does NOT include associations.
|
103
113
|
#
|
104
114
|
# @param symbolize_names [Boolean] if true, make Hash keys Symbols instead of Strings
|
105
|
-
# @param recursive [Boolean] if true,
|
115
|
+
# @param recursive [Boolean] if true, include assiciations and them (and their associations) to hashes.
|
106
116
|
# @return [Hash] a Hash with String or Symbol keys
|
107
117
|
#
|
108
|
-
def to_h(symbolize_names: false, recursive:
|
118
|
+
def to_h(symbolize_names: false, recursive: false)
|
109
119
|
hash = self.class.columns.reduce({}) { |a, col_name|
|
110
120
|
key = symbolize_names ? col_name.to_sym : col_name
|
111
121
|
a[key] = send col_name
|
@@ -116,9 +126,9 @@ module OccamsRecord
|
|
116
126
|
key = symbolize_names ? assoc_name.to_sym : assoc_name
|
117
127
|
assoc = send assoc_name
|
118
128
|
a[key] = if assoc.is_a? Array
|
119
|
-
assoc.map { |x| x.to_h(symbolize_names: symbolize_names) }
|
129
|
+
assoc.map { |x| x.to_h(symbolize_names: symbolize_names, recursive: true) }
|
120
130
|
elsif assoc
|
121
|
-
assoc.to_h(symbolize_names: symbolize_names)
|
131
|
+
assoc.to_h(symbolize_names: symbolize_names, recursive: true)
|
122
132
|
end
|
123
133
|
a
|
124
134
|
} : hash
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: occams-record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.29.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jordan Hollinger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-05-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -76,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
76
|
version: '0'
|
77
77
|
requirements: []
|
78
78
|
rubyforge_project:
|
79
|
-
rubygems_version: 2.
|
79
|
+
rubygems_version: 2.7.3
|
80
80
|
signing_key:
|
81
81
|
specification_version: 4
|
82
82
|
summary: The missing high-efficiency query API for ActiveRecord
|