Class: RDF::Tabular::Row

Inherits:
Object show all
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/rdf-tabular-40be65aa0ee4/lib/rdf/tabular/metadata.rb

Overview

Wraps each resulting row

Defined Under Namespace

Classes: Cell

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row, metadata, number, source_number, options = {}) ⇒ Row

Parameters:

Options Hash (options):

  • :validate (Boolean)

    check for PK/FK consistency



2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-tabular-40be65aa0ee4/lib/rdf/tabular/metadata.rb', line 2029

def initialize(row, , number, source_number, options = {})
  @table = 
  @number = number
  @sourceNumber = source_number
  @values = []
  skipColumns = .dialect.skipColumns.to_i

  @context = table.context.dup
  @context.base = table.url

  # Create values hash
  # SPEC CONFUSION: are values pre-or-post conversion?
  map_values = {"_row" => number, "_sourceRow" => source_number}

  columns = .tableSchema.columns ||= []
  non_virtual_columns = columns.reject(&:virtual)

  if row.length < non_virtual_columns.length
    raise Error, "Row #{source_number} has #{row.length} columns, expected #{non_virtual_columns.length}"
  end

  # Make sure that the row length is at least as long as the number of column definitions, to implicitly include virtual columns
  columns.each_with_index {|c, index| row[index] ||= c.null}

  row.each_with_index do |value, index|

    next if index < skipColumns

    cell_errors = []

    # create column if necessary
    columns[index - skipColumns] ||=
      Column.new({}, options.merge(table: , parent: .tableSchema, number: index + 1 - skipColumns))

    column = columns[index - skipColumns]

    @values << cell = Cell.new(, column, self, value)

    datatype = column.datatype || Datatype.new({base: "string"}, options.merge(parent: column))
    value = value.gsub(/\r\n\t/, ' ') unless %w(string json xml html anyAtomicType).include?(datatype.base)
    value = value.strip.gsub(/\s+/, ' ') unless %w(string json xml html anyAtomicType normalizedString).include?(datatype.base)
    # if the resulting string is an empty string, apply the remaining steps to the string given by the default property
    value = column.default || '' if value.empty?

    cell_values = column.separator ? value.split(column.separator) : [value]

    cell_values = cell_values.map do |v|
      v = v.strip unless %w(string anyAtomicType).include?(datatype.base)
      v = column.default || '' if v.empty?
      if Array(column.null).include?(v)
        nil
      else
        expanded_dt = datatype.id || .context.expand_iri(datatype.base, vocab: true)
        if (lit_or_errors = value_matching_datatype(v.dup, datatype, expanded_dt, column.lang)).is_a?(RDF::Literal)
          lit_or_errors
        else
          cell_errors += lit_or_errors
          RDF::Literal(v, language: (column.lang unless column.lang == "und"))
        end
      end
    end.compact

    # Check for required values
    if column.required && (cell_values.any? {|v| v.to_s.empty?} || cell_values.empty?)
      cell_errors << "Required column has empty value(s): #{cell_values.map(&:to_s).inspect}"
    end
    cell.value = (column.separator ? cell_values : cell_values.first)
    cell.errors = cell_errors

    map_values[columns[index - skipColumns].name] = (column.separator ? cell_values.map(&:to_s) : cell_values.first.to_s)
  end

  # Record primaryKey if validating
  @primaryKey = @values.
    select {|cell| Array(table.tableSchema.primaryKey).include?(cell.column.name)} if options[:validate]

  # Record any row titles
  @titles = @values.
    select {|cell| Array(table.tableSchema.rowTitles).include?(cell.column.name)}.
    map(&:value)

  # Map URLs for row
  @values.each_with_index do |cell, index|
    mapped_values = map_values.merge(
      "_name" => URI.decode(cell.column.name),
      "_column" => cell.column.number,
      "_sourceColumn" => cell.column.sourceNumber
    )
    cell.set_urls(mapped_values)
  end
end

Instance Attribute Details

#contextJSON::LD::Context (readonly)

Context from Table with base set to table URL for expanding URI Templates

Returns:



2019
2020
2021
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-tabular-40be65aa0ee4/lib/rdf/tabular/metadata.rb', line 2019

def context
  @context
end

#numberInteger (readonly)

Row number of this row

Returns:



1995
1996
1997
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-tabular-40be65aa0ee4/lib/rdf/tabular/metadata.rb', line 1995

def number
  @number
end

#primaryKeyArray<Cell> (readonly)

Cells providing a unique row identifier

Returns:



2009
2010
2011
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-tabular-40be65aa0ee4/lib/rdf/tabular/metadata.rb', line 2009

def primaryKey
  @primaryKey
end

#sourceNumberInteger (readonly)

Row number of this row from the original source

Returns:



1999
2000
2001
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-tabular-40be65aa0ee4/lib/rdf/tabular/metadata.rb', line 1999

def sourceNumber
  @sourceNumber
end

#tableTable (readonly)

Table containing this row

Returns:



2004
2005
2006
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-tabular-40be65aa0ee4/lib/rdf/tabular/metadata.rb', line 2004

def table
  @table
end

#titlesArray<RDF::Literal> (readonly)

Title(s) of this row

Returns:



2014
2015
2016
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-tabular-40be65aa0ee4/lib/rdf/tabular/metadata.rb', line 2014

def titles
  @titles
end

#valuesObject (readonly)

Row values, hashed by name



1991
1992
1993
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-tabular-40be65aa0ee4/lib/rdf/tabular/metadata.rb', line 1991

def values
  @values
end

Instance Method Details

#idRDF::URI

Identifier for this row, as an RFC7111 fragment

Returns:



2123
2124
2125
2126
2127
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-tabular-40be65aa0ee4/lib/rdf/tabular/metadata.rb', line 2123

def id;
  u = table.url.dup
  u.fragment = "row=#{self.sourceNumber}"
  u
end

#inspectObject



2141
2142
2143
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-tabular-40be65aa0ee4/lib/rdf/tabular/metadata.rb', line 2141

def inspect
  self.class.name + to_atd.inspect
end

#to_atdObject

Return Annotated Row representation



2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-tabular-40be65aa0ee4/lib/rdf/tabular/metadata.rb', line 2130

def to_atd
  {
    "@id" => id.to_s,
    "@type" => "Row",
    "table" => (table.id || table.url),
    "number" => self.number,
    "sourceNumber" => self.sourceNumber,
    "cells" => @values.map(&:value)
  }.delete_if {|k,v| v.nil?}
end