Class: ML_ruby::ML_compound

Inherits:
Object
  • Object
show all
Defined in:
lib/ml-ruby/ml_data_types.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ ML_compound

Returns a new instance of ML_compound.



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/ml-ruby/ml_data_types.rb', line 138

def initialize data
  self.class.attributes.each do |attr,type| 
    set_compound_attribute( attr, data[attr], type )
  end
  if self.class.conditions
    self.class.conditions.each do |test,extra|
      key=data[test].payload
      extra[key].each do |attr,type| 
        set_compound_attribute( attr, data[attr], type )
      end if extra[key]
    end
  end
end

Class Method Details

.attributesObject



132
133
134
# File 'lib/ml-ruby/ml_data_types.rb', line 132

def self.attributes
  @attributes
end

.conditionsObject



135
136
137
# File 'lib/ml-ruby/ml_data_types.rb', line 135

def self.conditions
  @conditions
end

.decode(data) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ml-ruby/ml_data_types.rb', line 171

def self.decode data
  #    puts data.inspect
  attributes={}
  self.attributes.each do |attr,type|
    attributes[attr]= type.decode data
    data= data[attributes[attr].size..-1]
  end
  if conditions
  #      puts attributes.inspect
    conditions.each do |test,extra|
  #        puts "testing #{[test,extra].inspect}"
      key= attributes[test].payload
      extra[key].each do |attr,type|
        attributes[attr]= type.decode data
        data= data[attributes[attr].size..-1]
      end if extra[key]
    end
  end
  self.new attributes
end

Instance Method Details

#encodeObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/ml-ruby/ml_data_types.rb', line 154

def encode
  result=""
  self.class.attributes.each do |attr,type|
    result<<( send( attr ).encode )
  end
  if self.class.conditions
  #      puts attributes.inspect
    self.class.conditions.each do |test,extra|
  #        puts "testing #{[test,extra].inspect}"
      key= send( test ).payload
      extra[key].each do |attr,type|
        result<<( send( attr ).encode )
      end if extra[key]
    end
  end
  result
end

#sizeObject



151
152
153
# File 'lib/ml-ruby/ml_data_types.rb', line 151

def size
  self.class.attributes.inject( 0 ) { |sum,attr| sum+send( attr[0] ).size }
end