like_query 0.0.1 → 0.0.2

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
  SHA256:
3
- metadata.gz: 16e59dc644a7dc678761df657971741c56ac76b3a0dcd773cb51b9e7ed241ecf
4
- data.tar.gz: 7abd672371cb6ec62799ed15d882f41ed2d88a66f5828f25541eaa96cf1d49ce
3
+ metadata.gz: 12f503e7aa188d223e922496909dabff55b2ff5c843e18ed23ae5525a5190dfa
4
+ data.tar.gz: fa7edce57c33d96b1c040767f5e41a95d32573b78cbc259ade1411178748a941
5
5
  SHA512:
6
- metadata.gz: adb0afaf4ef8a6bc4a5c1c5571958702c751405a55a9f50954f71c63e2ac6283e3f1b62488778ba0ecf3d144dd98dbad62c1585e7ed86e8c6c65ea40b6a11a6d
7
- data.tar.gz: 3fb0bd08e5e6c3853f1ab63ae306080a17354d277c7ac0ede1310404c9e2f68777edf6df0770161bc13bc4247efffd5d5c9b7229d1442ffbe4df9f7897d53eaf
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=>[["customer.name", "Müller"]], :id=>1},
87
- {:attributes=>[["customer.name", "Müller"]], :id=>2},
88
- {
89
- :attributes=>[[:name, "second"], [:number, "01"]], :id=>3,
90
- :associations=>{:customer=>[{:attributes=>[[:name, "Ambühl"]], :id=>2}]}}],
91
- :length=>4,
92
- :overflow=>true,
93
- :columns_count=>2,
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
  ```
@@ -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
- pattern = (result_pattern.present? ? result_pattern : recs.like_query_pattern)
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
- if @length >= @limit
46
- @overflow = true
47
- break
48
- elsif @length < @limit
49
- c = sub_attr.length
50
- @sub_records_columns_count = c if c > @sub_records_columns_count
51
- rec_attr[:associations][assoc] ||= []
52
- rec_attr[:associations][assoc].push({ attributes: sub_attr, id: _sub.id })
53
- @length += 1
54
- end
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, v])
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
- elsif @length < @limit
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
@@ -58,7 +58,7 @@ module LikeQuery
58
58
 
59
59
  raise 'has to be called behind #like' unless @like_query_pattern.is_a?(Array)
60
60
  c = LikeQuery::Collect.new(limit)
61
- c.receive(*pattern){@query}
61
+ c.receive(*pattern, limit: limit, image: image){@query}
62
62
  c.result
63
63
  end
64
64
 
@@ -1,3 +1,3 @@
1
1
  module LikeQuery
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: like_query
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - christian