flexi-json 0.4.1 → 0.4.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/CHANGELOG.md +4 -0
- data/README.md +5 -2
- data/lib/flexi/json/dataset.rb +20 -8
- data/lib/flexi/json/searcher.rb +2 -2
- data/lib/flexi/json/version.rb +1 -1
- data/lib/flexi/json.rb +2 -4
- 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: 067cc7f0c8adc0d54bc8910ff6e67425c2c14c4f4bf49051e12f0ce103b31abf
|
4
|
+
data.tar.gz: 555b479c6e2662d3a5a78378c933bd2f4685f7d04d7ce1134301258a557b27c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa132fb46b6ebf35818dce444ac95ba9648da13ba9dea057f5d3084c6a6daa6ac753afb79cef3863bd56da7cdd1741514a9d6c5de9df108475d44db69e110dc4
|
7
|
+
data.tar.gz: 0307bae70965e8e846fb6abfa5050d3065a9f0a8563117a44d228a1d54adf4f5d2d3f99aefa0daadf6ed31fe8927cdd0e2c757dab3f4f2baf2fae263532e3a1b
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
# Version: 0.4.2 (23-09-2024)
|
2
|
+
|
3
|
+
* [#25](https://github.com/GD-Personal/flexi-json/pull/25): A bug from [v0.4.0](https://github.com/GD-Personal/flexi-json/releases/tag/v0.4.0) was introduced where the advanced search function don't work properly when the options.
|
4
|
+
|
1
5
|
# Version: 0.4.1 (22-09-2024)
|
2
6
|
|
3
7
|
* [0270c44](https://github.com/GD-Personal/flexi-json/commit/0270c44d7ffabd88ed0b69b09f1e98910257d99b): Update the readme and changelog
|
data/README.md
CHANGED
@@ -53,8 +53,11 @@ flexi_json.find_duplicates("email,full_name")
|
|
53
53
|
|
54
54
|
## Advanced search
|
55
55
|
```ruby
|
56
|
-
|
57
|
-
flexi_json.search(
|
56
|
+
search_query = {first_name: "john", address: "sydney"}
|
57
|
+
flexi_json.search(
|
58
|
+
search_query,
|
59
|
+
options: {matched_all: true, exact_match: false}
|
60
|
+
)
|
58
61
|
# => [#<Flexi::Json::Dataset:0x0000ffffa0f5f668 @address="Sydney Australia", @attributes={:name=>"John", :address=>"Sydney Australia"}, @name="John", @searchable_fields=["name", "address"]>]
|
59
62
|
```
|
60
63
|
|
data/lib/flexi/json/dataset.rb
CHANGED
@@ -12,20 +12,22 @@ module Flexi
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
-
def matches?(query, fields =
|
16
|
-
|
17
|
-
|
15
|
+
def matches?(query, fields = nil, options: nil)
|
16
|
+
options ||= Flexi::Json::Configuration.default_match_options
|
17
|
+
|
18
|
+
valid_fields = validateable_fields(query, fields)&.select do |field|
|
19
|
+
searchable_fields.include?(field)
|
20
|
+
end || searchable_fields
|
18
21
|
|
19
22
|
return false if valid_fields.empty?
|
20
23
|
|
21
24
|
matching_method = options[:match_all] ? :all? : :any?
|
22
25
|
valid_fields.public_send(matching_method) do |field|
|
23
26
|
search_query = query.is_a?(Hash) ? query[field.to_sym] : query
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
end
|
27
|
+
attribute_value = attributes[field.to_sym].to_s.downcase
|
28
|
+
query_value = search_query.to_s.downcase
|
29
|
+
|
30
|
+
options[:exact_match] ? attribute_value == query_value : attribute_value.include?(query_value)
|
29
31
|
end
|
30
32
|
end
|
31
33
|
|
@@ -35,6 +37,16 @@ module Flexi
|
|
35
37
|
|
36
38
|
private
|
37
39
|
|
40
|
+
def validateable_fields(query, fields)
|
41
|
+
if query.is_a?(Hash)
|
42
|
+
query.keys.map(&:to_s)
|
43
|
+
elsif fields.is_a?(String)
|
44
|
+
fields.delete(" ").split(",")
|
45
|
+
elsif fields.is_a?(Array)
|
46
|
+
[fields || searchable_fields].flatten
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
38
50
|
# Method to validate that a key is a valid method name and not dangerous
|
39
51
|
def valid_key?(key)
|
40
52
|
key.match?(/\A[a-z_][a-zA-Z0-9_]*\z/) && !dangerous_method?(key)
|
data/lib/flexi/json/searcher.rb
CHANGED
@@ -10,8 +10,8 @@ module Flexi
|
|
10
10
|
@result = []
|
11
11
|
end
|
12
12
|
|
13
|
-
def search(query, fields = nil)
|
14
|
-
@result = @data.select { |data| data.matches?(query, fields) }
|
13
|
+
def search(query, fields = nil, options: nil)
|
14
|
+
@result = @data.select { |data| data.matches?(query, fields, options: options) }
|
15
15
|
end
|
16
16
|
|
17
17
|
def find_duplicates(keys)
|
data/lib/flexi/json/version.rb
CHANGED
data/lib/flexi/json.rb
CHANGED
@@ -10,12 +10,10 @@ module Flexi::Json
|
|
10
10
|
class << self
|
11
11
|
attr_writer :configuration
|
12
12
|
|
13
|
-
# Access or initialize the configuration object
|
14
13
|
def configuration
|
15
14
|
@configuration ||= Flexi::Json::Configuration.instance
|
16
15
|
end
|
17
16
|
|
18
|
-
# Configure block for setting custom configurations
|
19
17
|
def configure
|
20
18
|
yield(configuration)
|
21
19
|
end
|
@@ -26,8 +24,8 @@ module Flexi::Json
|
|
26
24
|
end
|
27
25
|
end
|
28
26
|
|
29
|
-
def search(query = "", fields = nil)
|
30
|
-
@searcher.search(query, fields)
|
27
|
+
def search(query = "", fields = nil, options: nil)
|
28
|
+
@searcher.search(query, fields, options: options)
|
31
29
|
end
|
32
30
|
|
33
31
|
def find_duplicates(keys)
|