like_query 0.0.17 → 0.1.1
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 +7 -0
- data/lib/like_query/model_extensions.rb +51 -9
- data/lib/like_query/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: 9f271a2601cfb3587fb40e2cd50ae325d54b00194b3ae6935e1ebe9e4b9e8bcb
|
4
|
+
data.tar.gz: bd3d3863befc960ed4f94842f0e6643715aae58ff16394b7d047b124d0ca26b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 18786e249d72ece3a0d442be622a0cb44d99ffd7ea596dc0b5a0f4cc92f4845d9eea08f61ecfbdeb8959802a8115a258bf53f207f7590fd0d95c0e38c7285578
|
7
|
+
data.tar.gz: 43f86c5823d53103ea79ce9ecc92318b6fbf5ba2cccf9702a3c107b7f8b080df0f62144d76c0b20e157920a7d8df641791f6b718eeee23518bda40aad7b31cd2
|
data/README.md
CHANGED
@@ -99,6 +99,13 @@ Article.like('Truck', [:type_enum]).count #=> 1
|
|
99
99
|
|
100
100
|
Translated enums and enums on associated models (e.g. article.customer) not yet implemented
|
101
101
|
|
102
|
+
**Numbers**
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
Book.like('Nelson 3.4..8', [:title, :price])
|
106
|
+
# => find books where title includes "Nelson" and price (must be number column) is between 3.4 and 8
|
107
|
+
```
|
108
|
+
|
102
109
|
**Class LikeQuery::Collect**
|
103
110
|
|
104
111
|
```ruby
|
@@ -3,7 +3,9 @@ module LikeQuery
|
|
3
3
|
|
4
4
|
def like(search_string, schema = nil)
|
5
5
|
|
6
|
-
search_string = ''
|
6
|
+
search_string = '' if !search_string
|
7
|
+
search_string = search_string.to_s unless search_string.is_a?(String)
|
8
|
+
|
7
9
|
raise 'like can only be called from a model' if self == ApplicationRecord
|
8
10
|
|
9
11
|
queries = nil
|
@@ -11,8 +13,10 @@ module LikeQuery
|
|
11
13
|
schema = [schema] if schema.is_a?(Symbol)
|
12
14
|
@like_query_schema = schema
|
13
15
|
|
14
|
-
|
15
|
-
|
16
|
+
search_string.split(' ').each do |s|
|
17
|
+
|
18
|
+
f_str = format_string_element(s)
|
19
|
+
|
16
20
|
q = nil
|
17
21
|
if schema.first.is_a?(Array) && schema.length >= 2
|
18
22
|
raise "only one array can be given: Either schema as one array or as multiple args, not as array"
|
@@ -21,26 +25,26 @@ module LikeQuery
|
|
21
25
|
enum_lab = enum_keys(p, s)
|
22
26
|
if enum_lab.present?
|
23
27
|
q = arel_table[p].in(enum_lab)
|
24
|
-
elsif p.is_a?(Symbol) || p.is_a?(String)
|
25
|
-
_q = arel_table[p].matches(str)
|
26
|
-
q = (q ? q.or(_q) : _q)
|
27
28
|
elsif p.is_a?(Hash)
|
28
29
|
p.each do |k, v|
|
29
30
|
assoc = reflect_on_association(k)
|
30
31
|
raise "Unknown association: :#{k}" unless assoc.present?
|
31
32
|
associations.push(k.to_sym) unless associations.include?(k.to_sym)
|
32
33
|
if v.is_a?(Symbol) || v.is_a?(String)
|
33
|
-
_q = assoc
|
34
|
+
_q = create_arel(v, f_str, assoc)
|
34
35
|
q = (q ? q.or(_q) : _q)
|
35
36
|
elsif v.is_a?(Array)
|
36
37
|
v.each do |_v|
|
37
|
-
_q = assoc
|
38
|
+
_q = create_arel(_v, f_str, assoc)
|
38
39
|
q = (q ? q.or(_q) : _q)
|
39
40
|
end
|
40
41
|
else
|
41
42
|
raise "unknown element: #{p}"
|
42
43
|
end
|
43
44
|
end
|
45
|
+
else
|
46
|
+
_q = create_arel(p, f_str)
|
47
|
+
q = (q ? q.or(_q) : _q)
|
44
48
|
end
|
45
49
|
|
46
50
|
end
|
@@ -80,11 +84,49 @@ module LikeQuery
|
|
80
84
|
en = defined_enums[column_name.to_s]
|
81
85
|
return nil if en.nil?
|
82
86
|
r = []
|
83
|
-
en.each do |k,v|
|
87
|
+
en.each do |k, v|
|
84
88
|
r.push(v) if k.downcase.include?(value.downcase)
|
85
89
|
end
|
86
90
|
r
|
87
91
|
end
|
88
92
|
|
93
|
+
def format_string_element(str)
|
94
|
+
|
95
|
+
match_number = str.match(/^(\d+(?:\.\d+)?)$/)
|
96
|
+
match_range = str.match(/^(\d+(?:\.\d+)?)(\.\.)(\d+(?:\.\d+)?)$/)
|
97
|
+
|
98
|
+
if match_range
|
99
|
+
mr = match_range.to_s
|
100
|
+
{
|
101
|
+
float_from: mr.split('..').first.to_f,
|
102
|
+
float_until: mr.split('..').last.to_f,
|
103
|
+
}
|
104
|
+
elsif match_number
|
105
|
+
mn = match_number.to_s
|
106
|
+
{
|
107
|
+
float_from: mn,
|
108
|
+
float_until: mn,
|
109
|
+
}
|
110
|
+
else
|
111
|
+
{
|
112
|
+
string: "%#{str}%"
|
113
|
+
}
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def create_arel(schema_column, search_string, association = nil)
|
118
|
+
|
119
|
+
base_object = association&.klass || self
|
120
|
+
type = base_object.columns_hash[schema_column.to_s].type
|
121
|
+
arel = base_object.arel_table[schema_column]
|
122
|
+
|
123
|
+
if search_string[:float_from]
|
124
|
+
if [:float, :big_decimal, :integer].include?(type)
|
125
|
+
arel.between(search_string[:float_from]..search_string[:float_until])
|
126
|
+
end
|
127
|
+
else
|
128
|
+
arel.matches(search_string[:string])
|
129
|
+
end
|
130
|
+
end
|
89
131
|
end
|
90
132
|
end
|
data/lib/like_query/version.rb
CHANGED
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.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- christian
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-11-
|
11
|
+
date: 2024-11-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|