Class: GEDCOMBase

Inherits:
Object
  • Object
show all
Defined in:
lib/gedcom/gedcom_base.rb

Overview

base routines shared by all gedcom objects.

Constant Summary collapse

@@tabs =

If true, indent gedcom lines on output a tab per level. Normally wouldn't have tabs in a transmission file.

false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transmission = nil, changed = true, created = true) ⇒ GEDCOMBase

Create a new GEDCOMBase or most likely a subclass of GEDCOMBase. * transmission is the current transmission this object came from. This is useful in searchs of the transmission, starting from a reference in a record in that transmission. * changed indicates that we have altered the data in the record The default is true, as the normal instantiation is creating a new record. to_db uses this to determine if the record needs to be saved to the DB. * created indicates that this is a new record, rather than one we may have loaded from a DB.



18
19
20
21
22
23
24
# File 'lib/gedcom/gedcom_base.rb', line 18

def initialize(transmission = nil, changed=true,created=true)
  @changed = changed
  @created = created
  @this_level = []
  @sub_level = []
  @transmission = transmission
end

Instance Attribute Details

#restrictionObject

Restriction records only exist in some GEDCOM record types, and we do honor that. Internally though, my DB allows any record to be restricted, hence this global use of :restriction.



8
9
10
# File 'lib/gedcom/gedcom_base.rb', line 8

def restriction
  @restriction
end

Class Method Details

.no_tabsObject

sets @@tabs to false. This is the default as a lot of GEDCOM parsers don't like leading white space on lines.



37
38
39
# File 'lib/gedcom/gedcom_base.rb', line 37

def self.no_tabs
  @@tabs = false #The default
end

.tabsObject

sets @@tabs to true. This indents gedcom lines on output a tab per level. This is useful if pretty printing, but not normal in a gedcom file as most GEDCOM file parsers don't like leading tabs on lines of a transmission file. I use this only for debugging.



31
32
33
# File 'lib/gedcom/gedcom_base.rb', line 31

def self.tabs
  @@tabs = true 
end

Instance Method Details

#changedObject

Marks this object as having been altered so we know to synchronise it with the DB.



42
43
44
# File 'lib/gedcom/gedcom_base.rb', line 42

def changed
  @changed = true
end

#changed?Boolean

Tests for this object having been altered, so we know to synchronise it with the DB.

Returns:

  • (Boolean)


47
48
49
# File 'lib/gedcom/gedcom_base.rb', line 47

def changed?
  @changed
end

#created?Boolean

Tests to see if this is a new object, not one we have loaded from elsewhere (say a DB)

Returns:

  • (Boolean)


52
53
54
# File 'lib/gedcom/gedcom_base.rb', line 52

def created?
  @created
end

#find(*a) ⇒ Object

Find a XREF within this transmission. All classes inheriting from GEDCOMBase record the parent transmission object.



91
92
93
# File 'lib/gedcom/gedcom_base.rb', line 91

def find(*a)
  @transmission.find(*a) if @transmission != nil
end

#locked?Boolean

Test for a Restriction notice for a locked record. Locked records mean that the data is known to be correct, so shouldn't be altered without checking with the original source of the data.

Returns:

  • (Boolean)


104
105
106
# File 'lib/gedcom/gedcom_base.rb', line 104

def locked?
  (@restriction != nil && @restriction == "locked") ? true : false
end

#private?Boolean

Test for a Restriction notice for privacy People may request that a records contents not be available for public consumption.

Returns:

  • (Boolean)


97
98
99
# File 'lib/gedcom/gedcom_base.rb', line 97

def private?
  (@restriction != nil && @restriction == "privacy") ? true : false
end

#saveObject

This is the default method, used by all classes inheriting from GEDCOMBase, to recursively save the object and its sub-records to a DB. All subclasses of GEDCOMBase are expected to define @this_level and @sub_level arrays, which are instructions to to_db on how to generate GEDCOM lines from the attributes of the object.



86
87
88
# File 'lib/gedcom/gedcom_base.rb', line 86

def save
  to_db( level, @this_level, @sub_level)
end

#to_db(level = 0, this_level = [], sub_levels = []) ⇒ Object

Need to flesh this out. Right now it pretends to work and marks records as saved.



71
72
73
74
# File 'lib/gedcom/gedcom_base.rb', line 71

def to_db(level = 0, this_level=[], sub_levels=[])
  @changed = false
  @created = false
