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
-
#as_obj(txt, datatype, arraysize = nil) ⇒ Object
Much like #cast but understands arrays.
-
#as_string(obj, datatype, arraysize = nil) ⇒ Object
Much like #convert_to_s but understands arrays.
-
#cast(txt, to = 'char') ⇒ Object
Convert a string value into its corresponding basic object representation, if appropriate.
-
#convert_to_s(val, from = 'char') ⇒ Object
Serialize a ruby object to the format that is correct for a votable.
Instance Method Details
#as_obj(txt, datatype, arraysize = nil) ⇒ Object
Much like #cast but understands arrays.
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/voruby/votable/votable.rb', line 208 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.
237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/voruby/votable/votable.rb', line 237 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
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/voruby/votable/votable.rb', line 171 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.
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/voruby/votable/votable.rb', line 189 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 |