Class: GFA::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/gfa/record.rb

Direct Known Subclasses

Comment, Containment, Header, Jump, Link, Path, Segment, Walk

Defined Under Namespace

Modules: HasFromTo Classes: Comment, Containment, Header, Jump, Link, Path, Segment, Walk

Constant Summary collapse

CODES =

Class-level

{
  :'#' => :Comment,
  H: :Header,
  S: :Segment,
  L: :Link,
  J: :Jump, # Since 1.2
  C: :Containment,
  P: :Path,
  W: :Walk # Since 1.1
}
REQ_FIELDS =

Since 1.1

[]
OPT_FIELDS =
{}
TYPES =
CODES.values

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fieldsObject (readonly)

Instance-level



42
43
44
# File 'lib/gfa/record.rb', line 42

def fields
  @fields
end

Class Method Details

.[](string) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/gfa/record.rb', line 32

def self.[](string)
  return nil if string.nil? || string =~ /^\s*$/

  split = string[0] == '#' ? ['', 2] : ["\t", 0]
  code, *values = string.chomp.split(*split)
  code_class(code).new(*values)
end

.code_class(code) ⇒ Object



22
23
24
25
26
# File 'lib/gfa/record.rb', line 22

def self.code_class(code)
  name = CODES[code.to_sym]
  raise "Unknown record type: #{code}." if name.nil?
  name_class(name)
end

.name_class(name) ⇒ Object



28
29
30
# File 'lib/gfa/record.rb', line 28

def self.name_class(name)
  const_get(name)
end

Instance Method Details

#[](k) ⇒ Object



44
45
46
# File 'lib/gfa/record.rb', line 44

def [](k)
  fields[k]
end

#codeObject



52
53
54
# File 'lib/gfa/record.rb', line 52

def code
  self.class.const_get(:CODE)
end

#dupObject



72
73
74
# File 'lib/gfa/record.rb', line 72

def dup
  self.class[to_s]
end

#empty?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/gfa/record.rb', line 56

def empty?
  fields.empty?
end

#eql?(rec) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


80
81
82
# File 'lib/gfa/record.rb', line 80

def eql?(rec)
  hash == rec.hash
end

#hashObject



76
77
78
# File 'lib/gfa/record.rb', line 76

def hash
  { code => fields }.hash
end

#to_sObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gfa/record.rb', line 60

def to_s
  o = [code.to_s]
  self.class.REQ_FIELDS.each_index do |i|
    o << fields[i + 2].to_s(false)
  end
  fields.each do |k, v|
    next if k.is_a? Integer
    o << "#{k}:#{v}"
  end
  o.join("\t")
end

#typeObject



48
49
50
# File 'lib/gfa/record.rb', line 48

def type
  CODES[code]
end