like_query 0.0.1 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 16e59dc644a7dc678761df657971741c56ac76b3a0dcd773cb51b9e7ed241ecf
4
- data.tar.gz: 7abd672371cb6ec62799ed15d882f41ed2d88a66f5828f25541eaa96cf1d49ce
3
+ metadata.gz: eb533ef11731913bdf3356f5177354cbdc85b4506ba2aae392f7149b7fe7e6d2
4
+ data.tar.gz: 4b256fb9aa9336559e6a9fc529e7376fae1f6fc09da1cd9b662eb18dd770c5dd
5
5
  SHA512:
6
- metadata.gz: adb0afaf4ef8a6bc4a5c1c5571958702c751405a55a9f50954f71c63e2ac6283e3f1b62488778ba0ecf3d144dd98dbad62c1585e7ed86e8c6c65ea40b6a11a6d
7
- data.tar.gz: 3fb0bd08e5e6c3853f1ab63ae306080a17354d277c7ac0ede1310404c9e2f68777edf6df0770161bc13bc4247efffd5d5c9b7229d1442ffbe4df9f7897d53eaf
6
+ metadata.gz: 6fdeaadc79ef10c0690dff7d0213d8241ea0f01679fdb1494d95bb4b9f975813ef5319ae2639a431bb291793ca74ceb1902ca11e9ba5ad9b7423f114c4b3ed0e
7
+ data.tar.gz: 7d53223f4fe56b448e28aebb46e3db3f400de45b7698a5a3526543612180f4a937951733ade9cff093e177e9d4340fd50b90cc132ea64be811cde32ab2440e7b
data/README.md CHANGED
@@ -47,22 +47,24 @@ art1 = Article.create(name: 'first', number: '01', customer: customer)
47
47
 
48
48
  Article.like('fir', :name).like_result(limit: 10)
49
49
  # returns:
50
- # {
51
- # :data=>[
52
- # {
53
- # :attributes=>[[:name, "first"]],
54
- # :id=>1}],
55
- # :length=>1,
56
- # :overflow=>false,
57
- # :columns_count=>1,
58
- # :sub_records_columns_count=>0
59
- # }
50
+ {
51
+ :data=>[
52
+ {
53
+ :attributes=>[[:name, "first"]],
54
+ :id=>1}],
55
+ :length=>1,
56
+ :overflow=>false,
57
+ :columns_count=>1,
58
+ :sub_records_columns_count=>0
59
+ }
60
60
 
61
61
  Article.like('fir', :name).like_result( :number, limit: 10)
62
62
  # would query like the above example: Search scope is only :name
63
63
  # but would return article-number instead of article-name inside the data block
64
64
  ```
65
65
 
66
+ `#like_result` uses `LikeQuery::Collect`, functionality is the same.
67
+
66
68
  **Class LikeQuery::Collect**
67
69
 
68
70
  ```ruby
@@ -75,22 +77,26 @@ c = LikeQuery::Collect.new(4)
75
77
  c.receive { Article.like('any-art', :name) }
76
78
  # => would add 4 articles to the result hash because of limit
77
79
 
78
- c.receive { Customer.like('any-art', :name, articles: :name) }
80
+ c.receive { Customer.like('any-art', :name, image: :image_column, articles: :name) }
79
81
  # => limit is already exhausted: does nothing
80
82
  # => otherwise it would add Customers to the result hash
81
83
 
82
84
  c.result
83
- # => would return anything like:
85
+ # => would return anything like (this output is from different code!!):
84
86
  {
85
87
  :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,
88
+ {:attributes=>[[:name, "Ambühl"]], :id=>1, :image=>"src:customer-image",
89
+ :associations=>{
90
+ :articles=>[
91
+ {:attributes=>[[:number, "01"]], :id=>1, :image=>"src:article-image"},
92
+ {:attributes=>[[:number, "01"]], :id=>2, :image=>"src:article-image"}
93
+ ]
94
+ }
95
+ }
96
+ ],
97
+ :length=>3,
98
+ :overflow=>false,
99
+ :columns_count=>1,
94
100
  :sub_records_columns_count=>1
95
101
  }
96
102
  ```
@@ -7,20 +7,29 @@ module LikeQuery
7
7
  @overflow = false
8
8
  @columns_count = 0
9
9
  @sub_records_columns_count = 0
