Class: Ddr::Index::CSVQueryResult

Inherits:
AbstractQueryResult show all
Defined in:
lib/ddr/index/csv_query_result.rb

Constant Summary collapse

MAX_ROWS =
10**8
CSV_MV_SEPARATOR =
";"
DESCMD_HEADER_CONVERTER =
lambda { |header|
  if term = Ddr::Models::DescriptiveMetadata.mapping[header.to_sym]
    term.unqualified_name.to_s
  else
    header
  end
}

Instance Attribute Summary

Attributes inherited from AbstractQueryResult

#conn, #query

Instance Method Summary collapse

Methods inherited from AbstractQueryResult

#count, #initialize

Constructor Details

This class inherits a constructor from Ddr::Index::AbstractQueryResult

Instance Method Details

#[](index_or_header) ⇒ Object



27
28
29
# File 'lib/ddr/index/csv_query_result.rb', line 27

def [](index_or_header)
  table.by_col_or_row![index_or_header]
end

#csv_headersObject



55
56
57
# File 'lib/ddr/index/csv_query_result.rb', line 55

def csv_headers
  query.fields.empty? ? :first_row : query_field_headings
end

#csv_optsObject



35
36
37
38
39
40
41
# File 'lib/ddr/index/csv_query_result.rb', line 35

def csv_opts
  { headers:           csv_headers,
    return_headers:    false,
    write_headers:     true,
    header_converters: [ DESCMD_HEADER_CONVERTER ],
  }
end

#dataObject



71
72
73
74
# File 'lib/ddr/index/csv_query_result.rb', line 71

def data
  raw = conn.get("select", params: solr_csv_params)
  raw.gsub(/\\#{CSV_MV_SEPARATOR}/, CSV_MV_SEPARATOR)
end

#delete_empty_columns!Object



19
20
21
# File 'lib/ddr/index/csv_query_result.rb', line 19

def delete_empty_columns!
  table.by_col!.delete_if { |c, vals| vals.all?(&:nil?) }
end

#each(&block) ⇒ Object



23
24
25
# File 'lib/ddr/index/csv_query_result.rb', line 23

def each(&block)
  table.by_row!.each(&block)
end

#query_field_headingsObject



51
52
53
# File 'lib/ddr/index/csv_query_result.rb', line 51

def query_field_headings
  query.fields.map { |f| f.respond_to?(:heading) ? f.heading : f.to_s }
end

#solr_csv_header?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/ddr/index/csv_query_result.rb', line 59

def solr_csv_header?
  csv_headers == :first_row
end

#solr_csv_optsObject



43
44
45
46
47
48
49
# File 'lib/ddr/index/csv_query_result.rb', line 43

def solr_csv_opts
  { "csv.mv.separator" => CSV_MV_SEPARATOR,
    "csv.header"       => solr_csv_header?,
    "rows"             => solr_csv_rows,
    "wt"               => "csv",
  }
end

#solr_csv_paramsObject



67
68
69
# File 'lib/ddr/index/csv_query_result.rb', line 67

def solr_csv_params
  params.merge(solr_csv_opts)
end

#solr_csv_rowsObject



63
64
65
# File 'lib/ddr/index/csv_query_result.rb', line 63

def solr_csv_rows
  query.rows || MAX_ROWS
end

#tableObject



31
32
33
# File 'lib/ddr/index/csv_query_result.rb', line 31

def table
  @table ||= CSV.parse(data, csv_opts)
end