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) ⇒ 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



1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
# File 'lib/rdf/tabular/metadata.rb', line 1757

def initialize(row, , number, source_number)
  @table = 
  @number = number
  @sourceNumber = source_number
  @values = []
  skipColumns = .dialect.skipColumns.to_i + .dialect.headerColumnCount.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 ||= []

  # 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({}, table: , parent: .tableSchema, number: index + 1 - skipColumns)

    column = columns[index - skipColumns]

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

    datatype = .normalize_datatype(column.datatype || 'string')
    value = value.gsub(/\r\t\a/, ' ') unless %w(string json xml html anyAtomicType any).include?(datatype[:base])
    value = value.strip.gsub(/\s+/, ' ') unless %w(string json xml html anyAtomicType any 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 any).include?(datatype[:base])
      v = column.default || '' if v.empty?
      if Array(column.null).include?(v)
        nil
      else
        # Trim value
        if %w(string anyAtomicType any).include?(datatype[:base])
          v.lstrip! if %w(true start).include?(.dialect.trim.to_s)
          v.rstrip! if %w(true end).include?(.dialect.trim.to_s)
        else
          # unless the datatype is string or anyAtomicType or any, strip leading and trailing whitespace from the string value
          v.strip!
        end

        expanded_dt = .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)
        end
      end
    end.compact

    cell.value = (column.separator ? cell_values : cell_values.first)
    cell.errors = cell_errors
    .send(:debug, "#{self.number}: each_cell ##{self.sourceNumber},#{cell.column.sourceNumber}", cell.errors.join("\n")) unless cell_errors.empty?

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

  # 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:

  • (JSON::LD::Context)


1749
1750
1751
# File 'lib/rdf/tabular/metadata.rb', line 1749

def context
  @context
end

#numberInteger (readonly)

Row number of this row

Returns:

  • (Integer)


1735
1736
1737
# File 'lib/rdf/tabular/metadata.rb', line 1735

def number
  @number
end

#sourceNumberInteger (readonly)

Row number of this row from the original source

Returns:

  • (Integer)


1739
1740
1741
# File 'lib/rdf/tabular/metadata.rb', line 1739

def sourceNumber
  @sourceNumber
end

#tableTable (readonly)

Table containing this row

Returns:



1744
1745
1746
# File 'lib/rdf/tabular/metadata.rb', line 1744

def table
  @table
end

#valuesObject (readonly)

Row values, hashed by ‘name`



1731
1732
1733
# File 'lib/rdf/tabular/metadata.rb', line 1731

def values
  @values
end

Instance Method Details

#idRDF::URI

Identifier for this row, as an RFC7111 fragment

Returns:

  • (RDF::URI)


1842
# File 'lib/rdf/tabular/metadata.rb', line 1842

def id; table.url + "#row=#{self.sourceNumber}"; end

#to_atdObject

Return Annotated Row representation



1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
# File 'lib/rdf/tabular/metadata.rb', line 1845

def to_atd
  {
    "@id" => self.id,
    "@type" => "Row",
    "table" => table.id,
    "number" => self.number,
    "sourceNumber" => self.sourceNumber,
    "cells" => @values.map(&:to_atd)
  }
end