Class: UR::Serialize::DataObject

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDataObject

Returns a new instance of DataObject.



81
82
83
84
# File 'lib/serialize.rb', line 81

def initialize
  @values = {}
  @recipe_id = nil
end

Instance Attribute Details

#recipe_idObject

Returns the value of attribute recipe_id.



79
80
81
# File 'lib/serialize.rb', line 79

def recipe_id
  @recipe_id
end

#valuesObject (readonly)

Returns the value of attribute values.



78
79
80
# File 'lib/serialize.rb', line 78

def values
  @values
end

Class Method Details

.create_empty(names, recipe_id) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/serialize.rb', line 107

def self.create_empty(names, recipe_id)
  obj = DataObject.new
  names.each do |i|
    obj.values[i] = nil
  end
  obj.recipe_id = recipe_id
  obj
end

.unpack(data, names, types) ⇒ Object



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

def self.unpack(data, names, types)
  if names.length != types.length
    raise RuntimeError.new('List sizes are not identical.')
  end
  obj = DataObject.new
  offset = 0
  obj.recipe_id = data[0]
  names.each_with_index do |name,i|
    obj.values[name] = Serialize.unpack_field(data[1..-1], offset, types[i])
    offset += Serialize::get_item_size(types[i])
  end
  obj
end

Instance Method Details

#[](item) ⇒ Object



86
87
88
# File 'lib/serialize.rb', line 86

def [](item)
  @values[item]
end

#[]=(item, value) ⇒ Object



89
90
91
# File 'lib/serialize.rb', line 89

def []=(item,value)
  @values[item] = value
end

#pack(names, types) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/serialize.rb', line 116

def pack(names, types)
  if names.length != types.length
    raise RuntimeError.new('List sizes are not identical.')
  end
  l = []
  l.append @recipe_id if @recipe_id
  names.each do |i|
    raise RuntimeError.new('Uninitialized parameter: ' + i) unless @values[i]
    l.push *@values[i]
  end
  l
end