Class: GedLine

Inherits:
Object
  • Object
show all
Defined in:
lib/parser/ged_line.rb

Overview

GedLine takes a GEDCOM line tokenised into a word array returning an object with the: * level number in @level * the GEDCOM tag in @tag * the data portion in @data * the XREF value, if this line has one, in @XREF * the @user set to true if this is a user defined TAG (e.g. starts with '_')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*data) ⇒ GedLine

GedLine.new(*data) takes a GEDCOM line as word array and does all the work to categorize the tokens.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/parser/ged_line.rb', line 13

def initialize(*data)
  case
    when data.length < 2
      raise "line too short (#{data.length})"
    when data[0] == nil
      raise "level is nil"
    when data[1] == nil
      raise "No Tag or Xref after the level"
    #need to add a test to ensure level is a number!
  end 
  
  @level = data[0].to_i
  
  if @level == 0 #At the top level, where the xrefs and tags are reversed.
    if data[1][0..0] == '@' #Then we have an xref line
      @xref = data[1].gsub(/\A@|@\Z/, '')
      @tag = data[2]
      @data = data.length == 3 ? nil : data[3..-1] #note lines can have data here. Others will not.
      if @xref.length > 22
        raise "Xref too long (Max = 22, length = #{@xref.length}) "
      end
    elsif data.length != 2 #This is a Head or Trailer line, so should be two tokens long.
      raise "Level 0 line has too many tokens"
    else
      @xref = nil
      @data = nil
      @tag = data[1]
    end
  else
    @tag = data[1]
    if data.length == 2
      @xref = nil
      @data = nil
    elsif data[2][0..0] == '@' #Then we have an xref line
      if data.length != 3           #These will always have 3 tokens.
        raise "Level #{@level} #{@tag} Xref Line has too many tokens"
      end
      @xref = data[2].gsub(/\A@|@\Z/, '')
      @data = nil
      if @xref.length > 22
        raise "Xref too long (Max = 22, length = #{@xref.length}) "
      end
    else
      @xref = nil
      @data = data[2..-1]
    end
  end 
  @user = nil
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



10
11
12
# File 'lib/parser/ged_line.rb', line 10

def data
  @data
end

#levelObject

Returns the value of attribute level.



10
11
12
# File 'lib/parser/ged_line.rb', line 10

def level
  @level
end

#tagObject

Returns the value of attribute tag.



10
11
12
# File 'lib/parser/ged_line.rb', line 10

def tag
  @tag
end

#userObject

Returns the value of attribute user.



10
11
12
# File 'lib/parser/ged_line.rb', line 10

def user
  @user
end

#xrefObject

Returns the value of attribute xref.



10
11
12
# File 'lib/parser/ged_line.rb', line 10

def xref
  @xref
end

Instance Method Details

#indexObject

Returns the hash key for this GEDCOM LINE to lookup the action in the GedcomParser::TAG has.



69
70
71
# File 'lib/parser/ged_line.rb', line 69

def index
  user_tag? ? [ "NOTE", :user ] : ( @xref ? [@tag, :xref] : [@tag, nil] )
end

#to_sObject

Returns a String with the @level, @xref, @tag and @data values.



96
97
98
# File 'lib/parser/ged_line.rb', line 96

def to_s
  "Level #{@level}, Xref = @#{@xref}@, Tag = #{@tag}, Data = '#{@data ? @data.inject('') do |x,y| x + ' ' + y end : nil}'"
end

#user_tagObject

creates as NOTE from a user defined tag. sets the @user field to true.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/parser/ged_line.rb', line 76

def user_tag
  if @xref != nil
    if @data == nil
      @data = [ '@' + @xref + '@' ]
    else
      @data[0,0] = '@' + @xref + '@'
    end
    @xref = nil if @level != 0 #Retain for the NOTES xref if this is a level 0 user tag.
  end
  if @data == nil
    @data = [ @tag ]
  else
    @data[0,0] = @tag
  end
  @data[0,0] = @level.to_s
  @user = true
  @tag = "NOTE"
end

#user_tag?Boolean

Test for this being a user Tag.

Returns:

  • (Boolean)


64
65
66
# File 'lib/parser/ged_line.rb', line 64

def user_tag?
  @user == true
end