Class: Fsinv::LookupTable

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLookupTable

Returns a new instance of LookupTable.



7
8
9
10
# File 'lib/fsinv/lookuptable.rb', line 7

def initialize
  @val_map = Hash.new
  @idcursor = 0
end

Instance Attribute Details

#idcursorObject

Returns the value of attribute idcursor.



5
6
7
# File 'lib/fsinv/lookuptable.rb', line 5

def idcursor
  @idcursor
end

#val_mapObject

Returns the value of attribute val_map.



5
6
7
# File 'lib/fsinv/lookuptable.rb', line 5

def val_map
  @val_map
end

Instance Method Details

#add(value) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fsinv/lookuptable.rb', line 16

def add(value)
  if self.contains?(value)
    return get_id(value)
  elsif value == "" || value == nil
    return nil
  else
    @idcursor += 1
    @val_map[@idcursor] = value
    return @idcursor
  end
  
end

#as_json(options = { }) ⇒ Object



55
56
57
# File 'lib/fsinv/lookuptable.rb', line 55

def as_json(options = { })
  return to_a
end

#contains?(value) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/fsinv/lookuptable.rb', line 12

def contains?(value)
  return value == "" ? false : @val_map.has_value?(value)
end

#empty?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/fsinv/lookuptable.rb', line 29

def empty?
  return @val_map.empty?
end

#from_json(json) ⇒ Object



49
50
51
52
53
# File 'lib/fsinv/lookuptable.rb', line 49

def from_json(json)
  json.each do |entry|
    self.add(entry["value"]) unless self.contains?(entry["value"])
  end
end

#get_id(value) ⇒ Object



33
34
35
# File 'lib/fsinv/lookuptable.rb', line 33

def get_id(value)
  return self.contains?(value) ? @val_map.key(value) : nil
end

#get_value(id) ⇒ Object



37
38
39
# File 'lib/fsinv/lookuptable.rb', line 37

def get_value(id)
  return self.contains?(value) ? @val_map[id] : nil
end

#marshal_dumpObject



63
64
65
66
67
68
# File 'lib/fsinv/lookuptable.rb', line 63

def marshal_dump
  return {
    'val_map' => val_map, 
    'idcursor' => idcursor
  }
end

#marshal_load(data) ⇒ Object



70
71
72
73
# File 'lib/fsinv/lookuptable.rb', line 70

def marshal_load(data)
  self.val_map = data['val_map']
  self.idcursor = data['idcursor']
end

#to_aObject



41
42
43
44
45
46
47
# File 'lib/fsinv/lookuptable.rb', line 41

def to_a
  table_arr = []
  @val_map.each do | id, val | 
    table_arr << {"id" => id, "value" => val}
  end
  return table_arr
end

#to_json(*a) ⇒ Object



59
60
61
# File 'lib/fsinv/lookuptable.rb', line 59

def to_json(*a)
  return as_json.to_json(*a)
end