Class: Fsinv::LookupTable
- Inherits:
-
Object
- Object
- Fsinv::LookupTable
- Defined in:
- lib/fsinv/lookuptable.rb
Instance Attribute Summary collapse
-
#idcursor ⇒ Object
Returns the value of attribute idcursor.
-
#val_map ⇒ Object
Returns the value of attribute val_map.
Instance Method Summary collapse
- #add(value) ⇒ Object
- #as_json(options = { }) ⇒ Object
- #contains?(value) ⇒ Boolean
- #empty? ⇒ Boolean
- #from_json(json) ⇒ Object
- #get_id(value) ⇒ Object
- #get_value(id) ⇒ Object
-
#initialize ⇒ LookupTable
constructor
A new instance of LookupTable.
- #marshal_dump ⇒ Object
- #marshal_load(data) ⇒ Object
- #to_a ⇒ Object
- #to_json(*a) ⇒ Object
Constructor Details
#initialize ⇒ LookupTable
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
#idcursor ⇒ Object
Returns the value of attribute idcursor.
5 6 7 |
# File 'lib/fsinv/lookuptable.rb', line 5 def idcursor @idcursor end |
#val_map ⇒ Object
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( = { }) return to_a end |
#contains?(value) ⇒ 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
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_dump ⇒ Object
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_a ⇒ Object
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 |