Class: JSONCrossreference

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

Instance Method Summary collapse

Constructor Details

#initialize(input1, input2) ⇒ JSONCrossreference

Returns a new instance of JSONCrossreference.



5
6
7
8
9
# File 'lib/jsoncrossreference.rb', line 5

def initialize(input1, input2)
  @input1 = input1
  @input2 = input2
  @output = nil
end

Instance Method Details

#compare(*fieldnames) ⇒ Object

What: Cross-references fields in two files Input: Variable number of arrays with name of fields to compare Output: JSON with matches Limitations: Only returns data from one file. Matching multiple fields functions as && not ||. Only takes 2 files input



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jsoncrossreference.rb', line 15

def compare(*fieldnames)
  if JSON.parse(@input2).length < JSON.parse(@input1).length
    file1 = JSON.parse(@input2)
    file1length = file1.length
    file2 = @input1

    tmparray = Array.new
    fieldnames.each do |field|
      tmparray.push([field[1], field[0]])
    end
    fieldnames = tmparray
  else 
    file1 = JSON.parse(@input1)
    file1length = file1.length
    file2 = @input2
  end
  
  tmpfile = Array.new
  (0..file1length-1).each do |l|
    fieldnames.each do |field|
      value = (file1[l])[field[0]]
      matchnames = [field[1], value]
      m = DataCalc.new(file2)
      tmpfile << m.match(matchnames)
    end
  end
  @output = tmpfile.to_json
end