flexi-json 0.4.1 → 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: 6d23543bb36512b335987eb6c12815956d98b170f2148fbbf12b92454064a96c
4
- data.tar.gz: 8872ba4b925901cdeee8dcf27e5a409ca3784366dcf64ad370e60cdfe919bc54
3
+ metadata.gz: 067cc7f0c8adc0d54bc8910ff6e67425c2c14c4f4bf49051e12f0ce103b31abf
4
+ data.tar.gz: 555b479c6e2662d3a5a78378c933bd2f4685f7d04d7ce1134301258a557b27c3
5
5
  SHA512:
6
- metadata.gz: 7acf01abdf37decc0f5d5d8a230da8b91f542d0dbb6c81efb90d5f229a1245b58764c0ba05fe0aa59b72348e3ef26b3b5484fc12f8700b108a49f3a0a4481c74
7
- data.tar.gz: b6d8d236da0e37f0a9fd8a3785403363f5fc783820ea1ba351d7cf2647ec4f5666b24928ccd68a2eac6e0be94d4a41e707deb9acc948402acb8f91d4050a28dd
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
- options = {matched_all: true, exact_match: false}
57
- 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
+ )
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
 
@@ -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.1"
5
+ VERSION = "0.4.2"
6
6
  end
7
7
  end
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)
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.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerda Decio