Class: RGFA::FieldArray

Inherits:
Array show all
Defined in:
lib/rgfa/field_array.rb

Overview

Array representing multiple values of the same tag in different header lines

Defined Under Namespace

Classes: Error, TypeMismatchError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#rgfa_field_array?, #to_byte_array, #to_cigar, #to_cigar_operation, #to_numeric_array, #to_oriented_segment, #to_rgfa, #to_rgfa_field_array, #to_rgfa_line, #to_segment_end

Constructor Details

#initialize(datatype, data = []) ⇒ FieldArray

Returns a new instance of FieldArray.

Parameters:



6
7
8
9
# File 'lib/rgfa/field_array.rb', line 6

def initialize(datatype, data = [])
  @datatype = datatype
  super(data)
end

Instance Attribute Details

#datatypeObject (readonly)

Returns the value of attribute datatype.



3
4
5
# File 'lib/rgfa/field_array.rb', line 3

def datatype
  @datatype
end

Instance Method Details

#default_gfa_datatypeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Default datatype, in this case :J



19
20
21
# File 'lib/rgfa/field_array.rb', line 19

def default_gfa_datatype
  :J
end

#push_with_validation(value, type, fieldname = nil) ⇒ Object

Add a value to the array and validate

Parameters:

  • value (Object)

    the value to add

  • type (RGFA::Line::OPTFIELD_DATATYPE, nil)

    the datatype to use; if not nil, it will be checked that the specified datatype is the same as for previous elements of the field array; if nil, the value will be validated, according to the datatype specified on field array creation

  • fieldname (Symbol) (defaults to: nil)

    the field name to use for error messages

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rgfa/field_array.rb', line 45

def push_with_validation(value, type, fieldname=nil)
  if type.nil?
    value.validate_gfa_field!(@datatype, fieldname)
  elsif type != @datatype
    raise RGFA::FieldArray::TypeMismatchError,
      "Datatype mismatch error for field #{fieldname}:\n"+
      "value: #{value}\n"+
      "existing datatype: #{@datatype};\n"+
      "new datatype: #{type}"
  end
  self << value
end

#to_gfa_field(datatype: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Representation of the field array as JSON array, with two additional values: the datatype and a zero byte as “signature”.

Parameters:



27
28
29
30
31
# File 'lib/rgfa/field_array.rb', line 27

def to_gfa_field(datatype: nil)
  self << @datatype
  self << "\0"
  to_json
end

#validate_gfa_field!(datatype, fieldname = nil) ⇒ Object

Run a datatype-specific validation on each element of the array

Parameters:



13
14
15
# File 'lib/rgfa/field_array.rb', line 13

def validate_gfa_field!(datatype, fieldname=nil)
  each.validate_gfa_field!(@datatype, fieldname)
end