Module: VORuby::VOTable::Castable

Included in:
V1_0::Param, V1_0::Td, V1_1::Param, V1_1::Td
Defined in:
lib/voruby/votable/votable.rb

Overview

Some helper methods for converting string values that have an associated data type (like TDs do through their associated FIELD).

Instance Method Summary collapse

Instance Method Details

#as_obj(txt, datatype, arraysize = nil) ⇒ Object

Much like #cast but understands arrays.



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/voruby/votable/votable.rb', line 262

def as_obj(txt, datatype, arraysize=nil)
  raise "Unable to determine datatype" if !datatype
  
  txt_list = arraysize ? txt.split(/\s+/) : [txt]
  values_list = []
  
  # character string, do nothing
  return txt if datatype == 'char' or datatype == 'unicodeChar'
  
  # complex values are a bit different
  if datatype == 'floatComplex' || datatype == 'doubleComplex'
    txt_list = txt.split(/\s+/)
    complex_numbers = []
    i = 0
    (0...txt_list.size/2).each do |n|
      complex_numbers << Complex.new(txt_list[i].to_f, txt_list[i+1].to_f)
      i += 2
    end
    return arraysize ? complex_numbers : complex_numbers.first
  end
  
  txt_list.each do |component|
    values_list << cast(component, datatype)
  end
  
  arraysize ? values_list : values_list.first
end

#as_string(obj, datatype, arraysize = nil) ⇒ Object

Much like #convert_to_s but understands arrays.



291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/voruby/votable/votable.rb', line 291

def as_string(obj, datatype, arraysize=nil)
  raise "Unable to determine datatype" if !datatype
  raise "Supposed to contain an array of size '#{arraysize}'" if arraysize and !obj.is_a?(Array)
  raise "Supposed to contain a single value" if !arraysize and obj.is_a?(Array)
  
  obj = [obj] if !obj.is_a?(Array)
  
  values = []
  obj.each do |val|
    values << convert_to_s(val, datatype)
  end
  
  values.join(' ')
end

#cast(txt, to = 'char') ⇒ Object

Convert a string value into its corresponding basic object representation, if appropriate. This does not work on array values, only on scalars. Native ruby types map to votable datatypes in the following way:

  • ‘boolean’ => boolean (i.e TrueClass or FalseClass)

  • ‘bit’ => Integer (1 or 0)

  • ‘unsignedByte’ => Integer representation of the byte

  • ‘short, ’int’, ‘long’ => Integer

  • ‘char’, ‘unicodeChar’ => String

  • ‘float’, ‘double’ => Float or Bignum

  • ‘floatComplex’, ‘doubleComplex’ => Complex



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/voruby/votable/votable.rb', line 225

def cast(txt, to='char')
  case to
  when 'boolean'
    txt == '1' || txt.downcase == 'true' || txt.downcase == 't'
  when 'bit'          then txt.to_i
  when 'unsignedByte' then txt[0]
  when 'short'        then txt.to_i
  when 'int'          then txt.to_i
  when 'long'         then txt.to_i
  when 'double'       then txt.to_f
  when 'float'        then txt.to_f
  else
    txt
  end
end

#convert_to_s(val, from = 'char') ⇒ Object

Serialize a ruby object to the format that is correct for a votable.



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/voruby/votable/votable.rb', line 243

def convert_to_s(val, from='char')
  case from
  when 'boolean'
    (val.is_a?(TrueClass) or val.to_s.downcase =~ /^t/ or val.to_s == '1') ?
      'true' : 'false'
  when 'bit'            then val.to_i.to_s
  when 'short'          then val.to_i.to_s
  when 'int'            then val.to_i.to_s
  when 'long'           then val.to_i.to_s
  when 'float'          then val.to_f.to_s
  when 'double'         then val.to_f.to_s
  when 'floatComplex'   then "#{val.real} #{val.image}"
  when 'doubleComplex'  then "#{val.real} #{val.image}"
  else
    val.to_s
  end
end