Class: HandleInput

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

Instance Method Summary collapse

Constructor Details

#initialize(json, ignorefields, caseinfo) ⇒ HandleInput

Returns a new instance of HandleInput.



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/handleinput.rb', line 4

def initialize(json, ignorefields, caseinfo)
  @json = json
  if ignorefields
    @ignorefields = ignorefields
  else
    @ignorefields = Array.new
  end
  
  @caseinfo = caseinfo
  @output = Array.new
  @outhash = Hash.new
end

Instance Method Details

#checkCaseObject

Adds case sensitive preferences



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/handleinput.rb', line 93

def checkCase
  if @caseinfo == "casesensitive"
    @output.each do |i|
      @outhash[i] = true
    end
  elsif @caseinfo == "noncasesensitive"
    @output.each do |i|
      @outhash[i] = false
    end
  end
end

#detecttypeObject

Figure out which type of input it is: array, hash, hash with hash values



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/handleinput.rb', line 73

def detecttype
  if @json.is_a? Array
    @output = @json
    checkCase
  elsif @json.is_a? Hash
    @json.each do |k, v|
      if v.is_a? Hash
        parseValHash
        break
      else
        parseDictionary
        break
      end
    end
  end

  return @outhash
end

#mapout(addlist, mapto) ⇒ Object

Map output to value



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
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
# File 'lib/handleinput.rb', line 18

def mapout(addlist, mapto)
  outarr = Array.new
  
  addlist.each do |a|
    if mapto == "key"
      @json.each do |k, v|
        # If it's a nested hash
        if v.is_a? Hash
          # Go through all values
          v.each do |z, w|
            # Check if k is already included
            if !outarr.include? k
              if w == a
                outarr.push(k)
              end
            end
          end
        else
          # Map for dictionaries
          if !outarr.include? k
            if v == a || k == a
              outarr.push(k)
            end
          end
        end
      end
    elsif mapto == "value"
      @json.each do |k, v|
        if !v.is_a? Hash
          if !outarr.include? v
            if k == a || v == a
              outarr.push(v)
            end
          end
        end
      end
    else
      @json.each do |k, v|
        v.each do |z, w|
          # Only map if not already matched
          if !outarr.include? v[mapto]
            # Check if vals match
            if w == a
              outarr.push(v[mapto])
            end
          end
        end
      end
    end
  end

  return outarr
end

#parseDictionaryObject

Handle hashes



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/handleinput.rb', line 129

def parseDictionary
  @json.each do |k, v|
    if !@ignorefields.include? "hashkey"
      if @caseinfo.include? "hashkey"
        @outhash[k] = false
      else
        @outhash[k] = true
      end
    end

    if !@ignorefields.include? "hashval"
      if @caseinfo.include? "hashval"
        @outhash[v] = false
      else
        @outhash[v] = true
      end
    end
  end
end

#parseValHashObject

Handle hashes where the values are a hash



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/handleinput.rb', line 106

def parseValHash
  @json.each do |k, v|
    if !@ignorefields.include? "hashkey"
      if @caseinfo.include? "hashkey"
        @outhash[k] = false
      else
        @outhash[k] = true
      end
    end

    v.each do |i, j|
      if !@ignorefields.include? i
        if @caseinfo.include? i
          @outhash[j] = false
        else
          @outhash[j] = true
        end
      end
    end
  end
end