end

#to_gedcom(level = 0) ⇒ Object

This is the default method, used by all classes inheriting from GEDCOMBase, to recursively generate GEDCOM lines from that Object downward. All subclasses of GEDCOMBase are expected to define @this_level and @sub_level arrays, which are instructions to to_s_r on how to generate GEDCOM lines from the attributes of the object.



79
80
81
# File 'lib/gedcom/gedcom_base.rb', line 79

def to_gedcom(level = 0)
  to_s_r( level, @this_level, @sub_level )
end

#to_sObject

create a string from the objects instance variables, one per line, in the form “variable = valuen” … . For an ordered list, see to_s_ordered



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gedcom/gedcom_base.rb', line 58

def to_s
  #This might seem a little obscure, but this will find and print the attributes with get methods defined,
  #not having prior knowledge of what those attributes are.
  s = ''
  self.instance_variables.each do |v| #look at each of the instance variables
    if self.class.method_defined?(v_sym = v[1..-1].to_sym) #see if there is a method defined for this symbol (strip the :)
      s += "#{v} = " + pv_byname(v_sym).to_s + "\n" #print it
    end
  end
  s
end

#to_s_ordered(variable_list) ⇒ Object (protected)

to_s with the variable list (as symbols) passed to it in the order they are to be printed



289
290
291
292
293
294
295
296
297
298
299
# File 'lib/gedcom/gedcom_base.rb', line 289

def to_s_ordered(variable_list)
  if variable_list != nil
    s = ''
    variable_list.each do |v|
      s += "@#{v} = " + pv_byname(v) + "\n"
    end
    s
  else
    ''
  end
end

#to_s_r(level = 0, this_level = [], sub_levels = []) ⇒ Object (protected)

recursive to_s. We want to print this object and its sub-records. the definition of how we want to print and when to recurse, is in the this_level and sub_level arrays. These have the form [ [ action, tag, data_source ],…] (see to_s_r_action )



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/gedcom/gedcom_base.rb', line 305

def to_s_r(level = 0, this_level=[], sub_levels=[])
  s_out = ""
  this_level.each do |l|
    this_level_instruction = Instruction.new(l)
    if this_level_instruction.data != nil
      data = self.send( this_level_instruction.data ) #gets the contents using the symbol, and sending "self" a message
    else
      data =  [GedString.new('')]
    end

    if data != nil #could be if the self.send targets a variable that doesn't exist.
      data.each do |data_instance| 
        s_out += to_s_r_action(level, this_level_instruction.action, this_level_instruction.tag, data_instance)
      
        sub_levels.each do |sl|
          sub_level_instruction = Instruction.new(sl)
          if sub_level_instruction.data != nil
            sub_level_data = self.send( sub_level_instruction.data ) #gets the contents using the symbol, and sending "self" a message
          else
             sub_level_data = [GedString.new('')]
          end
          
          if sub_level_data != nil  #could be if the self.send targets a variable that doesn't exist.
            sub_level_data.each do |sub_data_instance| 
              s_out += to_s_r_action(level+1, sub_level_instruction.action, sub_level_instruction.tag, sub_data_instance )
            end
          end
        end

      end
    end
  end
  return s_out
end

#token_to_s(token) ⇒ Object

All our values are stored as arrays of words. This is quite useful in word wrapping of NOTES, TEXT, etc, and further parsing records like dates. It isn't really that helpful when we want to use a value as a string. This is a utility function to join the words into a space separated string.



110
111
112
# File 'lib/gedcom/gedcom_base.rb', line 110

def token_to_s(token)
  token
end

#xref_check(level, tag, xref) ⇒ Object (protected)

validate that the record referenced by the XREF actually exists in this transmission. Genearte a warning if it does not. It does not stop the processing of this line.



277
278
279
280
281
282
283
284
285
286
# File 'lib/gedcom/gedcom_base.rb', line 277

def xref_check( level, tag, xref )
  if xref.class != Xref
    print "#{level+1} XREF_CHECK ****************#{level}, #{tag}. #{xref.class} != Xref?\n"
    return
  end
  if @transmission != nil && @transmission.find(xref.index, xref.xref_value) == nil
    #Warning message that reference points to an unknown target.
    print "#{level+1} NOTE ****************#{level}, #{tag} Key not found: #{xref.index} #{xref.xref_value}\n"
  end
end