Class: RDF::Tabular::Row

Inherits:
Object
  • Object
show all
Defined in:
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:

  • row (Array<Array<String>>)
  • metadata (Metadata)

    for Table

  • number (Integer)

    1-based row number after skipped/header rows

  • source_number (Integer)

    1-based row number from source

  • options (Hash{Symbol => Object})

    ({})

Options Hash (**options):

  • :validate (Boolean)

    check for PK/FK consistency



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
2120
2121
2122
2123
2124
2125
2126
# File 'lib/rdf/tabular/metadata.rb', line 2036

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" => CGI.unescape(cell.column.name),
      "_column" => cell.column.number,
      "_sourceColumn" => cell.column.sourceNumber
    )
    cell.set_urls(mapped_values, options[:decode_uri])
  end
end

Instance Attribute Details

#contextJSON::LD::Context (readonly)

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

Returns:

  • (JSON::LD::Context)


2026
2027
2028
# File 'lib/rdf/tabular/metadata.rb', line 2026

def context
  @context
end

#numberInteger (readonly)

Row number of this row

Returns:

  • (Integer)


2002
2003
2004
# File 'lib/rdf/tabular/metadata.rb', line 2002

def number
  @number
end

#primaryKeyArray<Cell> (readonly)

Cells providing a unique row identifier

Returns:



2016
2017
2018
# File 'lib/rdf/tabular/metadata.rb', line 2016

def primaryKey
  @primaryKey
end

#sourceNumberInteger (readonly)

Row number of this row from the original source

Returns:

  • (Integer)


2006
2007
2008
# File 'lib/rdf/tabular/metadata.rb', line 2006

def sourceNumber
  @sourceNumber
end

#tableTable (readonly)

Table containing this row

Returns:



2011
2012
2013
# File 'lib/rdf/tabular/metadata.rb', line 2011

def table
  @table
end

#titlesArray<RDF::Literal> (readonly)

Title(s) of this row

Returns:

  • (Array<RDF::Literal>)


2021
2022
2023
# File 'lib/rdf/tabular/metadata.rb', line 2021

def titles
  @titles
end

#valuesObject (readonly)

Row values, hashed by name



1998
1999
2000
# File 'lib/rdf/tabular/metadata.rb', line 1998

def values
  @values
end

Instance Method Details

#idRDF::URI

Identifier for this row, as an RFC7111 fragment

Returns:

  • (RDF::URI)


2130
2131
2132
2133
2134
# File 'lib/rdf/tabular/metadata.rb', line 2130

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

#inspectObject



2148
2149
2150
# File 'lib/rdf/tabular/metadata.rb', line 2148

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

#to_atdObject

Return Annotated Row representation



2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
# File 'lib/rdf/tabular/metadata.rb', line 2137

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