Module: ASpaceImport::CSVConvert

Included in:
AccessionConverter, DigitalObjectConverter
Defined in:
backend/app/converters/lib/csv_converter.rb

Defined Under Namespace

Modules: ClassMethods Classes: CSVSyntaxException, CellHandler

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) included(base)



37
38
39
# File 'backend/app/converters/lib/csv_converter.rb', line 37

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

- (Object) configuration



42
43
44
# File 'backend/app/converters/lib/csv_converter.rb', line 42

def configuration
  self.class.configuration
end

- (Object) get_new(key)



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'backend/app/converters/lib/csv_converter.rb', line 109

def get_new(key)

  conf = configuration[key.to_sym] || {}

  type = conf[:record_type] ? conf[:record_type] : key

  proxy = @proxies.get_proxy_for(key, type)

  if conf[:on_create]
    proxy.on_spawn(conf[:on_create])
  end

  # Set defaults when done getting data
  if conf[:defaults]
    conf[:defaults].each do |key, val|
      proxy.on_discharge(self, :set_default, key, val)
    end
  end

  # Set links before batching the record
  if conf[:on_row_complete]
    proxy.on_discharge(conf[:on_row_complete], :call, @batch.working_area)
  end

  proxy
end

- (Object) get_queued_or_new(key)



99
100
101
102
103
104
105
106
# File 'backend/app/converters/lib/csv_converter.rb', line 99

def get_queued_or_new(key)
  if (prox = @batch.working_area.find {|j| j.key == key })
    yield  prox
  elsif (prox = get_new(key))
    yield prox
    @batch << prox
  end
end

- (Object) parse_cell(handler, cell_contents)



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'backend/app/converters/lib/csv_converter.rb', line 84

def parse_cell(handler, cell_contents)

  return nil unless handler

  val = handler.extract_value(cell_contents)

  return nil unless val

  get_queued_or_new(handler.target_key) do |prox|
    property = handler.target_path
    prox.set(property, val)
  end
end

- (Object) parse_row(row)



70
71
72
73
74
75
76
77
78
79
80
81
# File 'backend/app/converters/lib/csv_converter.rb', line 70

def parse_row(row)
  row.each_with_index { |cell, i| parse_cell(@cell_handlers[i], cell) }

  # swap out proxy objects for real JSONModel objects
  @batch.working_area.map! {|proxy| proxy.spawn }.compact!

  # run linking jobs and set defaults
  @batch.working_area.each { |obj| @proxies.discharge_proxy(obj.key, obj) }

  # empty the working area of the cache
  @batch.flush
end

- (Object) run



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'backend/app/converters/lib/csv_converter.rb', line 47

def run

  @cell_handlers = []
  @proxies = ASpaceImport::RecordProxyMgr.new

  CSV.foreach(@input_file, 'r:bom|utf-8') do |row|

    if @cell_handlers.empty?
      @cell_handlers, bad_headers = self.class.configure_cell_handlers(row)
      unless bad_headers.empty?
        Log.warn("Data source has headers that aren't defined: #{bad_headers.join(', ')}")
      end
    else
      parse_row(row)
    end
  end

  @proxies.undischarged.each do |prox|
    Log.warn("Undischarged: #{prox.to_s}")
  end
end

- (Object) set_default(property, val, obj)



137
138
139
140
141
# File 'backend/app/converters/lib/csv_converter.rb', line 137

def set_default(property, val, obj)
  if obj.send("#{property}").nil?
    obj.send("#{property}=", val)
  end
end