Class: RDF::Tabular::Schema

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

Constant Summary collapse

PROPERTIES =
{
  :@id       => :link,
  :@type     => :atomic,
  columns:      :array,
  foreignKeys:  :array,
  primaryKey:   :column_reference,
  rowTitles:    :column_reference,
}.freeze
DEFAULTS =
{}.freeze
REQUIRED =
[].freeze

Constants inherited from Metadata

Metadata::DATATYPES, Metadata::INHERITED_DEFAULTS, 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, #datatype=, #default_value, #describes_file?, #dialect, #dialect=, #each, #each_row, for_input, #has_annotations?, #initialize, #inspect, #normalize!, #normalize_jsonld, open, #root, #set_array_value, #set_nl, #set_property, site_wide_config, #tableSchema=, #tables=, #to_json, #transformations=, #type, #valid?, #valid_natural_language_property?, #validate, #validate!, #verify_compatible!

Constructor Details

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

Instance Method Details

#columns=(value) ⇒ Object



1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
# File 'lib/rdf/tabular/metadata.rb', line 1490

def columns=(value)
  object[:columns] = case value
  when Array
    number = 0
    value.map do |v|
      number += 1
      case v
      when Hash
        Column.new(v, **@options.merge(
          table: (parent if parent.is_a?(Table)),
          parent: self,
          context: nil,
          number: number))
      else
        v
      end
    end
  else
    log_warn "#{type} has invalid property 'columns': expected array of Column"
    []
  end

  unless object[:columns].all? {|v| v.is_a?(Column)}
    log_warn "#{type} has invalid property 'columns': expected array of Column"
    # Remove elements that aren't of the right types
    object[:columns] = object[:columns].select! {|v| v.is_a?(Column)}
  end
end

#foreign_keys_referencing(table) ⇒ Array<Hash>

List of foreign keys referencing the specified table

Parameters:

Returns:

  • (Array<Hash>)


1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
# File 'lib/rdf/tabular/metadata.rb', line 1539

def foreign_keys_referencing(table)
  Array(foreignKeys).select do |fk|
    reference = fk['reference']
    if reference['resource']
      ref = context.base.join(reference['resource']).to_s
      table.url == ref
    else # schemaReference
      ref = context.base.join(reference['schemaReference']).to_s
      table.tableSchema.id == ref
    end
  end
end

#foreignKeys=(value) ⇒ Object



1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
# File 'lib/rdf/tabular/metadata.rb', line 1519

def foreignKeys=(value)
  object[:foreignKeys] = case value
  when Array then value
  else
    log_warn "#{type} has invalid property 'foreignKeys': expected array of ForeignKey"
    []
  end

  unless object[:foreignKeys].all? {|v| v.is_a?(Hash)}
    log_warn "#{type} has invalid property 'foreignKeys': expected array of ForeignKey"
    # Remove elements that aren't of the right types
    object[:foreignKeys] = object[:foreignKeys].select! {|v| v.is_a?(Hash)}
  end
end

#to_atdObject

Return Annotated Table representation



1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
# File 'lib/rdf/tabular/metadata.rb', line 1553

def to_atd
  object.inject({
    "@id" => (id.to_s if id),
    "@type" => "Schema",
    "columns" => Array(columns).map(&:to_atd),
  }) do |memo, (k, v)|
    memo[k.to_s] ||= v
    memo
  end.delete_if {|k,v| v.nil? || v.is_a?(Metadata)}
end