Module: GenerateId

Included in:
IndexManager
Defined in:
lib/generate_id.rb

Instance Method Summary collapse

Instance Method Details

#addIndex(id, dataspec) ⇒ Object

Add index name to id iff more than one dataset


12
13
14
15
16
17
18
19
20
# File 'lib/generate_id.rb', line 12

def addIndex(id, dataspec)
  dataset_count = @importer.first[1].length

  # Append if more than one dataset
  if dataset_count > 1
    return id+dataspec.index_name
  else return id
  end
end

#appendSecondary(clean_id, item, dataspec) ⇒ Object

Append secondary ID fields


23
24
25
26
27
28
29
30
# File 'lib/generate_id.rb', line 23

def appendSecondary(clean_id, item, dataspec)
  dataspec.id_secondary.each do |f|
    secondary_field = item[set_name(f, item)]
    clean_id += cleanID(secondary_field.to_s) if secondary_field != nil
  end

  return clean_id
end

#cleanID(str) ⇒ Object

Removes non-urlsafe chars from ID


42
43
44
45
46
# File 'lib/generate_id.rb', line 42

def cleanID(str)
  if str
    return str.gsub("/", "").gsub(" ", "").gsub(",", "").gsub(":", "").gsub(";", "").gsub("'", "").gsub(".", "")
  end
end

#getID(item, dataspec) ⇒ Object

Get unique ID from formatted primary and secondary fields


5
6
7
8
9
# File 'lib/generate_id.rb', line 5

def getID(item, dataspec)
  id_initial = item[set_name(dataspec.id_field, item)]
  clean_id = cleanID(removeIDPart(id_initial, dataspec))
  return addIndex(appendSecondary(clean_id, item, dataspec), dataspec)
end

#removeIDPart(id_initial, dataspec) ⇒ Object

Remove part of ID if needed


33
34
35
36
37
38
39
# File 'lib/generate_id.rb', line 33

def removeIDPart(id_initial, dataspec)
  if dataspec.get_after != nil && !dataspec.get_after.empty?
    id_initial = id_initial.split(dataspec.get_after)[1]
  end

  return id_initial
end