Class: Jsonar::Indexer

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonar/indexer.rb

Class Method Summary collapse

Class Method Details

.from_files(files = []) ⇒ Object

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
# File 'lib/jsonar/indexer.rb', line 6

def self.from_files(files = [])
  raise ArgumentError if files.empty?
  index = {}
  files.each do |file|
    index = Jsonar::Indexer.from_string(File.read(file), index)
  end
  index
end

.from_string(json, index = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/jsonar/indexer.rb', line 17

def self.from_string(json, index = {})
  input = JSON.parse(json)

  # assume the json input is always an array at the top level
  input.each do |item|
    index_fields(item, item, index)
  end

  index
end

.index_fields(root, input, index) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jsonar/indexer.rb', line 28

def self.index_fields(root, input, index)
  if input.is_a? Array
    input.each do |item|
      index_fields(root, item, index)
    end
  elsif input.is_a? Hash
    input.each_value do |v|
      index_fields(root, v, index)
    end
  else
    input = input.to_s
    input = 'null' if input.empty?
    index[input] = index[input] || Set.new
    index[input] << root
  end

  index
end