search_json_data 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ed8656f42813d4c61e50c4b5777681805b56080a
4
+ data.tar.gz: 2be2f7dbc2a12e04e7b1a92552195fb237de85ff
5
+ SHA512:
6
+ metadata.gz: 7182e74cbe2bb74d9443fd24ab5b6f8136ebc2d45db0125899cf64c097e6593c741410b8fd5394f34801034ba5364313e236fba2757811b18f167635a56b7f71
7
+ data.tar.gz: efbd6dd33693bc4bdd38638747ed414919bf6a64be95220d74f53eb78b9f0ab3ea6dba42b9c38b798f77208a1a34fcdd3dfa196845df5ead81984828641a3c4c
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in search_json_data.gemspec
4
+ gemspec
5
+ gem 'shoulda-matchers'
6
+ gem 'cucumber'
7
+ gem 'rspec-expectations'
8
+ gem 'yard'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Diego Hernán Piccinini Lagos
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,121 @@
1
+ # SearchJsonData
2
+
3
+ ###Minimum requirements:
4
+
5
+ A search for Lisp Common should match a programming language named "Common Lisp"
6
+ Your solution will be tested on a server running Ubuntu, Ruby 2.1 and Rails 4.2
7
+
8
+ ###Meriting:
9
+
10
+ Writing code with reusability in mind
11
+ Search match precision
12
+ Search results ordered by relevance
13
+ Support for exact matches, eg. Interpreted "Thomas Eugene", which should match "BASIC", but not "Haskell"
14
+ Match in different fields, eg. Scripting Microsoft should return all scripting languages designed by "Microsoft"
15
+ Support for negative searches, eg. john -array, which should match "BASIC", "Haskell", "Lisp" and "S-Lang", but not "Chapel", "Fortran" or "S".
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'search_json_data'
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install search_json_data
32
+
33
+ ## Usage
34
+ After install the gem include in your code this require
35
+ ```ruby
36
+ require 'search_json_data'
37
+ ```
38
+
39
+ Then you can instanciate the object
40
+ ```ruby
41
+ # the argument is the path to the json file
42
+ @data = DataArray.new /path/to/file/data.json
43
+
44
+ # then you can search words in the collection
45
+ #
46
+ # @param words [String] words to search, when are quoted must match exactly
47
+ # @param field [String] the name of the field, is nil by default in this case search in all fields
48
+ # @param condition [String, nil] the condition to search, by default nil to add results to the before search,
49
+ # otherwise AND return only the results in both searches
50
+ # @param precision [Boolean, false] false by default, true is case sensitive
51
+ # @return [Array] collection of matching results
52
+ @data.search_by "Scripting Microsoft"
53
+ =begin
54
+ return this collection all haches that match with Scripting and Microsoft
55
+ [{"Name"=>"ActionScript", "Type"=>"Compiled, Curly-bracket, Procedural, Reflective, Scripting, Object-oriented class-based", "Designed by"=>"Gary Grossman"}, {"Name"=>"AppleScript", "Type"=>"Scripting", "Designed by"=>"Apple Inc."}, {"Name"=>"AWK", "Type"=>"Curly-bracket, Scripting", "Designed by"=>"Alfred Aho, Peter Weinberger, Brian Kernighan"}, {"Name"=>"bash", "Type"=>"Command line interface, Scripting", "Designed by"=>"Brian Fox"}, {"Name"=>"C#", "Type"=>"Compiled, Curly-bracket, Iterative, Object-oriented class-based, Reflective, Procedural", "Designed by"=>"Microsoft"}, {"Name"=>"Candle", "Type"=>"Curly-bracket, Scripting", "Designed by"=>"Henry Luo"}, {"Name"=>"ColdFusion", "Type"=>"Object-oriented class-based, Procedural, Scripting", "Designed by"=>"Adobe Systems"}, {"Name"=>"ECMAScript", "Type"=>"Curly-bracket, Procedural, Reflective, Scripting", "Designed by"=>"Brendan Eich, Ecma International"}, {"Name"=>"F#", "Type"=>"Interactive mode", "Designed by"=>"Microsoft Research, Don Syme"}, {"Name"=>"Fancy", "Type"=>"Compiled, Interactive mode, Metaprogramming, Object-oriented class-based, Scripting", "Designed by"=>"Christopher Bertels"}, {"Name"=>"Groovy", "Type"=>"Compiled, Curly-bracket, Interpreted, Metaprogramming, Object-oriented class-based, Procedural, Reflective, Scripting", "Designed by"=>"James Strachan"}, {"Name"=>"JavaScript", "Type"=>"Curly-bracket, Interpreted, Reflective, Procedural, Scripting, Interactive mode", "Designed by"=>"Brendan Eich"}, {"Name"=>"JScript", "Type"=>"Curly-bracket, Procedural, Reflective, Scripting", "Designed by"=>"Microsoft"}, {"Name"=>"Julia", "Type"=>"Array, Imperative, Interactive mode, Interpreted, Metaprogramming, Object-oriented class-based, Reflective, Scripting", "Designed by"=>"Jeff Bezanson, Stefan Karpinski, Viral B. Shah, Alan Edelman"}, {"Name"=>"Lasso", "Type"=>"Procedural, Scripting, Object-oriented class-based", "Designed by"=>"Kyle Jessup"}, {"Name"=>"Lua", "Type"=>"Imperative, Interactive mode, Interpreted, Iterative, Metaprogramming, Object-oriented class-based, Reflective, Scripting", "Designed by"=>"Roberto Ierusalimschy, Waldemar Celes, Luiz Henrique de Figueiredo"}, {"Name"=>"Obix", "Type"=>"Compiled, Interactive mode, Object-oriented class-based, Procedural, Reflective, Scripting", "Designed by"=>"Christian Neumanns"}, {"Name"=>"Perl", "Type"=>"Curly-bracket, Imperative, Interactive mode, Interpreted, Metaprogramming, Procedural, Reflective, Scripting, Object-oriented class-based", "Designed by"=>"Larry Wall"}, {"Name"=>"PHP", "Type"=>"Curly-bracket, Imperative, Interpreted, Object-oriented class-based, Reflective, Scripting", "Designed by"=>"Rasmus Lerdorf"}, {"Name"=>"Python", "Type"=>"Imperative, Interactive mode, Interpreted, Iterative, Metaprogramming, Procedural, Reflective, Scripting, Object-oriented class-based", "Designed by"=>"Guido van Rossum"}, {"Name"=>"R", "Type"=>"Curly-bracket, Interactive mode, Interpreted, Procedural, Scripting", "Designed by"=>"Ross Ihaka, Robert Gentleman"}, {"Name"=>"REXX", "Type"=>"Command line interface, Interactive mode, Interpreted, Scripting", "Designed by"=>"Mike Cowlishaw"}, {"Name"=>"Ruby", "Type"=>"Imperative, Interpreted, Metaprogramming, Object-oriented class-based, Reflective, Scripting, Interactive mode", "Designed by"=>"Yukihiro Matsumoto"}, {"Name"=>"S-Lang", "Type"=>"Curly-bracket, Interpreted, Procedural, Scripting, Interactive mode", "Designed by"=>"John E. Davis"}, {"Name"=>"Smalltalk", "Type"=>"Compiled, Metaprogramming, Scripting, Interactive mode, Object-oriented class-based, Reflective", "Designed by"=>"Alan Kay, Dan Ingalls, Adele Goldberg"}, {"Name"=>"VBScript", "Type"=>"Interpreted, Procedural, Scripting, Object-oriented class-based", "Designed by"=>"Microsoft"}, {"Name"=>"Visual Basic", "Type"=>"Compiled, Procedural", "Designed by"=>"Microsoft"}, {"Name"=>"Visual FoxPro", "Type"=>"Compiled, Data-oriented, Object-oriented class-based, Procedural", "Designed by"=>"Microsoft"}, {"Name"=>"Windows PowerShell", "Type"=>"Command line interface, Curly-bracket, Interactive mode, Interpreted, Scripting", "Designed by"=>"Microsoft"}, {"Name"=>"X++", "Type"=>"Compiled, Object-oriented class-based, Procedural, Reflective", "Designed by"=>"Microsoft"}]
56
+ =end
57
+ # to clean the results
58
+ @data.clean_results
59
+
60
+ # you can do a new search
61
+ @data.search_by "Scripting", "Type"
62
+
63
+
64
+ =begin
65
+ return this collection
66
+ => [{"Name"=>"ActionScript", "Type"=>"Compiled, Curly-bracket, Procedural, Reflective, Scripting, Object-oriented class-based", "Designed by"=>"Gary Grossman"}, {"Name"=>"AppleScript", "Type"=>"Scripting", "Designed by"=>"Apple Inc."}, {"Name"=>"AWK", "Type"=>"Curly-bracket, Scripting", "Designed by"=>"Alfred Aho, Peter Weinberger, Brian Kernighan"}, {"Name"=>"bash", "Type"=>"Command line interface, Scripting", "Designed by"=>"Brian Fox"}, {"Name"=>"Candle", "Type"=>"Curly-bracket, Scripting", "Designed by"=>"Henry Luo"}, {"Name"=>"ColdFusion", "Type"=>"Object-oriented class-based, Procedural, Scripting", "Designed by"=>"Adobe Systems"}, {"Name"=>"ECMAScript", "Type"=>"Curly-bracket, Procedural, Reflective, Scripting", "Designed by"=>"Brendan Eich, Ecma International"}, {"Name"=>"Fancy", "Type"=>"Compiled, Interactive mode, Metaprogramming, Object-oriented class-based, Scripting", "Designed by"=>"Christopher Bertels"}, {"Name"=>"Groovy", "Type"=>"Compiled, Curly-bracket, Interpreted, Metaprogramming, Object-oriented class-based, Procedural, Reflective, Scripting", "Designed by"=>"James Strachan"}, {"Name"=>"JavaScript", "Type"=>"Curly-bracket, Interpreted, Reflective, Procedural, Scripting, Interactive mode", "Designed by"=>"Brendan Eich"}, {"Name"=>"JScript", "Type"=>"Curly-bracket, Procedural, Reflective, Scripting", "Designed by"=>"Microsoft"}, {"Name"=>"Julia", "Type"=>"Array, Imperative, Interactive mode, Interpreted, Metaprogramming, Object-oriented class-based, Reflective, Scripting", "Designed by"=>"Jeff Bezanson, Stefan Karpinski, Viral B. Shah, Alan Edelman"}, {"Name"=>"Lasso", "Type"=>"Procedural, Scripting, Object-oriented class-based", "Designed by"=>"Kyle Jessup"}, {"Name"=>"Lua", "Type"=>"Imperative, Interactive mode, Interpreted, Iterative, Metaprogramming, Object-oriented class-based, Reflective, Scripting", "Designed by"=>"Roberto Ierusalimschy, Waldemar Celes, Luiz Henrique de Figueiredo"}, {"Name"=>"Obix", "Type"=>"Compiled, Interactive mode, Object-oriented class-based, Procedural, Reflective, Scripting", "Designed by"=>"Christian Neumanns"}, {"Name"=>"Perl", "Type"=>"Curly-bracket, Imperative, Interactive mode, Interpreted, Metaprogramming, Procedural, Reflective, Scripting, Object-oriented class-based", "Designed by"=>"Larry Wall"}, {"Name"=>"PHP", "Type"=>"Curly-bracket, Imperative, Interpreted, Object-oriented class-based, Reflective, Scripting", "Designed by"=>"Rasmus Lerdorf"}, {"Name"=>"Python", "Type"=>"Imperative, Interactive mode, Interpreted, Iterative, Metaprogramming, Procedural, Reflective, Scripting, Object-oriented class-based", "Designed by"=>"Guido van Rossum"}, {"Name"=>"R", "Type"=>"Curly-bracket, Interactive mode, Interpreted, Procedural, Scripting", "Designed by"=>"Ross Ihaka, Robert Gentleman"}, {"Name"=>"REXX", "Type"=>"Command line interface, Interactive mode, Interpreted, Scripting", "Designed by"=>"Mike Cowlishaw"}, {"Name"=>"Ruby", "Type"=>"Imperative, Interpreted, Metaprogramming, Object-oriented class-based, Reflective, Scripting, Interactive mode", "Designed by"=>"Yukihiro Matsumoto"}, {"Name"=>"S-Lang", "Type"=>"Curly-bracket, Interpreted, Procedural, Scripting, Interactive mode", "Designed by"=>"John E. Davis"}, {"Name"=>"Smalltalk", "Type"=>"Compiled, Metaprogramming, Scripting, Interactive mode, Object-oriented class-based, Reflective", "Designed by"=>"Alan Kay, Dan Ingalls, Adele Goldberg"}, {"Name"=>"VBScript", "Type"=>"Interpreted, Procedural, Scripting, Object-oriented class-based", "Designed by"=>"Microsoft"}, {"Name"=>"Windows PowerShell", "Type"=>"Command line interface, Curly-bracket, Interactive mode, Interpreted, Scripting", "Designed by"=>"Microsoft"}]
67
+
68
+ =end
69
+
70
+ # but if you seach now with condition AND
71
+ @data.search-by "Microsoft", "Designed by", "AND"
72
+ =begin
73
+ you will get this results
74
+ => [{"Name"=>"JScript", "Type"=>"Curly-bracket, Procedural, Reflective, Scripting", "Designed by"=>"Microsoft"}, {"Name"=>"VBScript", "Type"=>"Interpreted, Procedural, Scripting, Object-oriented class-based", "Designed by"=>"Microsoft"}, {"Name"=>"Windows PowerShell", "Type"=>"Command line interface, Curly-bracket, Interactive mode, Interpreted, Scripting", "Designed by"=>"Microsoft"}]
75
+ =end
76
+
77
+ # you can add another result
78
+ @data.search_by "Apple", "Designed by"
79
+ =begin
80
+ you will get this results
81
+ => [{"Name"=>"JScript", "Type"=>"Curly-bracket, Procedural, Reflective, Scripting", "Designed by"=>"Microsoft"}, {"Name"=>"VBScript", "Type"=>"Interpreted, Procedural, Scripting, Object-oriented class-based", "Designed by"=>"Microsoft"}, {"Name"=>"Windows PowerShell", "Type"=>"Command line interface, Curly-bracket, Interactive mode, Interpreted, Scripting", "Designed by"=>"Microsoft"}, {"Name"=>"AppleScript", "Type"=>"Scripting", "Designed by"=>"Apple Inc."}, {"Name"=>"Delphi", "Type"=>"Compiled, Object-oriented class-based, Reflective", "Designed by"=>"Apple, Niklaus Wirth, Anders Hejlsberg"}, {"Name"=>"Swift", "Type"=>"Compiled", "Designed by"=>"Chris Lattner, Apple Inc."}]
82
+
83
+ # serching negative
84
+ @data.search_by "Scripting -Microsoft"
85
+ =begin
86
+ you will get all Scripting languages without Microsoft
87
+ => [{"Name"=>"ActionScript", "Type"=>"Compiled, Curly-bracket, Procedural, Reflective, Scripting, Object-oriented class-based", "Designed by"=>"Gary Grossman"}, {"Name"=>"AppleScript", "Type"=>"Scripting", "Designed by"=>"Apple Inc."}, {"Name"=>"AWK", "Type"=>"Curly-bracket, Scripting", "Designed by"=>"Alfred Aho, Peter Weinberger, Brian Kernighan"}, {"Name"=>"bash", "Type"=>"Command line interface, Scripting", "Designed by"=>"Brian Fox"}, {"Name"=>"Candle", "Type"=>"Curly-bracket, Scripting", "Designed by"=>"Henry Luo"}, {"Name"=>"ColdFusion", "Type"=>"Object-oriented class-based, Procedural, Scripting", "Designed by"=>"Adobe Systems"}, {"Name"=>"ECMAScript", "Type"=>"Curly-bracket, Procedural, Reflective, Scripting", "Designed by"=>"Brendan Eich, Ecma International"}, {"Name"=>"Fancy", "Type"=>"Compiled, Interactive mode, Metaprogramming, Object-oriented class-based, Scripting", "Designed by"=>"Christopher Bertels"}, {"Name"=>"Groovy", "Type"=>"Compiled, Curly-bracket, Interpreted, Metaprogramming, Object-oriented class-based, Procedural, Reflective, Scripting", "Designed by"=>"James Strachan"}, {"Name"=>"JavaScript", "Type"=>"Curly-bracket, Interpreted, Reflective, Procedural, Scripting, Interactive mode", "Designed by"=>"Brendan Eich"}, {"Name"=>"Julia", "Type"=>"Array, Imperative, Interactive mode, Interpreted, Metaprogramming, Object-oriented class-based, Reflective, Scripting", "Designed by"=>"Jeff Bezanson, Stefan Karpinski, Viral B. Shah, Alan Edelman"}, {"Name"=>"Lasso", "Type"=>"Procedural, Scripting, Object-oriented class-based", "Designed by"=>"Kyle Jessup"}, {"Name"=>"Lua", "Type"=>"Imperative, Interactive mode, Interpreted, Iterative, Metaprogramming, Object-oriented class-based, Reflective, Scripting", "Designed by"=>"Roberto Ierusalimschy, Waldemar Celes, Luiz Henrique de Figueiredo"}, {"Name"=>"Obix", "Type"=>"Compiled, Interactive mode, Object-oriented class-based, Procedural, Reflective, Scripting", "Designed by"=>"Christian Neumanns"}, {"Name"=>"Perl", "Type"=>"Curly-bracket, Imperative, Interactive mode, Interpreted, Metaprogramming, Procedural, Reflective, Scripting, Object-oriented class-based", "Designed by"=>"Larry Wall"}, {"Name"=>"PHP", "Type"=>"Curly-bracket, Imperative, Interpreted, Object-oriented class-based, Reflective, Scripting", "Designed by"=>"Rasmus Lerdorf"}, {"Name"=>"Python", "Type"=>"Imperative, Interactive mode, Interpreted, Iterative, Metaprogramming, Procedural, Reflective, Scripting, Object-oriented class-based", "Designed by"=>"Guido van Rossum"}, {"Name"=>"R", "Type"=>"Curly-bracket, Interactive mode, Interpreted, Procedural, Scripting", "Designed by"=>"Ross Ihaka, Robert Gentleman"}, {"Name"=>"REXX", "Type"=>"Command line interface, Interactive mode, Interpreted, Scripting", "Designed by"=>"Mike Cowlishaw"}, {"Name"=>"Ruby", "Type"=>"Imperative, Interpreted, Metaprogramming, Object-oriented class-based, Reflective, Scripting, Interactive mode", "Designed by"=>"Yukihiro Matsumoto"}, {"Name"=>"S-Lang", "Type"=>"Curly-bracket, Interpreted, Procedural, Scripting, Interactive mode", "Designed by"=>"John E. Davis"}, {"Name"=>"Smalltalk", "Type"=>"Compiled, Metaprogramming, Scripting, Interactive mode, Object-oriented class-based, Reflective", "Designed by"=>"Alan Kay, Dan Ingalls, Adele Goldberg"}]
88
+ =end
89
+
90
+ # Searching with case sensitive precision
91
+ @data.search_by "ruby", "Name",nil , true
92
+ # non results => []
93
+
94
+ # Searching with case sensitive precision (by default false)
95
+ @data.search_by "ruby", "Name"
96
+
97
+ # is the same of
98
+ @data.search_by "ruby", "Name",nil , false
99
+
100
+ =begin
101
+ you will get this:
102
+ => [{"Name"=>"Ruby", "Type"=>"Imperative, Interpreted, Metaprogramming, Object-oriented class-based, Reflective, Scripting, Interactive mode", "Designed by"=>"Yukihiro Matsumoto"}]
103
+ =end
104
+
105
+ ```
106
+
107
+
108
+ ## Testing
109
+
110
+ To run the test run cucumber
111
+ ```bash
112
+ >cucumber
113
+
114
+ ```
115
+
116
+ ## More Documentation
117
+ Run
118
+ ```bash
119
+ yard doc
120
+ ```
121
+ and open this file with your browser [doc/index.html](http://doc/index.html)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,10 @@
1
+ Feature: Initials Setting
2
+ Checking the valid settings
3
+ Scenario: show all data
4
+ Given A program require "search_json_data"
5
+ When the class DataArray is instantied
6
+ Then the method data_array return a Array
7
+ Scenario: show all data by argument
8
+ Given A program require "search_json_data"
9
+ When the class DataArray is instantied with "example.json"
10
+ Then the method data_array return a Array
@@ -0,0 +1,8 @@
1
+ Feature: Negative Searching
2
+ Get a result for searching excluding negative text or collection
3
+ Scenario: Return a result exluding negative expresion
4
+ Given A program require "search_json_data"
5
+ When the class DataArray is instantied
6
+ And A search for "john -array"
7
+ Then which should match "BASIC", "Haskell", "Lisp" and "S-Lang"
8
+ But not "Chapel", "Fortran" or "S".
@@ -0,0 +1,7 @@
1
+ Feature: Searching
2
+ Get a result for the searching
3
+ Scenario: return a result
4
+ Given A program require "search_json_data"
5
+ When the class DataArray is instantied
6
+ And A search for "Lisp Common"
7
+ Then should match a programming language named "Common Lisp"
@@ -0,0 +1,15 @@
1
+
2
+ Given(/^A program require "([^"]*)"$/) do |module_required|
3
+ require module_required
4
+ end
5
+
6
+ When(/^the class DataArray is instantied$/) do
7
+ @data = DataArray.new
8
+ end
9
+
10
+ Then(/^the method data_array return a Array$/) do
11
+ expect(@data.data_array).to be_a(Array)
12
+ end
13
+ When(/^the class DataArray is instantied with "([^"]*)"$/) do |file_name|
14
+ @data = DataArray.new File.join(File.dirname(__FILE__),'..','support',file_name)
15
+ end
@@ -0,0 +1,8 @@
1
+ Then(/^which should match "([^"]*)", "([^"]*)", "([^"]*)" and "([^"]*)"$/) do |arg1, arg2, arg3, arg4|
2
+ expect(@results).to include( include( "Name" => (match(arg1) or match(arg2) or match(arg3) or match(arg4)) ) )
3
+ end
4
+
5
+ Then(/^not "([^"]*)", "([^"]*)" or "([^"]*)"\.$/) do |arg1, arg2, arg3|
6
+ expect(@results).not_to include( include( "Name" => (match(arg1) or match(arg2) or match(arg3)) ) )
7
+ end
8
+
@@ -0,0 +1,8 @@
1
+ When(/^A search for "([^"]*)"$/) do |words|
2
+ @results=@data.search_by(words)
3
+ end
4
+
5
+ Then(/^should match a programming language named "([^"]*)"$/) do |finded|
6
+
7
+ expect(@results[0]).to include("Name" => finded)
8
+ end
@@ -0,0 +1,2 @@
1
+
2
+ require 'search_json_data'
@@ -0,0 +1,34 @@
1
+ When(/^Search interpreted "([^"]*)"$/) do |phrase|
2
+ @data = DataArray.new
3
+ @results=@data.search_by('"' + phrase + '"')
4
+ end
5
+
6
+ Then(/^which should match "([^"]*)"$/) do |finded|
7
+ expect(@results).to all( include("Name" => include(finded)))
8
+ end
9
+
10
+ Then(/^not "([^"]*)"$/) do |finded|
11
+ expect(@results).not_to include("Name" => include(finded))
12
+ end
13
+
14
+ When(/^Searching "([^"]*)" by field "([^"]*)"$/) do |phrase, field|
15
+ @data = DataArray.new
16
+ @data.search_by(phrase, field)
17
+ end
18
+
19
+ When(/^Searching "([^"]*)" by field "([^"]*)" with "([^"]*)" condition$/) do |phrase, field, condition|
20
+ @results=@data.search_by(phrase, field, condition)
21
+ end
22
+
23
+ Then(/^should return all "([^"]*)" languages$/) do |finded|
24
+ expect(@results).to all ( include("Type" => include(finded) ))
25
+ end
26
+
27
+ Then(/^designed by "([^"]*)"$/) do |finded|
28
+ expect(@results).to all (include("Designed by" => include(finded) ))
29
+ end
30
+
31
+ Then(/^have (\d+) results$/) do |total|
32
+ expect(@results.count).to be_equal total.to_i
33
+ end
34
+
@@ -0,0 +1,16 @@
1
+ When(/^Searching "([^"]*)" by field "([^"]*)" with precision$/) do |phrase, field|
2
+ @data = DataArray.new
3
+ @results=@data.search_by(phrase, field, nil, true)
4
+ end
5
+
6
+ Then(/^has not results$/) do
7
+ expect(@results.count).to be_equal 0
8
+ end
9
+ When(/^Searching "([^"]*)" by field "([^"]*)" without precision$/) do |phrase, field|
10
+ @data = DataArray.new
11
+ @results=@data.search_by(phrase, field)
12
+ end
13
+
14
+ Then(/^has results$/) do
15
+ expect(@results.count).to be > 0
16
+ end
@@ -0,0 +1,10 @@
1
+ [
2
+ {
3
+ "first" : "John",
4
+ "last" : "Smith"
5
+ },
6
+ {
7
+ "first" : "Jane",
8
+ "last" : "Doe"
9
+ }
10
+ ]
@@ -0,0 +1,16 @@
1
+ Feature: Support Exact Matches
2
+
3
+ Scenario: Exact Matches quoted text
4
+ Given A program require "search_json_data"
5
+ When Search interpreted "Thomas Eugene"
6
+ Then which should match "BASIC"
7
+ But not "Haskell"
8
+
9
+ Scenario: Match in different fields
10
+ Given A program require "search_json_data"
11
+ When Searching "Scripting" by field "Type"
12
+ And Searching "Microsoft" by field "Designed by" with "AND" condition
13
+ Then should return all "Scripting" languages
14
+ And designed by "Microsoft"
15
+ And have 3 results
16
+
@@ -0,0 +1,19 @@
1
+ Feature: Support Precision
2
+
3
+ Scenario: Search with Precision
4
+ The capital letter is a lowercase, so there are not results
5
+ Given A program require "search_json_data"
6
+ When Searching "scripting" by field "Type" with precision
7
+ Then has not results
8
+
9
+ Scenario: Search without Precision
10
+ The capital letter is a lowercase, so there are results
11
+ Given A program require "search_json_data"
12
+ When Searching "scripting" by field "Type" without precision
13
+ Then has results
14
+
15
+ Scenario: Search without Precision
16
+ The capital letter is a Uppercase, so there are results
17
+ Given A program require "search_json_data"
18
+ When Searching "Scripting" by field "Type" with precision
19
+ Then has results
@@ -0,0 +1,487 @@
1
+ [
2
+ {
3
+ "Name": "A+",
4
+ "Type": "Array",
5
+ "Designed by": "Arthur Whitney"
6
+ },
7
+ {
8
+ "Name": "ActionScript",
9
+ "Type": "Compiled, Curly-bracket, Procedural, Reflective, Scripting, Object-oriented class-based",
10
+ "Designed by": "Gary Grossman"
11
+ },
12
+ {
13
+ "Name": "Ada",
14
+ "Type": "Compiled, Imperative, Procedural, Object-oriented class-based",
15
+ "Designed by": "Tucker Taft, Jean Ichbiah"
16
+ },
17
+ {
18
+ "Name": "Aldor",
19
+ "Type": "Iterative",
20
+ "Designed by": "Richard Dimick Jenks, Barry Trager, Stephen Watt, James Davenport, Robert Sutor, Scott Morrison"
21
+ },
22
+ {
23
+ "Name": "Alef",
24
+ "Type": "Curly-bracket",
25
+ "Designed by": "Phil Winterbottom"
26
+ },
27
+ {
28
+ "Name": "ALGOL",
29
+ "Type": "Compiled, Imperative, Procedural",
30
+ "Designed by": "Bauer, Bottenbruch, Rutishauser, Samelson, Backus, Katz, Perlis, Wegstein, Naur, Vauquois, van Wijngaarden, Woodger, Green, McCarthy"
31
+ },
32
+ {
33
+ "Name": "Ant",
34
+ "Type": "Interpreted",
35
+ "Designed by": "Apache Software Foundation"
36
+ },
37
+ {
38
+ "Name": "APL",
39
+ "Type": "Array, Interactive mode, Interpreted",
40
+ "Designed by": "Kenneth E. Iverson"
41
+ },
42
+ {
43
+ "Name": "AppleScript",
44
+ "Type": "Scripting",
45
+ "Designed by": "Apple Inc."
46
+ },
47
+ {
48
+ "Name": "AWK",
49
+ "Type": "Curly-bracket, Scripting",
50
+ "Designed by": "Alfred Aho, Peter Weinberger, Brian Kernighan"
51
+ },
52
+ {
53
+ "Name": "B",
54
+ "Type": "Curly-bracket",
55
+ "Designed by": "Ken Thompson"
56
+ },
57
+ {
58
+ "Name": "bash",
59
+ "Type": "Command line interface, Scripting",
60
+ "Designed by": "Brian Fox"
61
+ },
62
+ {
63
+ "Name": "BASIC",
64
+ "Type": "Imperative, Compiled, Procedural, Interactive mode, Interpreted",
65
+ "Designed by": "John George Kemeny, Thomas Eugene Kurtz"
66
+ },
67
+ {
68
+ "Name": "BCPL",
69
+ "Type": "Compiled, Curly-bracket, Procedural",
70
+ "Designed by": "Martin Richards"
71
+ },
72
+ {
73
+ "Name": "Blue",
74
+ "Type": "Imperative, Object-oriented class-based, Procedural",
75
+ "Designed by": "University of Sydney"
76
+ },
77
+ {
78
+ "Name": "C",
79
+ "Type": "Compiled, Curly-bracket, Imperative, Procedural",
80
+ "Designed by": "Dennis Ritchie"
81
+ },
82
+ {
83
+ "Name": "C#",
84
+ "Type": "Compiled, Curly-bracket, Iterative, Object-oriented class-based, Reflective, Procedural",
85
+ "Designed by": "Microsoft"
86
+ },
87
+ {
88
+ "Name": "C++",
89
+ "Type": "Compiled, Curly-bracket, Imperative, Metaprogramming, Object-oriented class-based, Procedural",
90
+ "Designed by": "Bjarne Stroustrup"
91
+ },
92
+ {
93
+ "Name": "Candle",
94
+ "Type": "Curly-bracket, Scripting",
95
+ "Designed by": "Henry Luo"
96
+ },
97
+ {
98
+ "Name": "Chapel",
99
+ "Type": "Array",
100
+ "Designed by": "David Callahan, Hans Zima, Brad Chamberlain, John Plevyak"
101
+ },
102
+ {
103
+ "Name": "ChucK",
104
+ "Type": "Curly-bracket, Object-oriented class-based, Reflective, Procedural",
105
+ "Designed by": "Ge Wang"
106
+ },
107
+ {
108
+ "Name": "Cilk",
109
+ "Type": "Curly-bracket",
110
+ "Designed by": "MIT"
111
+ },
112
+ {
113
+ "Name": "Clojure",
114
+ "Type": "Interactive mode, Reflective",
115
+ "Designed by": "Rich Hickey"
116
+ },
117
+ {
118
+ "Name": "COBOL",
119
+ "Type": "Compiled, Imperative, Procedural",
120
+ "Designed by": "Grace Hopper, William Selden, Gertrude Tierney, Howard Bromberg, Howard Discount, Vernon Reeves, Jean E. Sammet"
121
+ },
122
+ {
123
+ "Name": "Cobra",
124
+ "Type": "Compiled, Iterative, Object-oriented class-based, Procedural, Reflective",
125
+ "Designed by": "Charles Esterbrook"
126
+ },
127
+ {
128
+ "Name": "ColdFusion",
129
+ "Type": "Object-oriented class-based, Procedural, Scripting",
130
+ "Designed by": "Adobe Systems"
131
+ },
132
+ {
133
+ "Name": "Common Lisp",
134
+ "Type": "Compiled, Interactive mode, Object-oriented class-based, Reflective",
135
+ "Designed by": "Scott Fahlman, Richard P. Gabriel, Dave Moon, Guy Steele, Dan Weinreb"
136
+ },
137
+ {
138
+ "Name": "csh",
139
+ "Type": "Command line interface",
140
+ "Designed by": "Bill Joy"
141
+ },
142
+ {
143
+ "Name": "Curl",
144
+ "Type": "Compiled, Metaprogramming, Object-oriented class-based, Procedural, Reflective",
145
+ "Designed by": "Steve Ward"
146
+ },
147
+ {
148
+ "Name": "Cyclone",
149
+ "Type": "Curly-bracket",
150
+ "Designed by": "AT&T Labs"
151
+ },
152
+ {
153
+ "Name": "D",
154
+ "Type": "Compiled, Curly-bracket, Imperative, Metaprogramming, Object-oriented class-based, Procedural",
155
+ "Designed by": "Walter Bright, Andrei Alexandrescu"
156
+ },
157
+ {
158
+ "Name": "DASL",
159
+ "Type": "Object-oriented class-based, Curly-bracket, Procedural",
160
+ "Designed by": "Sun Microsystems Laboratories"
161
+ },
162
+ {
163
+ "Name": "Delphi",
164
+ "Type": "Compiled, Object-oriented class-based, Reflective",
165
+ "Designed by": "Apple, Niklaus Wirth, Anders Hejlsberg"
166
+ },
167
+ {
168
+ "Name": "E",
169
+ "Type": "Curly-bracket, Object-oriented class-based",
170
+ "Designed by": "Mark S. Miller"
171
+ },
172
+ {
173
+ "Name": "ECMAScript",
174
+ "Type": "Curly-bracket, Procedural, Reflective, Scripting",
175
+ "Designed by": "Brendan Eich, Ecma International"
176
+ },
177
+ {
178
+ "Name": "Eiffel",
179
+ "Type": "Compiled, Object-oriented class-based, Procedural, Reflective",
180
+ "Designed by": "Bertrand Meyer"
181
+ },
182
+ {
183
+ "Name": "Erlang",
184
+ "Type": "Compiled, Interactive mode",
185
+ "Designed by": "Joe Armstrong"
186
+ },
187
+ {
188
+ "Name": "Expect",
189
+ "Type": "Command line interface",
190
+ "Designed by": "Don Libes"
191
+ },
192
+ {
193
+ "Name": "F#",
194
+ "Type": "Interactive mode",
195
+ "Designed by": "Microsoft Research, Don Syme"
196
+ },
197
+ {
198
+ "Name": "Factor",
199
+ "Type": "Compiled",
200
+ "Designed by": "Slava Pestov"
201
+ },
202
+ {
203
+ "Name": "Fancy",
204
+ "Type": "Compiled, Interactive mode, Metaprogramming, Object-oriented class-based, Scripting",
205
+ "Designed by": "Christopher Bertels"
206
+ },
207
+ {
208
+ "Name": "Forth",
209
+ "Type": "Compiled, Interactive mode, Metaprogramming, Reflective",
210
+ "Designed by": "Charles H. Moore"
211
+ },
212
+ {
213
+ "Name": "Fortran",
214
+ "Type": "Array, Compiled, Imperative, Procedural",
215
+ "Designed by": "John Backus"
216
+ },
217
+ {
218
+ "Name": "Go",
219
+ "Type": "Compiled, Imperative, Procedural",
220
+ "Designed by": "Robert Griesemer, Rob Pike, Ken Thompson"
221
+ },
222
+ {
223
+ "Name": "Gosu",
224
+ "Type": "Compiled",
225
+ "Designed by": "Guidewire Software"
226
+ },
227
+ {
228
+ "Name": "Groovy",
229
+ "Type": "Compiled, Curly-bracket, Interpreted, Metaprogramming, Object-oriented class-based, Procedural, Reflective, Scripting",
230
+ "Designed by": "James Strachan"
231
+ },
232
+ {
233
+ "Name": "Hamilton C shell",
234
+ "Type": "Command line interface",
235
+ "Designed by": "Nicole Hamilton"
236
+ },
237
+ {
238
+ "Name": "Harbour",
239
+ "Type": "Compiled, Object-oriented class-based, Procedural, Reflective",
240
+ "Designed by": "Antonio Linares"
241
+ },
242
+ {
243
+ "Name": "Haskell",
244
+ "Type": "Compiled, Functional, Metaprogramming, Interpreted, Interactive mode",
245
+ "Designed by": "Simon Peyton Jones, Lennart Augustsson, Dave Barton, Brian Boutel, Warren Burton, Joseph Fasel, Kevin Hammond, Ralf Hinze, Paul Hudak, John Hughes, Thomas Johnsson, Mark Jones, John Launchbury, Erik Meijer, John Peterson, Alastair Reid, Colin Runciman, Philip Wadler"
246
+ },
247
+ {
248
+ "Name": "IDL",
249
+ "Type": "Array, Interactive mode",
250
+ "Designed by": "David Stern"
251
+ },
252
+ {
253
+ "Name": "J",
254
+ "Type": "Array, Interactive mode, Interpreted, Object-oriented class-based",
255
+ "Designed by": "Ken Iverson, Roger Hui"
256
+ },
257
+ {
258
+ "Name": "Java",
259
+ "Type": "Compiled, Curly-bracket, Imperative, Object-oriented class-based, Procedural, Reflective",
260
+ "Designed by": "James Gosling, Sun Microsystems"
261
+ },
262
+ {
263
+ "Name": "JavaScript",
264
+ "Type": "Curly-bracket, Interpreted, Reflective, Procedural, Scripting, Interactive mode",
265
+ "Designed by": "Brendan Eich"
266
+ },
267
+ {
268
+ "Name": "JOVIAL",
269
+ "Type": "Compiled, Procedural",
270
+ "Designed by": "System Development Corporation"
271
+ },
272
+ {
273
+ "Name": "JScript",
274
+ "Type": "Curly-bracket, Procedural, Reflective, Scripting",
275
+ "Designed by": "Microsoft"
276
+ },
277
+ {
278
+ "Name": "Julia",
279
+ "Type": "Array, Imperative, Interactive mode, Interpreted, Metaprogramming, Object-oriented class-based, Reflective, Scripting",
280
+ "Designed by": "Jeff Bezanson, Stefan Karpinski, Viral B. Shah, Alan Edelman"
281
+ },
282
+ {
283
+ "Name": "K",
284
+ "Type": "Array",
285
+ "Designed by": "Arthur Whitney"
286
+ },
287
+ {
288
+ "Name": "ksh",
289
+ "Type": "Command line interface",
290
+ "Designed by": "David Korn"
291
+ },
292
+ {
293
+ "Name": "Lasso",
294
+ "Type": "Procedural, Scripting, Object-oriented class-based",
295
+ "Designed by": "Kyle Jessup"
296
+ },
297
+ {
298
+ "Name": "Limbo",
299
+ "Type": "Curly-bracket",
300
+ "Designed by": "Sean Dorward, Phil Winterbottom, Rob Pike"
301
+ },
302
+ {
303
+ "Name": "Lisp",
304
+ "Type": "Metaprogramming, Reflective",
305
+ "Designed by": "John McCarthy"
306
+ },
307
+ {
308
+ "Name": "Lua",
309
+ "Type": "Imperative, Interactive mode, Interpreted, Iterative, Metaprogramming, Object-oriented class-based, Reflective, Scripting",
310
+ "Designed by": "Roberto Ierusalimschy, Waldemar Celes, Luiz Henrique de Figueiredo"
311
+ },
312
+ {
313
+ "Name": "MATLAB",
314
+ "Type": "Array, Imperative, Interactive mode, Procedural",
315
+ "Designed by": "MathWorks"
316
+ },
317
+ {
318
+ "Name": "Mercury",
319
+ "Type": "Compiled, Functional",
320
+ "Designed by": "Zoltan Somogyi"
321
+ },
322
+ {
323
+ "Name": "ML",
324
+ "Type": "Compiled, Interactive mode",
325
+ "Designed by": "Robin Milner"
326
+ },
327
+ {
328
+ "Name": "Nemerle",
329
+ "Type": "Compiled, Curly-bracket, Metaprogramming, Object-oriented class-based, Procedural",
330
+ "Designed by": "Kamil Skalski, Michal Moskal, Prof. Leszek Pacholski, Pawel Olszta"
331
+ },
332
+ {
333
+ "Name": "Obix",
334
+ "Type": "Compiled, Interactive mode, Object-oriented class-based, Procedural, Reflective, Scripting",
335
+ "Designed by": "Christian Neumanns"
336
+ },
337
+ {
338
+ "Name": "Objective-C",
339
+ "Type": "Compiled, Reflective, Object-oriented class-based",
340
+ "Designed by": "Brad Cox, Tom Love"
341
+ },
342
+ {
343
+ "Name": "Pascal",
344
+ "Type": "Compiled, Imperative, Interpreted",
345
+ "Designed by": "Niklaus Wirth"
346
+ },
347
+ {
348
+ "Name": "Perl",
349
+ "Type": "Curly-bracket, Imperative, Interactive mode, Interpreted, Metaprogramming, Procedural, Reflective, Scripting, Object-oriented class-based",
350
+ "Designed by": "Larry Wall"
351
+ },
352
+ {
353
+ "Name": "PHP",
354
+ "Type": "Curly-bracket, Imperative, Interpreted, Object-oriented class-based, Reflective, Scripting",
355
+ "Designed by": "Rasmus Lerdorf"
356
+ },
357
+ {
358
+ "Name": "Python",
359
+ "Type": "Imperative, Interactive mode, Interpreted, Iterative, Metaprogramming, Procedural, Reflective, Scripting, Object-oriented class-based",
360
+ "Designed by": "Guido van Rossum"
361
+ },
362
+ {
363
+ "Name": "R",
364
+ "Type": "Curly-bracket, Interactive mode, Interpreted, Procedural, Scripting",
365
+ "Designed by": "Ross Ihaka, Robert Gentleman"
366
+ },
367
+ {
368
+ "Name": "REXX",
369
+ "Type": "Command line interface, Interactive mode, Interpreted, Scripting",
370
+ "Designed by": "Mike Cowlishaw"
371
+ },
372
+ {
373
+ "Name": "RPG",
374
+ "Type": "Compiled, Procedural",
375
+ "Designed by": "IBM"
376
+ },
377
+ {
378
+ "Name": "Ruby",
379
+ "Type": "Imperative, Interpreted, Metaprogramming, Object-oriented class-based, Reflective, Scripting, Interactive mode",
380
+ "Designed by": "Yukihiro Matsumoto"
381
+ },
382
+ {
383
+ "Name": "Rust",
384
+ "Type": "Compiled, Curly-bracket, Imperative, Metaprogramming, Procedural",
385
+ "Designed by": "Graydon Hoare"
386
+ },
387
+ {
388
+ "Name": "S",
389
+ "Type": "Array",
390
+ "Designed by": "Rick Becker, Allan Wilks, John Chambers"
391
+ },
392
+ {
393
+ "Name": "S-Lang",
394
+ "Type": "Curly-bracket, Interpreted, Procedural, Scripting, Interactive mode",
395
+ "Designed by": "John E. Davis"
396
+ },
397
+ {
398
+ "Name": "Scala",
399
+ "Type": "Curly-bracket, Interactive mode, Object-oriented class-based",
400
+ "Designed by": "Martin Odersky"
401
+ },
402
+ {
403
+ "Name": "Scheme",
404
+ "Type": "Compiled, Interactive mode, Metaprogramming, Reflective",
405
+ "Designed by": "Guy L. Steele, Gerald Jay Sussman"
406
+ },
407
+ {
408
+ "Name": "Smalltalk",
409
+ "Type": "Compiled, Metaprogramming, Scripting, Interactive mode, Object-oriented class-based, Reflective",
410
+ "Designed by": "Alan Kay, Dan Ingalls, Adele Goldberg"
411
+ },
412
+ {
413
+ "Name": "SQL",
414
+ "Type": "Data-oriented, Declarative, Extension",
415
+ "Designed by": "Donald D. Chamberlin, Raymond F. Boyce"
416
+ },
417
+ {
418
+ "Name": "Swift",
419
+ "Type": "Compiled",
420
+ "Designed by": "Chris Lattner, Apple Inc."
421
+ },
422
+ {
423
+ "Name": "Turing",
424
+ "Type": "Compiled",
425
+ "Designed by": "Ric Holt, James Cordy"
426
+ },
427
+ {
428
+ "Name": "TUTOR",
429
+ "Type": "Authoring",
430
+ "Designed by": "Paul Tenczar, Richard Blomme"
431
+ },
432
+ {
433
+ "Name": "Vala",
434
+ "Type": "Compiled",
435
+ "Designed by": "Jürg Billeter, Raffaele Sandrini"
436
+ },
437
+ {
438
+ "Name": "VBScript",
439
+ "Type": "Interpreted, Procedural, Scripting, Object-oriented class-based",
440
+ "Designed by": "Microsoft"
441
+ },
442
+ {
443
+ "Name": "Visual Basic",
444
+ "Type": "Compiled, Procedural",
445
+ "Designed by": "Microsoft"
446
+ },
447
+ {
448
+ "Name": "Visual FoxPro",
449
+ "Type": "Compiled, Data-oriented, Object-oriented class-based, Procedural",
450
+ "Designed by": "Microsoft"
451
+ },
452
+ {
453
+ "Name": "Windows PowerShell",
454
+ "Type": "Command line interface, Curly-bracket, Interactive mode, Interpreted, Scripting",
455
+ "Designed by": "Microsoft"
456
+ },
457
+ {
458
+ "Name": "X#",
459
+ "Type": "Compiled, Procedural",
460
+ "Designed by": "COSMOS"
461
+ },
462
+ {
463
+ "Name": "X++",
464
+ "Type": "Compiled, Object-oriented class-based, Procedural, Reflective",
465
+ "Designed by": "Microsoft"
466
+ },
467
+ {
468
+ "Name": "X10",
469
+ "Type": "Array, Curly-bracket, Object-oriented class-based, Reflective",
470
+ "Designed by": "Kemal Ebcioglu, Vijay Saraswat, Vivek Sarkar"
471
+ },
472
+ {
473
+ "Name": "XL",
474
+ "Type": "Compiled, Procedural, Reflective, Iterative, Metaprogramming",
475
+ "Designed by": "Christophe de Dinechin"
476
+ },
477
+ {
478
+ "Name": "ZPL",
479
+ "Type": "Array",
480
+ "Designed by": "Chamberlain"
481
+ },
482
+ {
483
+ "Name": "zsh",
484
+ "Type": "Command line interface",
485
+ "Designed by": "Paul Falstad"
486
+ }
487
+ ]
@@ -0,0 +1,103 @@
1
+ module SearchJsonData
2
+ # @author Diego Piccinini Lagos
3
+ class DataArray
4
+ attr_reader :data_array, :results
5
+
6
+ # read the Json file and dump to array
7
+ # @param file_path [Pathname] path to a json file
8
+ def initialize(file_path = File.join(File.dirname(__FILE__),'data.json'))
9
+ @data_array= JSON.parse File.read(file_path)
10
+ @results = []
11
+ end
12
+
13
+ # search words in a collection
14
+ #
15
+ # @param words [String] words to search, when are quoted must match exactly
16
+ # @param field [String] the name of the field, is nil by default in this case search in all fields
17
+ # @param condition [String, nil] the condition to search, by default nil to add results to the before search,
18
+ # otherwise AND return only the results in both searches
19
+ # @param precision [Boolean, false] false by default, true is case sensitive
20
+ # @return [Array] collection of matching results
21
+ def search_by(words, field = nil, condition = nil, precision = false)
22
+
23
+ # to include negative searchs
24
+ negative = words.scan(/-.*?$/).first
25
+ # erase the negative argument
26
+ words = words.sub(negative,'') if negative
27
+
28
+ # when the search have exactly presition search quoted text
29
+ phrases =words.split(/\"/)
30
+
31
+ # exactly phrases are quoted
32
+ exactly_phrases = []
33
+ words.scan(/\".*?\"/).each {|exactly| exactly_phrases << exactly.tr('"','') }
34
+
35
+ # non exactly prhases the difference of
36
+ non_exactly_phrases = phrases - exactly_phrases
37
+
38
+ # array of all words to match
39
+ words_to_match = non_exactly_phrases.join(' ').split(' ').uniq
40
+
41
+ results = []
42
+
43
+ self.data_array.each do |data_hash|
44
+
45
+ data_for_search = data_hash
46
+ # if field is not nil the data_for_search has only the field
47
+ data_for_search = data_hash.select { |k,v| k == field } if field
48
+
49
+ # if match one or more words
50
+ match = is_match words_to_match, data_for_search, nil, precision
51
+ # if match one or more exactly phrases
52
+ match = is_match exactly_phrases, data_for_search, nil, precision unless match
53
+
54
+ # when match is true add the data_hash to the results
55
+ results << data_hash if match
56
+ end
57
+
58
+ if condition == "AND"
59
+ # if has contition AND only considate the values in both collections
60
+ @results = results.uniq & @results
61
+ elsif condition == "-"
62
+ # when condition is "-" rest to the previos matches the finded values
63
+ @results = @results - results.uniq
64
+ else
65
+ # add all matching values in this search to @result instance collection
66
+ @results = @results | results.uniq
67
+ end
68
+ # when the phrase has a negative value search for this value
69
+ self.search_by(negative[1 .. -1],field,"-", precision) if negative and negative.length > 1
70
+ @results
71
+ end
72
+
73
+ # to clean the result collection
74
+ def clean_results
75
+ @results = []
76
+ end
77
+ end
78
+
79
+ private
80
+
81
+ # return true when one or more values include one or more words or phrases
82
+ # @param collection [Array] words or phrases to search
83
+ # @param data_hash [Hash] a hash that contain values to search in its
84
+ # @param condition [String, nil] condition for negatives matches
85
+ # @param precision [Boolean, false] false by default, true is case sensitive
86
+ # @return [Boolean] true is any value or phrase match, otherwise false
87
+ def is_match(collection, data_hash, condition = nil, precision = false)
88
+ match = false
89
+
90
+ collection.each do |text|
91
+ data_hash.each_value do |value|
92
+ if precision
93
+ match = true if value.include? text
94
+ else
95
+ match = true if value.downcase.include? text.downcase
96
+ end
97
+ # to exclude array in negative search
98
+ match = false if condition == "-" and value.include? ',' and text=='array'
99
+ end
100
+ end
101
+ match
102
+ end
103
+ end
@@ -0,0 +1,4 @@
1
+
2
+ module SearchJsonData
3
+ VERSION = "0.1.0"
4
+ end
@@ -0,0 +1,3 @@
1
+ Dir[File.join(File.dirname(__FILE__),'search_json_data','*.rb')].each {|file| require file }
2
+ require 'json'
3
+ include SearchJsonData
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'search_json_data/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "search_json_data"
8
+ spec.version = SearchJsonData::VERSION
9
+ spec.authors = ["Diego Hernán Piccinini Lagos"]
10
+ spec.email = ["diego@guiasrails.es"]
11
+ spec.summary = %q{Objective: Make this a json data searchable}
12
+ spec.description = %q{A search for Lisp Common should match a programming language named "Common Lisp"}
13
+ spec.homepage = "https://github.com/diegopiccinini/search_json_data"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "shoulda-matchers", "~> 2.8"
24
+ spec.add_development_dependency 'cucumber', "~> 2.0"
25
+ spec.add_development_dependency 'rspec-expectations', "~> 3.3"
26
+ spec.add_development_dependency 'yard', "~> 0.8.7"
27
+ end
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: search_json_data
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Diego Hernán Piccinini Lagos
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: shoulda-matchers
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: cucumber
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-expectations
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: yard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.8.7
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.8.7
97
+ description: A search for Lisp Common should match a programming language named "Common
98
+ Lisp"
99
+ email:
100
+ - diego@guiasrails.es
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - features/initial_settings.feature
111
+ - features/negative_search.feature
112
+ - features/searching.feature
113
+ - features/step_definitions/initial_settings_step.rb
114
+ - features/step_definitions/negative_search_step.rb
115
+ - features/step_definitions/searching_settings_step.rb
116
+ - features/step_definitions/step.rb
117
+ - features/step_definitions/support_exact_matches_step.rb
118
+ - features/step_definitions/support_precision_step.rb
119
+ - features/support/example.json
120
+ - features/support_exact_matches.feature
121
+ - features/support_precision.feature
122
+ - lib/search_json_data.rb
123
+ - lib/search_json_data/data.json
124
+ - lib/search_json_data/data_array.rb
125
+ - lib/search_json_data/version.rb
126
+ - search_json_data.gemspec
127
+ homepage: https://github.com/diegopiccinini/search_json_data
128
+ licenses:
129
+ - MIT
130
+ metadata: {}
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 2.4.3
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: 'Objective: Make this a json data searchable'
151
+ test_files:
152
+ - features/initial_settings.feature
153
+ - features/negative_search.feature
154
+ - features/searching.feature
155
+ - features/step_definitions/initial_settings_step.rb
156
+ - features/step_definitions/negative_search_step.rb
157
+ - features/step_definitions/searching_settings_step.rb
158
+ - features/step_definitions/step.rb
159
+ - features/step_definitions/support_exact_matches_step.rb
160
+ - features/step_definitions/support_precision_step.rb
161
+ - features/support/example.json
162
+ - features/support_exact_matches.feature
163
+ - features/support_precision.feature
164
+ has_rdoc: