Class: RDF::Tabular::Dialect

Inherits:
Metadata show all
Defined in:
lib/rdf/tabular/metadata.rb

Constant Summary collapse

DIALECT_DEFAULTS =

Defaults for dialects

{
  commentPrefix:      nil,
  delimiter:          ",".freeze,
  doubleQuote:        true,
  encoding:           "utf-8".freeze,
  header:             true,
  headerColumnCount:  0,
  headerRowCount:     1,
  lineTerminator:     :auto, # SPEC says "\r\n"
  quoteChar:          '"',
  skipBlankRows:      false,
  skipColumns:        0,
  skipInitialSpace:   false,
  skipRows:           0,
  trim:               false
}.freeze
PROPERTIES =
{
  :@id             => :link,
  :@type           => :atomic,
  commentPrefix:      :atomic,
  delimiter:          :atomic,
  doubleQuote:        :atomic,
  encoding:           :atomic,
  header:             :atomic,
  headerColumnCount:  :atomic,
  headerRowCount:     :atomic,
  lineTerminator:     :atomic,
  quoteChar:          :atomic,
  skipBlankRows:      :atomic,
  skipColumns:        :atomic,
  skipInitialSpace:   :atomic,
  skipRows:           :atomic,
  trim:               :atomic,
}.freeze
REQUIRED =
[].freeze

Constants inherited from Metadata

Metadata::DATATYPES, Metadata::INHERITED_PROPERTIES, Metadata::LOCAL_CONTEXT, Metadata::NAME_SYNTAX

Instance Attribute Summary

Attributes inherited from Metadata

#filenames, #id, #object, #parent, #url

Instance Method Summary collapse

Methods inherited from Metadata

#==, #[], #[]=, #base, #common_properties, #context, #dialect, #dialect=, #each, #each_row, #errors, for_input, #has_annotations?, #initialize, #inspect, #merge, #merge!, new, #normalize!, #normalize_datatype, #normalize_jsonld, open, #to_json, #type, #valid?, #valid_inherited_property?, #valid_natural_language_property?, #validate!

Methods included from Utils

debug, #depth

Constructor Details

This class inherits a constructor from RDF::Tabular::Metadata

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Logic for accessing elements as accessors



1681
1682
1683
1684
1685
1686
1687
1688
# File 'lib/rdf/tabular/metadata.rb', line 1681

def method_missing(method, *args)
  if DIALECT_DEFAULTS.has_key?(method.to_sym)
    # As set, or with default
    object.fetch(method.to_sym, DIALECT_DEFAULTS[method.to_sym])
  else
    super
  end
end

Instance Method Details

#embedded_metadata(input, options = {}) ⇒ Metadata

Extract a new Metadata document from the file or data provided

Parameters:

  • input (#read, #to_s)

    IO, or file path or URL

  • options (Hash{Symbol => Object}) (defaults to: {})

    any additional options (see ‘RDF::Util::File.open_file`)

Returns:

See Also:



1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
# File 'lib/rdf/tabular/metadata.rb', line 1624

def (input, options = {})
  options = options.dup
  options.delete(:context) # Don't accidentally use a passed context
  # Normalize input to an IO object
  if !input.respond_to?(:read)
    return ::RDF::Util::File.open_file(input.to_s) {|f| (f, options.merge(base: input.to_s))}
  end

  table = {
    "url" => (options.fetch(:base, "")),
    "@type" => "Table",
    "tableSchema" => {
      "@type" => "Schema",
      "columns" => []
    }
  }

  # Set encoding on input
  csv = ::CSV.new(input, csv_options)
  (1..skipRows.to_i).each do
    value = csv.shift.join(delimiter)  # Skip initial lines, these form comment annotations
    # Trim value
    value.lstrip! if %w(true start).include?(trim.to_s)
    value.rstrip! if %w(true end).include?(trim.to_s)

    value = value[1..-1].strip if commentPrefix && value.start_with?(commentPrefix)
    (table["rdfs:comment"] ||= []) << value unless value.empty?
  end
  debug("embedded_metadata") {"notes: #{table["notes"].inspect}"}

  (1..headerRowCount).each do
    row_data = Array(csv.shift)
    Array(row_data).each_with_index do |value, index|
      # Skip columns
      skipCols = skipColumns.to_i + headerColumnCount.to_i
      next if index < skipCols

      # Trim value
      value.lstrip! if %w(true start).include?(trim.to_s)
      value.rstrip! if %w(true end).include?(trim.to_s)

      # Initialize title
      # SPEC CONFUSION: does title get an array, or concatenated values?
      columns = table["tableSchema"]["columns"] ||= []
      column = columns[index - skipCols] ||= {
        "title" => {"und" => []},
      }
      column["title"]["und"] << value
    end
  end
  debug("embedded_metadata") {"table: #{table.inspect}"}
  input.rewind if input.respond_to?(:rewind)

  Table.new(table, options.merge(reason: "load embedded metadata: #{table['@id']}"))
end

#escape_characterString

escape character

Returns:

  • (String)


1600
1601
1602
# File 'lib/rdf/tabular/metadata.rb', line 1600

def escape_character
  self.doubleQuote ? '"' : '\\'
end

#headerRowCountInteger

default for headerRowCount is zero if header is false

Returns:

  • (Integer)


1606
1607
1608
# File 'lib/rdf/tabular/metadata.rb', line 1606

def headerRowCount
  object.fetch(:headerRowCount, self.header ? 1 : 0)
end

#trimBoolean, String

default for trim comes from skipInitialSpace

Returns:

  • (Boolean, String)


1612
1613
1614
# File 'lib/rdf/tabular/metadata.rb', line 1612

def trim
  object.fetch(:trim, self.skipInitialSpace ? 'start' : false)
end