10
+ @image = false
11
+ @sub_records_image = false
10
12
  end
11
13
 
12
- def receive(*result_pattern, image: nil, &block)
14
+ def receive(*result_pattern, limit: nil, image: nil, &block)
13
15
 
14
16
  return false if @length >= @limit
17
+ length = 0
15
18
 
16
19
  recs = yield
17
20
 
18
- pattern = (result_pattern.present? ? result_pattern : recs.like_query_pattern)
21
+ _pattern = (result_pattern.present? ? result_pattern : recs.like_query_pattern)
22
+ if _pattern.first.is_a?(Array)
23
+ raise 'pattern can only be a array of hashes or symbols' if _pattern.length >= 2
24
+ pattern = _pattern.first
25
+ else
26
+ pattern = _pattern
27
+ end
19
28
 
20
29
  associations = []
21
30
  pattern.each do |p|
22
31
  if p.is_a?(Hash)
23
- associations += p.keys
32
+ associations += p.keys.map { |k| (k.to_sym >= :image ? nil : k.to_sym) }.compact
24
33
  end
25
34
  end
26
35
 
@@ -29,52 +38,79 @@ module LikeQuery
29
38
  pattern.each do |p|
30
39
  if p.is_a?(Hash)
31
40
 
32
- # ASSOCIATED RECORDS
33
-
34
41
  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
42
 
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
43
+ if assoc.to_sym == :image
44
+
45
+ raise 'Missing input: image column for given key :image' unless cols.present?
46
+
47
+ # IMAGE COLUMN
48
+
49
+ rec_attr[:image] = get_column_value(r, cols)
50
+ @image = true
51
+
52
+ else
53
+
54
+ # ASSOCIATIONS
55
+
56
+ sub_records = r.send(assoc)
57
+ image_column = nil
58
+
59
+ _cols = if cols.is_a?(Hash)
60
+ image_column = cols[:image]
61
+ cols[:attributes]
62
+ elsif cols.is_a?(Enumerable)
63
+ cols
64
+ else
65
+ [cols]
66
+ end
67
+
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, model: sub_record.class.to_s.underscore }
83
+ if image_column
84
+ sub_hash[:image] = get_column_value(sub_record, image_column)
85
+ @sub_records_image = true
86
+ end
87
+ rec_attr[:associations][assoc].push(sub_hash)
88
+ @length += 1
89
+ length += 1
90
+ end
54
91
  end
55
-
56
92
  end
57
93
  end
58
94
  elsif p.is_a?(Symbol) || p.is_a?(String)
59
95
 
60
96
  # MAIN RECORD
61
97
 
62
- v = nil
63
- p.to_s.split('.').each { |_p| v = (v ? v : r).send(_p) }
64
98
  rec_attr[:attributes] ||= []
65
- rec_attr[:attributes].push([p, v])
99
+ rec_attr[:attributes].push([p, get_column_value(r, p)])
66
100
  rec_attr[:id] = r.id
101
+ rec_attr[:model] = r.class.to_s.underscore
67
102
 
68
103
  end
69
104
  end
70
- if @length >= @limit
105
+ if @length >= @limit || (limit && length >= limit)
71
106
  @overflow = true
72
107
  break
73
- elsif @length < @limit
108
+ else
74
109
  c = (image ? 1 : 0) + rec_attr[:attributes].length
75
110
  @columns_count = c if c > @columns_count
76
111
  @data.push(rec_attr)
77
112
  @length += 1
113
+ length += 1
78
114
  end
79
115
  end
80
116
 
@@ -87,9 +123,19 @@ module LikeQuery
87
123
  length: @length,
88
124
  overflow: @overflow,
89
125
  columns_count: @columns_count,
90
- sub_records_columns_count: @sub_records_columns_count
126
+ sub_records_columns_count: @sub_records_columns_count,
127
+ image: @image,
128
+ sub_records_image: @sub_records_image
91
129
  }
92
130
  end
93
131
 
132
+ private
133
+
134
+ def get_column_value(record, column)
135
+ val = nil
136
+ column.to_s.split('.').each { |i| val = (val ? val : record).send(i) }
137
+ val
138
+ end
139
+
94
140
  end
95
141
  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.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
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.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - christian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-26 00:00:00.000000000 Z
11
+ date: 2023-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails