Class: SearchJsonData::DataArray
- Inherits:
-
Object
- Object
- SearchJsonData::DataArray
- Defined in:
- lib/search_json_data/data_array.rb
Overview
Instance Attribute Summary collapse
-
#data_array ⇒ Object
readonly
Returns the value of attribute data_array.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
Instance Method Summary collapse
-
#clean_results ⇒ Object
to clean the result collection.
-
#initialize(file_path = File.join(File.dirname(__FILE__),'data.json')) ⇒ DataArray
constructor
read the Json file and dump to array.
-
#search_by(words, field = nil, condition = nil, precision = false) ⇒ Array
search words in a collection.
Constructor Details
#initialize(file_path = File.join(File.dirname(__FILE__),'data.json')) ⇒ DataArray
read the Json file and dump to array
8 9 10 11 |
# File 'lib/search_json_data/data_array.rb', line 8 def initialize(file_path = File.join(File.dirname(__FILE__),'data.json')) @data_array= JSON.parse File.read(file_path) @results = [] end |
Instance Attribute Details
#data_array ⇒ Object (readonly)
Returns the value of attribute data_array.
4 5 6 |
# File 'lib/search_json_data/data_array.rb', line 4 def data_array @data_array end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
4 5 6 |
# File 'lib/search_json_data/data_array.rb', line 4 def results @results end |
Instance Method Details
#clean_results ⇒ Object
to clean the result collection
74 75 76 |
# File 'lib/search_json_data/data_array.rb', line 74 def clean_results @results = [] end |
#search_by(words, field = nil, condition = nil, precision = false) ⇒ Array
search words in a collection
otherwise AND return only the results in both searches
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/search_json_data/data_array.rb', line 21 def search_by(words, field = nil, condition = nil, precision = false) # to include negative searchs negative = words.scan(/-.*?$/).first # erase the negative argument words = words.sub(negative,'') if negative # when the search have exactly presition search quoted text phrases =words.split(/\"/) # exactly phrases are quoted exactly_phrases = [] words.scan(/\".*?\"/).each {|exactly| exactly_phrases << exactly.tr('"','') } # non exactly prhases the difference of non_exactly_phrases = phrases - exactly_phrases # array of all words to match words_to_match = non_exactly_phrases.join(' ').split(' ').uniq results = [] self.data_array.each do |data_hash| data_for_search = data_hash # if field is not nil the data_for_search has only the field data_for_search = data_hash.select { |k,v| k == field } if field # if match one or more words match = is_match words_to_match, data_for_search, nil, precision # if match one or more exactly phrases match = match & (is_match exactly_phrases, data_for_search, nil, precision) # when match is true add the data_hash to the results results << data_hash if match end if condition == "AND" # if has contition AND only considate the values in both collections @results = results.uniq & @results elsif condition == "-" # when condition is "-" rest to the previos matches the finded values @results = @results - results.uniq else # add all matching values in this search to @result instance collection @results = @results | results.uniq end # when the phrase has a negative value search for this value self.search_by(negative[1 .. -1],field,"-", precision) if negative and negative.length > 1 @results end |