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

#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