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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4e8c41b6f187468d64f2d6d3321f7557a5ec17410a69508b8cc1391560ab81be
4
- data.tar.gz: 5b7aeb733149ae14bce5481878566a99b4a971232900285153278f11d74decf8
3
+ metadata.gz: 067cc7f0c8adc0d54bc8910ff6e67425c2c14c4f4bf49051e12f0ce103b31abf
4
+ data.tar.gz: 555b479c6e2662d3a5a78378c933bd2f4685f7d04d7ce1134301258a557b27c3
5
5
  SHA512:
6
- metadata.gz: 717ae733016f7913d3ef4d2d8d065fbded43304ff3f62b45ede49abf1aed3de63144202fd17bd8e12e4fa0bde1b9c22067027a6f66f873201ce5f30434f9843d
7
- data.tar.gz: f08b3a2418807a5afce6e2b2c30db6e142829ff2c6fbe11968376f0612e8d6cc5a92315a8e98b377fb49c983d3229ca40ec0d3102982d488f0c4ff8e857d84ca
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::Run.new("{\"name\":\"John\",\"address\":\"Sydney Australia\"}")
36
- # => <Flexi::Json::Run:0x0000ffffa0f5fc58 @datasets=[#<Flexi::Json::Dataset:0x0000ffffa0f5f668 @address="Sydney Australia", @attributes={:name=>"John", :address=>"Sydney Australia"}, @name="John", @searchable_fields=["name", "address"]>]>
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 aurl
39
- flexi_json = Flexi::Json::Run.new("https://raw.githubusercontent.com/GD-Personal/flexi-json/main/spec/data/dataset.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
- options = {matched_all: true, exact_match: false}
56
- flexi_json.search({first_name: "john", address: "sydney"}, options)
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
 
@@ -12,20 +12,22 @@ module Flexi
12
12
  end
13
13
  end
14
14
 
15
- def matches?(query, fields = searchable_fields, options: Flexi::Json::Configuration.default_match_options)
16
- validateable_fields = query.is_a?(Hash) ? query.keys.map(&:to_s) : fields
17
- valid_fields = validateable_fields&.select { |field| searchable_fields.include?(field) } || searchable_fields
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
- if options[:exact_match]
25
- attributes[field.to_sym].to_s.downcase == search_query.to_s.downcase
26
- else
27
- attributes[field.to_sym].to_s.downcase.include?(search_query.to_s.downcase)
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)
@@ -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)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Flexi
4
4
  module Json
5
- VERSION = "0.4.0"
5
+ VERSION = "0.4.2"
6
6
  end
7
7
  end
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
- class Run
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
- def search(query = "", fields = nil)
32
- @searcher.search(query, fields)
33
- end
27
+ def search(query = "", fields = nil, options: nil)
28
+ @searcher.search(query, fields, options: options)
29
+ end
34
30
 
35
- def find_duplicates(keys)
36
- @searcher.find_duplicates(keys)
37
- end
31
+ def find_duplicates(keys)
32
+ @searcher.find_duplicates(keys)
38
33
  end
39
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flexi-json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerda Decio