like_query 0.0.1 → 0.0.2
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 +13 -9
- data/lib/like_query/collect.rb +76 -29
- data/lib/like_query/model_extensions.rb +1 -1
- data/lib/like_query/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 12f503e7aa188d223e922496909dabff55b2ff5c843e18ed23ae5525a5190dfa
|
4
|
+
data.tar.gz: fa7edce57c33d96b1c040767f5e41a95d32573b78cbc259ade1411178748a941
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 50b1ff03fd06283c506b70a20867261236cae2e2d12f00732efb3a42da474761d817163c77fa8a6eda205b0a96812bb3d7347b86b4adf4184287f53ee1b7dabb
|
7
|
+
data.tar.gz: 3dd63ff9264451c79616e112885329ddff287ac34abb0dc5df438cba2de4402c8756b30aa65b7ae085c55f0f20e4d1919d02b847032a2b959eee3a55ee38737e
|
data/README.md
CHANGED
@@ -75,7 +75,7 @@ c = LikeQuery::Collect.new(4)
|
|
75
75
|
c.receive { Article.like('any-art', :name) }
|
76
76
|
# => would add 4 articles to the result hash because of limit
|
77
77
|
|
78
|
-
c.receive { Customer.like('any-art', :name, articles: :name) }
|
78
|
+
c.receive { Customer.like('any-art', :name, image: :image_column, articles: :name) }
|
79
79
|
# => limit is already exhausted: does nothing
|
80
80
|
# => otherwise it would add Customers to the result hash
|
81
81
|
|
@@ -83,14 +83,18 @@ c.result
|
|
83
83
|
# => would return anything like:
|
84
84
|
{
|
85
85
|
:data=>[
|
86
|
-
{:attributes=>[[
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
86
|
+
{:attributes=>[[:name, "Ambühl"]], :id=>1, :image=>"src:customer-image",
|
87
|
+
:associations=>{
|
88
|
+
:articles=>[
|
89
|
+
{:attributes=>[[:number, "01"]], :id=>1, :image=>"src:article-image"},
|
90
|
+
{:attributes=>[[:number, "01"]], :id=>2, :image=>"src:article-image"}
|
91
|
+
]
|
92
|
+
}
|
93
|
+
}
|
94
|
+
],
|
95
|
+
:length=>3,
|
96
|
+
:overflow=>false,
|
97
|
+
:columns_count=>1,
|
94
98
|
:sub_records_columns_count=>1
|
95
99
|
}
|
96
100
|
```
|
data/lib/like_query/collect.rb
CHANGED
@@ -9,18 +9,25 @@ module LikeQuery
|
|
9
9
|
@sub_records_columns_count = 0
|
10
10
|
end
|
11
11
|
|
12
|
-
def receive(*result_pattern, image: nil, &block)
|
12
|
+
def receive(*result_pattern, limit: nil, image: nil, &block)
|
13
13
|
|
14
14
|
return false if @length >= @limit
|
15
|
+
length = 0
|
15
16
|
|
16
17
|
recs = yield
|
17
18
|
|
18
|
-
|
19
|
+
_pattern = (result_pattern.present? ? result_pattern : recs.like_query_pattern)
|
20
|
+
if _pattern.first.is_a?(Array)
|
21
|
+
raise 'pattern can only be a array of hashes or symbols' if _pattern.length >= 2
|
22
|
+
pattern = _pattern.first
|
23
|
+
else
|
24
|
+
pattern = _pattern
|
25
|
+
end
|
19
26
|
|
20
27
|
associations = []
|
21
28
|
pattern.each do |p|
|
22
29
|
if p.is_a?(Hash)
|
23
|
-
associations += p.keys
|
30
|
+
associations += p.keys.map { |k| (k.to_sym >= :image ? nil : k.to_sym) }.compact
|
24
31
|
end
|
25
32
|
end
|
26
33
|
|
@@ -29,52 +36,84 @@ module LikeQuery
|
|
29
36
|
pattern.each do |p|
|
30
37
|
if p.is_a?(Hash)
|
31
38
|
|
32
|
-
# ASSOCIATED RECORDS
|
33
|
-
|
34
39
|
p.each do |assoc, cols|
|
35
|
-
rec_attr[:associations] ||= {}
|
36
|
-
rec_attr[:associations][assoc] ||= []
|
37
|
-
sub = r.send(assoc)
|
38
|
-
_cols = (cols.is_a?(Enumerable) ? cols : [cols])
|
39
|
-
(sub.is_a?(Enumerable) ? sub : [sub]).each do |_sub|
|
40
|
-
sub_attr = []
|
41
|
-
_cols.each do |c|
|
42
|
-
sub_attr.push([c, _sub.send(c)])
|
43
|
-
end
|
44
40
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
41
|
+
if assoc.to_sym == :image
|
42
|
+
|
43
|
+
raise 'Missing input: image column for given key :image' unless cols.present?
|
44
|
+
|
45
|
+
# IMAGE COLUMN
|
46
|
+
|
47
|
+
rec_attr[:image] = get_column_value(r, cols)
|
48
|
+
|
49
|
+
else
|
50
|
+
|
51
|
+
# ASSOCIATIONS
|
55
52
|
|
53
|
+
sub_records = r.send(assoc)
|
54
|
+
image_column = nil
|
55
|
+
|
56
|
+
|
57
|
+
_cols = if cols.is_a?(Hash)
|
58
|
+
image_column = cols[:image]
|
59
|
+
cols[:attributes]
|
60
|
+
elsif cols.is_a?(Enumerable)
|
61
|
+
cols
|
62
|
+
else
|
63
|
+
[cols]
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
#_cols = (cols.is_a?(Enumerable) ? cols : [cols])
|
68
|
+
(sub_records.is_a?(Enumerable) ? sub_records : [sub_records]).each do |sub_record|
|
69
|
+
sub_attr = []
|
70
|
+
_cols.each do |c|
|
71
|
+
sub_attr.push([c, sub_record.send(c)])
|
72
|
+
end
|
73
|
+
|
74
|
+
if @length >= @limit || (limit && length >= limit)
|
75
|
+
@overflow = true
|
76
|
+
break
|
77
|
+
else
|
78
|
+
c = sub_attr.length
|
79
|
+
@sub_records_columns_count = c if c > @sub_records_columns_count
|
80
|
+
rec_attr[:associations] ||= {}
|
81
|
+
rec_attr[:associations][assoc] ||= []
|
82
|
+
sub_hash = { attributes: sub_attr, id: sub_record.id }
|
83
|
+
sub_hash[:image] = get_column_value(sub_record, image_column) if image_column
|
84
|
+
rec_attr[:associations][assoc].push(sub_hash)
|
85
|
+
@length += 1
|
86
|
+
length += 1
|
87
|
+
end
|
88
|
+
end
|
56
89
|
end
|
57
90
|
end
|
58
91
|
elsif p.is_a?(Symbol) || p.is_a?(String)
|
59
92
|
|
60
93
|
# MAIN RECORD
|
61
94
|
|
62
|
-
v = nil
|
63
|
-
p.to_s.split('.').each { |_p| v = (v ? v : r).send(_p) }
|
95
|
+
# v = nil
|
96
|
+
# p.to_s.split('.').each { |_p| v = (v ? v : r).send(_p) }
|
64
97
|
rec_attr[:attributes] ||= []
|
65
|
-
rec_attr[:attributes].push([p,
|
98
|
+
rec_attr[:attributes].push([p, get_column_value(r, p)])
|
99
|
+
# if image
|
100
|
+
# img = nil
|
101
|
+
# image.to_s.split('.').each { |i| img = (img ? img : r).send(i) }
|
102
|
+
# rec_attr[:image] = img
|
103
|
+
# end
|
66
104
|
rec_attr[:id] = r.id
|
67
105
|
|
68
106
|
end
|
69
107
|
end
|
70
|
-
if @length >= @limit
|
108
|
+
if @length >= @limit || (limit && length >= limit)
|
71
109
|
@overflow = true
|
72
110
|
break
|
73
|
-
|
111
|
+
else
|
74
112
|
c = (image ? 1 : 0) + rec_attr[:attributes].length
|
75
113
|
@columns_count = c if c > @columns_count
|
76
114
|
@data.push(rec_attr)
|
77
115
|
@length += 1
|
116
|
+
length += 1
|
78
117
|
end
|
79
118
|
end
|
80
119
|
|
@@ -91,5 +130,13 @@ module LikeQuery
|
|
91
130
|
}
|
92
131
|
end
|
93
132
|
|
133
|
+
private
|
134
|
+
|
135
|
+
def get_column_value(record, column)
|
136
|
+
val = nil
|
137
|
+
column.to_s.split('.').each { |i| val = (val ? val : record).send(i) }
|
138
|
+
val
|
139
|
+
end
|
140
|
+
|
94
141
|
end
|
95
142
|
end
|
data/lib/like_query/version.rb
CHANGED