Class: Libis::Tools::SharepointMapping

Inherits:
Hash show all
Defined in:
lib/libis/tools/metadata/sharepoint_mapping.rb

Instance Method Summary collapse

Methods inherited from Hash

#cleanup, #key_strings_to_symbols, #key_strings_to_symbols!, #key_symbols_to_strings, #key_symbols_to_strings!, #recursive_cleanup, #recursive_merge, #recursive_merge!

Constructor Details

#initialize(mapping_file) ⇒ SharepointMapping

Returns a new instance of SharepointMapping.



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
62
63
64
65
66
67
68
69
70
# File 'lib/libis/tools/metadata/sharepoint_mapping.rb', line 13

def initialize(mapping_file)


  CSV.foreach(mapping_file, headers: true, skip_blanks: true) do |row|
    next unless row[1]
    # next unless (row[2] || row[3])

    # compensation for bug in library that reads the Excel data
    0.upto(5) { |i| row[i] = row[i].gsub(/_x005F(_x[0-9a-fA-F]{4}_)/, '\1') if row[i] }

    name = row[0] ? row[0].strip : nil
    label = row[1].strip.to_sym
    dc_tag = row[2] ? row[2].strip : ''
    db_column = row[3] ? row[3].strip : nil
    db_datatype = row[4] ? row[4].strip.upcase.to_sym : nil
    db_valuemask = row[5] ? row[5] : nil
    #      scope_tag = row[6] ? row[6].strip : nil
    #      scope_id = (row[7] and row[7] =~ /[0-9]+/ ? Integer(row[7].strip) : nil)


    mapping = {}
    mapping[:fancy_name] = name if name
    mapping[:db_column] = db_column if db_column
    mapping[:db_datatype] = :STRING
    mapping[:db_datatype] = db_datatype if db_datatype
    mapping[:db_valuemask] = (mapping[:db_datatype] == :STRING ? "'@@'" : '@@')
    mapping[:db_valuemask] = db_valuemask if db_valuemask
    #      mapping[:scope_tag] = scope_tag if scope_tag
    #      mapping[:scope_id] = scope_id if scope_id

    if dc_tag.match(/^\s*"(.*)"\s*(<.*)$/)
      mapping[:dc_prefix] = $1
      dc_tag = $2
    end

    if dc_tag.match(/^\s*<dc:[^.]+\.([^.>]+)>(.*)$/)
      mapping[:dc_tag] = "dcterms:#{$1}"
      dc_tag = $2

    elsif dc_tag.match(/^\s*<dc:([^.>]+)>(.*)$/)
      mapping[:dc_tag] = "dc:#{$1}"
      dc_tag = $2
    end

    if dc_tag.match(/^\s*"(.*)"\s*$/)
      mapping[:dc_postfix] = $1
    end

    self[label] = mapping.empty? ? nil : mapping

  end

  File.open('mapping.yml', 'wt') { |fp|
    fp.puts self.to_yaml
  }
  super nil

end

Instance Method Details

#db_column(label) ⇒ Object



102
103
104
105
106
# File 'lib/libis/tools/metadata/sharepoint_mapping.rb', line 102

def db_column(label)
  mapping = self[label]
  mapping = mapping[:db_column] if mapping
  mapping
end

#db_value(label, value) ⇒ Object



108
109
110
111
112
113
# File 'lib/libis/tools/metadata/sharepoint_mapping.rb', line 108

def db_value(label, value)
  mapping = self[label]
  return nil unless mapping
  mask = mapping[:db_valuemask]
  mask.gsub('@@', value.to_s)
end

#dc_postfix(label) ⇒ Object



96
97
98
99
100
# File 'lib/libis/tools/metadata/sharepoint_mapping.rb', line 96

def dc_postfix(label)
  mapping = self[label]
  mapping = mapping[:dc_postfix] if mapping
  mapping
end

#dc_prefix(label) ⇒ Object



90
91
92
93
94
# File 'lib/libis/tools/metadata/sharepoint_mapping.rb', line 90

def dc_prefix(label)
  mapping = self[label]
  mapping = mapping[:dc_prefix] if mapping
  mapping
end

#dc_tag(label) ⇒ Object



84
85
86
87
88
# File 'lib/libis/tools/metadata/sharepoint_mapping.rb', line 84

def dc_tag(label)
  mapping = self[label]
  mapping = mapping[:dc_tag] if mapping
  mapping
end

#fancy_label(label) ⇒ Object



78
79
80
81
82
# File 'lib/libis/tools/metadata/sharepoint_mapping.rb', line 78

def fancy_label(label)
  mapping = self[label]
  mapping = mapping[:fancy_name] if mapping
  "#{label}#{mapping ? '(' + mapping + ')' : ''}"
end

#name(label) ⇒ Object



72
73
74
75
76
# File 'lib/libis/tools/metadata/sharepoint_mapping.rb', line 72

def name(label)
  mapping = self[label]
  mapping = mapping[:fancy_name] if mapping
  mapping || label
end