module JsonTableSchema::Model

Constants

DEFAULTS

Public Instance Methods

fields() click to toggle source
# File lib/jsontableschema/model.rb, line 15
def fields
  self['fields']
end
foreign_keys() click to toggle source
# File lib/jsontableschema/model.rb, line 23
def foreign_keys
  self['foreignKeys'] || []
end
get_constraints(key) click to toggle source
# File lib/jsontableschema/model.rb, line 31
def get_constraints(key)
  get_field(key)['constraints'] || {}
end
get_field(key) click to toggle source
# File lib/jsontableschema/model.rb, line 46
def get_field(key)
  fields.find { |f| f['name'] == key }
end
get_fields_by_type(type) click to toggle source
# File lib/jsontableschema/model.rb, line 50
def get_fields_by_type(type)
  fields.select { |f| f['type'] == type }
end
get_type(key) click to toggle source
# File lib/jsontableschema/model.rb, line 27
def get_type(key)
  get_field(key)['type']
end
has_field?(key) click to toggle source
# File lib/jsontableschema/model.rb, line 42
def has_field?(key)
  get_field(key) != nil
end
headers() click to toggle source
# File lib/jsontableschema/model.rb, line 9
def headers
  fields.map { |f| transform(f['name']) }
rescue NoMethodError
  []
end
primary_keys() click to toggle source
# File lib/jsontableschema/model.rb, line 19
def primary_keys
  [self['primaryKey']].flatten.reject { |k| k.nil? }
end
required_headers() click to toggle source
# File lib/jsontableschema/model.rb, line 35
def required_headers
  fields.select { |f| f['constraints']!= nil && f['constraints']['required'] == true }
        .map { |f| transform(f['name']) }
rescue NoMethodError
  []
end

Private Instance Methods

expand!() click to toggle source
# File lib/jsontableschema/model.rb, line 61
def expand!
  (self['fields'] || []).each do |f|
    f['type'] = DEFAULTS['type'] if f['type'] == nil
    f['format'] = DEFAULTS['format'] if f['format'] == nil
  end
end
load_fields!() click to toggle source
# File lib/jsontableschema/model.rb, line 68
def load_fields!
  self['fields'] = (self['fields'] || []).map { |f| JsonTableSchema::Field.new(f) }
end
transform(name) click to toggle source
# File lib/jsontableschema/model.rb, line 56
def transform(name)
  name.downcase! if @opts[:case_insensitive_headers]
  name
end