flexi-json 0.3.0 → 0.4.1

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: b10a91e8b4e687fdd0c69e73b8a53d6f1e66e96361fe08259c7244aec87e638b
4
- data.tar.gz: 2f7a7dd6d9acc9069c3c1884ff98736041113889c8cd35efa5c94bd7f6acd48e
3
+ metadata.gz: 6d23543bb36512b335987eb6c12815956d98b170f2148fbbf12b92454064a96c
4
+ data.tar.gz: 8872ba4b925901cdeee8dcf27e5a409ca3784366dcf64ad370e60cdfe919bc54
5
5
  SHA512:
6
- metadata.gz: 1f793341d32d74087ce18d669f0df367e1503a081a4e227cb8dbda80e8e3a64fdc02b9bd7324b69738bde9b86aeb6f70c287ea75db0bd3accafff23ea08216b5
7
- data.tar.gz: dbd6b4bf1f222c4fb657ab346057ae975073397b7c5a4d84c32f0589b02e9c905be510ae6ea10c49b19515a312f19ef950bc64e0573268f80172ca185e8fa896
6
+ metadata.gz: 7acf01abdf37decc0f5d5d8a230da8b91f542d0dbb6c81efb90d5f229a1245b58764c0ba05fe0aa59b72348e3ef26b3b5484fc12f8700b108a49f3a0a4481c74
7
+ data.tar.gz: b6d8d236da0e37f0a9fd8a3785403363f5fc783820ea1ba351d7cf2647ec4f5666b24928ccd68a2eac6e0be94d4a41e707deb9acc948402acb8f91d4050a28dd
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ # Version: 0.4.1 (22-09-2024)
2
+
3
+ * [0270c44](https://github.com/GD-Personal/flexi-json/commit/0270c44d7ffabd88ed0b69b09f1e98910257d99b): Update the readme and changelog
4
+
5
+
6
+ # Version: 0.4.0 - 2024-09-22
7
+
8
+ * [#15](https://github.com/GD-Personal/flexi-json/pull/15): Add a configuration and renamed matched_all to match_all.
9
+
1
10
  # Version: 0.3.0 - 2024-09-22
2
11
 
3
12
  * [#10](https://github.com/GD-Personal/flexi-json/pull/10): Release 0.2.0
data/README.md CHANGED
@@ -1,4 +1,7 @@
1
- [![Gem Version](https://badge.fury.io/rb/flexi-json.svg)](https://badge.fury.io/rb/flexi-json) [![Github Actions](https://github.com/GD-Personal/flexi-json/actions/workflows/ci.yml/badge.svg)](https://github.com/GD-Personal/flexi-json/actions) [![Maintainability](https://api.codeclimate.com/v1/badges/bd14f8a5a0c7575d2ac2/maintainability)](https://codeclimate.com/github/GD-Personal/flexi-json/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/bd14f8a5a0c7575d2ac2/test_coverage)](https://codeclimate.com/github/GD-Personal/flexi-json/test_coverage)
1
+ [![Gem Version](https://img.shields.io/gem/v/flexi-json.svg)](https://rubygems.org/gems/flexi-json)
2
+ [![Github Actions](https://github.com/GD-Personal/flexi-json/actions/workflows/ci.yml/badge.svg)](https://github.com/GD-Personal/flexi-json/actions)
3
+ [![Maintainability](https://api.codeclimate.com/v1/badges/bd14f8a5a0c7575d2ac2/maintainability)](https://codeclimate.com/github/GD-Personal/flexi-json/maintainability)
4
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/bd14f8a5a0c7575d2ac2/test_coverage)](https://codeclimate.com/github/GD-Personal/flexi-json/test_coverage)
2
5
 
3
6
  # flexi-json
4
7
 
@@ -25,18 +28,19 @@ bundle install
25
28
  ```ruby
26
29
  require 'flexi/json'
27
30
 
28
- # Load a json data from a local file
29
- flexi_json = Flexi::Json::Run.new("some/path/to/file.json")
30
-
31
31
  # Load a raw json data
32
- flexi_json = Flexi::Json::Run.new("{\"name\":\"John\",\"address\":\"Sydney Australia\"}")
33
- # => <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"]>]>
34
+
35
+ # Load a json data from a local file
36
+ flexi_json = Flexi::Json.new("some/path/to/file.json")
34
37
 
35
- # Load a json data from aurl
36
- flexi_json = Flexi::Json::Run.new("https://raw.githubusercontent.com/GD-Personal/flexi-json/main/spec/data/dataset.json")
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")
37
40
 
38
41
  # Search for data
39
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"]>]
40
44
 
41
45
  # Or filter it by your chosen key e.g first_name
42
46
  flexi_json.search("john", "first_name")
@@ -54,8 +58,16 @@ flexi_json.search({first_name: "john", address: "sydney"}, options)
54
58
  # => [#<Flexi::Json::Dataset:0x0000ffffa0f5f668 @address="Sydney Australia", @attributes={:name=>"John", :address=>"Sydney Australia"}, @name="John", @searchable_fields=["name", "address"]>]
55
59
  ```
56
60
 
61
+ ## Configuration
62
+ ```ruby
63
+ Flexi::Json.configure do |config|
64
+ config.exact_match_search = true
65
+ config.match_all_fields = true
66
+ end
67
+ ```
68
+
57
69
  ## TODOS
58
- - Improve search filter by specifying fields to filter from
70
+ - Generate results in json, csv, txt, or output in the console
59
71
  - Add CRUD support to the dataset
60
72
  - Optimise the search function by implementing indeces
61
73
  - Optimise the loader by chunking the data
@@ -0,0 +1,26 @@
1
+ require "singleton"
2
+ module Flexi
3
+ module Json
4
+ class ConfigError < StandardError; end
5
+
6
+ class Configuration
7
+ include Singleton
8
+
9
+ attr_accessor :exact_match_search, :match_all_fields
10
+
11
+ class << self
12
+ def default_match_options
13
+ {
14
+ match_all: @match_all_fields || false,
15
+ exact_match: @exact_match_search || false
16
+ }.freeze
17
+ end
18
+ end
19
+
20
+ def initialize
21
+ @exact_match_search = false
22
+ @match_all_fields = false
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,4 +1,3 @@
1
- require "debug"
2
1
  module Flexi
3
2
  module Json
4
3
  class Dataset
@@ -13,13 +12,13 @@ module Flexi
13
12
  end
14
13
  end
15
14
 
16
- def matches?(query, fields = searchable_fields, options: Searcher::DEFAULT_MATCH_OPTIONS)
15
+ def matches?(query, fields = searchable_fields, options: Flexi::Json::Configuration.default_match_options)
17
16
  validateable_fields = query.is_a?(Hash) ? query.keys.map(&:to_s) : fields
18
17
  valid_fields = validateable_fields&.select { |field| searchable_fields.include?(field) } || searchable_fields
19
18
 
20
19
  return false if valid_fields.empty?
21
20
 
22
- matching_method = options[:matched_all] ? :all? : :any?
21
+ matching_method = options[:match_all] ? :all? : :any?
23
22
  valid_fields.public_send(matching_method) do |field|
24
23
  search_query = query.is_a?(Hash) ? query[field.to_sym] : query
25
24
  if options[:exact_match]
@@ -5,11 +5,6 @@ module Flexi
5
5
  class Searcher
6
6
  attr_reader :data, :result
7
7
 
8
- DEFAULT_MATCH_OPTIONS = {
9
- matched_all: false,
10
- exact_match: false
11
- }.freeze
12
-
13
8
  def initialize(data)
14
9
  @data = data
15
10
  @result = []
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Flexi
4
4
  module Json
5
- VERSION = "0.3.0"
5
+ VERSION = "0.4.1"
6
6
  end
7
7
  end
data/lib/flexi/json.rb CHANGED
@@ -1,30 +1,36 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "json/configuration"
3
4
  require_relative "json/dataset"
4
5
  require_relative "json/loader"
5
6
  require_relative "json/searcher"
6
7
  require_relative "json/version"
7
8
 
8
9
  module Flexi::Json
9
- class Error < StandardError; end
10
+ class << self
11
+ attr_writer :configuration
10
12
 
11
- class Run
12
- # Your code goes here...
13
- def initialize(data)
14
- datasets = Flexi::Json::Loader.new(data).load_data
15
- @searcher = Flexi::Json::Searcher.new(datasets)
13
+ # Access or initialize the configuration object
14
+ def configuration
15
+ @configuration ||= Flexi::Json::Configuration.instance
16
16
  end
17
17
 
18
- def search(query = "", fields = nil)
19
- @searcher.search(query, fields)
18
+ # Configure block for setting custom configurations
19
+ def configure
20
+ yield(configuration)
20
21
  end
21
22
 
22
- def find_duplicates(keys)
23
- @searcher.find_duplicates(keys)
23
+ def new(data)
24
+ datasets = Flexi::Json::Loader.new(data).load_data
25
+ @searcher = Flexi::Json::Searcher.new(datasets)
24
26
  end
27
+ end
25
28
 
26
- def display_results
27
- @searcher.display_results
28
- end
29
+ def search(query = "", fields = nil)
30
+ @searcher.search(query, fields)
31
+ end
32
+
33
+ def find_duplicates(keys)
34
+ @searcher.find_duplicates(keys)
29
35
  end
30
36
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flexi-json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerda Decio
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-09-21 00:00:00.000000000 Z
11
+ date: 2024-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -125,6 +125,7 @@ files:
125
125
  - Rakefile
126
126
  - changelog_config.yml
127
127
  - lib/flexi/json.rb
128
+ - lib/flexi/json/configuration.rb
128
129
  - lib/flexi/json/dataset.rb
129
130
  - lib/flexi/json/loader.rb
130
131
  - lib/flexi/json/searcher.rb
@@ -139,7 +140,7 @@ metadata:
139
140
  homepage_uri: https://github.com/GD-Personal/flexi-json
140
141
  source_code_uri: https://github.com/GD-Personal/flexi-json
141
142
  changelog_uri: https://github.com/GD-Personal/flexi-json/blob/main/CHANGELOG.md
142
- post_install_message:
143
+ post_install_message:
143
144
  rdoc_options: []
144
145
  require_paths:
145
146
  - lib
@@ -154,8 +155,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
154
155
  - !ruby/object:Gem::Version
155
156
  version: '0'
156
157
  requirements: []
157
- rubygems_version: 3.5.14
158
- signing_key:
158
+ rubygems_version: 3.5.16
159
+ signing_key:
159
160
  specification_version: 4
160
161
  summary: A ruby gem designed for manipulating JSON data.
161
162
  test_files: []