active_record_lite 0.2.0 → 0.3.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 +4 -4
- data/README.md +14 -2
- data/lib/active_record_lite/base.rb +12 -5
- data/lib/active_record_lite/core_ext.rb +4 -3
- data/lib/active_record_lite/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: 25396dbcff19274d249d4e139f1c04e82f574e968bcc0c142e07822176a7baca
|
4
|
+
data.tar.gz: 7638d9c05aebadc391cd49718800cd14f1f5d8d7a454baf0f4c258d94768859b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 35c746af91f3865d313117a9ce57a7f808338e247375c0fd4988898fdd58c4b5b880bca7f41ac7126f8f407eecdf02598ea2326abb01a7215a04242db4c3beef
|
7
|
+
data.tar.gz: 20cf42ac85d713411e6227f722a83db2510934f1e883e351939c256e40540cf9c1ca2cdab84b38e99cba5d6f655b0d10efcd3ca5e7fbc577f8a3a90572a7f62a
|
data/README.md
CHANGED
@@ -53,13 +53,25 @@ By default the lite class will include all columns defined in the parent model c
|
|
53
53
|
@user.email # => undefined method
|
54
54
|
```
|
55
55
|
|
56
|
+
For specific use-cases, you can also limit loading the number of columns by passing the selectable columns to `to_lite`.
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
class User < ActiveRecord::Base
|
60
|
+
has_lite, columns: %w(id name email)
|
61
|
+
end
|
62
|
+
|
63
|
+
@user = User.where(id: 1).to_lite(:id, :name) # select id, name from users where id = 1;
|
64
|
+
@user.id # => 1
|
65
|
+
@user.name # => "name"
|
66
|
+
|
67
|
+
@user.email # => nil
|
68
|
+
```
|
69
|
+
|
56
70
|
#### NOTE:
|
57
71
|
1. active_record_lite internally uses active_record, hence model level default scopes will apply when querying for the record from db.
|
58
72
|
|
59
73
|
2. `to_lite` should be called after all `serialize` calls in the model. `to_lite` relies on `serialised_attributes` which will be correctly initialised only after all the `serialize` calls.
|
60
74
|
|
61
|
-
3. Dynamic selects are not possible. `to_lite` will run a `unscope(:select)` on the current chained scope.
|
62
|
-
|
63
75
|
#### TODO:
|
64
76
|
1. Add support to eager load lite objects via AR relation.currently.
|
65
77
|
|
@@ -8,27 +8,34 @@ module ActiveRecordLite
|
|
8
8
|
}
|
9
9
|
end
|
10
10
|
|
11
|
-
def self.perform(sql)
|
11
|
+
def self.perform(sql, selects)
|
12
12
|
res = fetch_from_db(sql).map { |obj|
|
13
|
-
new(obj)
|
13
|
+
new(obj, selects)
|
14
14
|
}
|
15
15
|
res.size > 1 ? res : res.first
|
16
16
|
end
|
17
17
|
|
18
|
-
def initialize(obj)
|
18
|
+
def initialize(obj, selects)
|
19
19
|
@attributes = obj
|
20
|
+
@selects = selects&.map!(&:to_s)
|
20
21
|
end
|
21
22
|
|
22
23
|
private
|
23
24
|
|
24
|
-
|
25
|
+
attr_reader :attributes, :selects
|
25
26
|
|
26
27
|
def self.fetch_from_db(sql)
|
27
28
|
base_class.connection.select_rows(sql)
|
28
29
|
end
|
29
30
|
|
30
31
|
def read_attribute(attr_name)
|
31
|
-
|
32
|
+
if selects
|
33
|
+
index = selects.index(attr_name)
|
34
|
+
index && attributes[index]
|
35
|
+
else
|
36
|
+
index = self.class.attr_lookup(attr_name)
|
37
|
+
attributes[index]
|
38
|
+
end
|
32
39
|
end
|
33
40
|
|
34
41
|
def memoize_results(key)
|
@@ -5,9 +5,10 @@ module ActiveRecordLite
|
|
5
5
|
@_arlcolumns = options.delete(:columns)
|
6
6
|
end
|
7
7
|
class_eval <<-EOV
|
8
|
-
def self.to_lite
|
9
|
-
|
10
|
-
|
8
|
+
def self.to_lite(*args)
|
9
|
+
selects = args.presence || @_arlcolumns
|
10
|
+
scoper = unscope(:select).select(selects)
|
11
|
+
ActiveRecordLite.perform(scoper.to_sql, selects)
|
11
12
|
end
|
12
13
|
|
13
14
|
class ActiveRecordLite < ActiveRecordLite::Base
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_lite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ritikesh
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-07-
|
11
|
+
date: 2019-07-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|