flexi-json 0.4.0 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -0
- data/README.md +13 -9
- 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 +7 -12
- 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,16 @@
|
|
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
|
+
|
5
|
+
# Version: 0.4.1 (22-09-2024)
|
6
|
+
|
7
|
+
* [0270c44](https://github.com/GD-Personal/flexi-json/commit/0270c44d7ffabd88ed0b69b09f1e98910257d99b): Update the readme and changelog
|
8
|
+
|
9
|
+
|
10
|
+
# Version: 0.4.0 - 2024-09-22
|
11
|
+
|
12
|
+
* [#15](https://github.com/GD-Personal/flexi-json/pull/15): Add a configuration and renamed matched_all to match_all.
|
13
|
+
|
1
14
|
# Version: 0.3.0 - 2024-09-22
|
2
15
|
|
3
16
|
* [#10](https://github.com/GD-Personal/flexi-json/pull/10): Release 0.2.0
|
data/README.md
CHANGED
@@ -28,18 +28,19 @@ bundle install
|
|
28
28
|
```ruby
|
29
29
|
require 'flexi/json'
|
30
30
|
|
31
|
-
# Load a json data from a local file
|
32
|
-
flexi_json = Flexi::Json::Run.new("some/path/to/file.json")
|
33
|
-
|
34
31
|
# Load a raw json data
|
35
|
-
flexi_json = Flexi::Json
|
36
|
-
# => <Flexi::Json
|
32
|
+
flexi_json = Flexi::Json.new("{\"name\":\"John\",\"address\":\"Sydney Australia\"}")
|
33
|
+
# => <Flexi::Json:0x0000ffffa0f5fc58 @datasets=[#<Flexi::Json::Dataset:0x0000ffffa0f5f668 @address="Sydney Australia", @attributes={:name=>"John", :address=>"Sydney Australia"}, @name="John", @searchable_fields=["name", "address"]>]>
|
37
34
|
|
38
|
-
# Load a json data from
|
39
|
-
flexi_json = Flexi::Json
|
35
|
+
# Load a json data from a local file
|
36
|
+
flexi_json = Flexi::Json.new("some/path/to/file.json")
|
37
|
+
|
38
|
+
# Load a json data from a url
|
39
|
+
flexi_json = Flexi::Json.new("https://raw.githubusercontent.com/GD-Personal/flexi-json/main/spec/data/dataset.json")
|
40
40
|
|
41
41
|
# Search for data
|
42
42
|
flexi_json.search("john")
|
43
|
+
# => [#<Flexi::Json::Dataset:0x0000ffffa0f5f668 @address="Sydney Australia", @attributes={:name=>"John", :address=>"Sydney Australia"}, @name="John", @searchable_fields=["name", "address"]>]
|
43
44
|
|
44
45
|
# Or filter it by your chosen key e.g first_name
|
45
46
|
flexi_json.search("john", "first_name")
|
@@ -52,8 +53,11 @@ flexi_json.find_duplicates("email,full_name")
|
|
52
53
|
|
53
54
|
## Advanced search
|
54
55
|
```ruby
|
55
|
-
|
56
|
-
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
|
+
)
|
57
61
|
# => [#<Flexi::Json::Dataset:0x0000ffffa0f5f668 @address="Sydney Australia", @attributes={:name=>"John", :address=>"Sydney Australia"}, @name="John", @searchable_fields=["name", "address"]>]
|
58
62
|
```
|
59
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,30 +10,25 @@ 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
|
22
|
-
end
|
23
20
|
|
24
|
-
|
25
|
-
# Your code goes here...
|
26
|
-
def initialize(data)
|
21
|
+
def new(data)
|
27
22
|
datasets = Flexi::Json::Loader.new(data).load_data
|
28
23
|
@searcher = Flexi::Json::Searcher.new(datasets)
|
29
24
|
end
|
25
|
+
end
|
30
26
|
|
31
|
-
|
32
|
-
|
33
|
-
|
27
|
+
def search(query = "", fields = nil, options: nil)
|
28
|
+
@searcher.search(query, fields, options: options)
|
29
|
+
end
|
34
30
|
|
35
|
-
|
36
|
-
|
37
|
-
end
|
31
|
+
def find_duplicates(keys)
|
32
|
+
@searcher.find_duplicates(keys)
|
38
33
|
end
|
39
34
|
end
|