Class: SearchJsonData::DataArray

Inherits:
Object
  • Object
show all
Defined in:
lib/search_json_data/data_array.rb

Overview

Author:

  • Diego Piccinini Lagos

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path = File.join(File.dirname(__FILE__),'data.json')) ⇒ DataArray

read the Json file and dump to array

Parameters:

  • file_path (Pathname) (defaults to: File.join(File.dirname(__FILE__),'data.json'))

    path to a json file



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_arrayObject (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

#resultsObject (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_resultsObject

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

Parameters:

  • words (String)

    words to search, when are quoted must match exactly

  • field (String) (defaults to: nil)

    the name of the field, is nil by default in this case search in all fields

  • condition (String, nil) (defaults to: nil)

    the condition to search, by default nil to add results to the before search,

  • precision (Boolean, false) (defaults to: false)

    false by default, true is case sensitive

Returns:

  • (Array)

    collection of matching results



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