class Hyrax::Ingest::Fetcher::CSVFile

Attributes

column[R]
filename[R]
header_row_number[R]
row[R]
row_offset[R]

Public Class Methods

new(options={}) click to toggle source
Calls superclass method Hyrax::Ingest::Fetcher::Base::new
# File lib/hyrax/ingest/fetcher/csv_file.rb, line 15
def initialize(options={})
  raise ArgumentError, "Required option :filename is missing" unless options.key?(:filename)
  raise ArgumentError, "Required option :column is missing" unless options.key?(:column)
  raise ArgumentError, "Required option :row is missing" unless options.key?(:row)

  @filename = options.fetch(:filename)
  @column = options.fetch(:column, '').to_s.strip.downcase
  @row = options.fetch(:row, '').to_s.strip.downcase
  @header_row_number = options.fetch(:header_row_number, 1)
  @row_offset = options[:row_offset]
  super
end

Public Instance Methods

fetch() click to toggle source
# File lib/hyrax/ingest/fetcher/csv_file.rb, line 28
def fetch
  @fetched_value ||= cell_value
end

Private Instance Methods

cell_value() click to toggle source
# File lib/hyrax/ingest/fetcher/csv_file.rb, line 38
def cell_value
  roo.cell(row_number, column_number)
end
column_number() click to toggle source

@return The column number from which to retrieve the cell.

# File lib/hyrax/ingest/fetcher/csv_file.rb, line 43
def column_number
  @column_number ||= column_number_from_header || specific_column_number
  # TODO: custom error
  raise ArgumentError, "Value for column: option must be a number or a column header; '#{column}' was given." if @column_number.nil?
  @column_number
end
column_number_from_header() click to toggle source
# File lib/hyrax/ingest/fetcher/csv_file.rb, line 50
def column_number_from_header
  headers.index(column) + 1 if headers.index(column)
end
headers() click to toggle source
# File lib/hyrax/ingest/fetcher/csv_file.rb, line 77
def headers
  @headers ||= roo.row(header_row_number).map(&:to_s).map(&:downcase)
end
next_row() click to toggle source
# File lib/hyrax/ingest/fetcher/csv_file.rb, line 65
def next_row
  iteration + 1 if @row == 'next'
end
roo() click to toggle source
# File lib/hyrax/ingest/fetcher/csv_file.rb, line 34
def roo
  @roo ||= Roo::CSV.new(sip.find_file_path(filename))
end
row_number() click to toggle source
# File lib/hyrax/ingest/fetcher/csv_file.rb, line 58
def row_number
  @row_number ||= next_row || specific_row_number
  # TODO: custom error
  raise ArgumentError, "Value for row: option must be a number or the keyword 'next'; #{@row} was given." if @row_number.nil?
  @row_number + row_offset
end
specific_column_number() click to toggle source
# File lib/hyrax/ingest/fetcher/csv_file.rb, line 54
def specific_column_number
  column.to_i if string_is_integer?(column)
end
specific_row_number() click to toggle source
# File lib/hyrax/ingest/fetcher/csv_file.rb, line 69
def specific_row_number
  row.to_i if string_is_integer?(row)
end
string_is_integer?(str) click to toggle source
# File lib/hyrax/ingest/fetcher/csv_file.rb, line 81
def string_is_integer?(str)
  # remove leading zeros first
  str.sub!(/^0+/, '')
  str.to_i.to_s == str.to_s
end