Class: LibSL::Block
- Inherits:
-
Object
show all
- Defined in:
- lib/packet.rb
Instance Method Summary
collapse
Constructor Details
#initialize(structure) ⇒ Block
Returns a new instance of Block.
165
166
167
168
169
|
# File 'lib/packet.rb', line 165
def initialize(structure)
@structure = structure
@data = []
@structure.length.times{ @data << nil }
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
# File 'lib/packet.rb', line 195
def method_missing(name, *args)
name = name.to_s
if name[-1..-1] == "=" then
var = name[0..-2].to_sym
throw "The method #{name} does not exist in #{self.class.name}" if @structure.assoc(var).nil?
index = @structure.index{|v| v[0] == var}
@data[index] = [var, args[0]]
else
var = name.to_sym
throw "The method #{name} does not exist in #{self.class.name}" if @structure.assoc(var).nil?
@data.assoc(var)[1]
end
end
|
Instance Method Details
#decode(data) ⇒ Object
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
# File 'lib/packet.rb', line 171
def decode(data)
@data = []
@structure.each do |var|
begin
args = [data]
args << var[2] if var[1] == :LLFixed
value, data = LibSL.const_get(var[1]).decode(*args)
end
@data << [var[0], value]
end
data
end
|
#encode ⇒ Object
187
188
189
190
191
192
193
|
# File 'lib/packet.rb', line 187
def encode()
throw "Data has to be an array." unless @data.is_a? Array
throw "Block is missing data." if @data.length != @structure.length
data = ""
@data.each{ |var| data += var[1].encode }
data
end
|