Module: JsonTableSchema::Model

Included in:
Schema
Defined in:
lib/jsontableschema/model.rb

Constant Summary collapse

DEFAULTS =
{
  'format' => 'default',
  'type' => 'string'
}

Instance Method Summary collapse

Instance Method Details

#fieldsObject



15
16
17
# File 'lib/jsontableschema/model.rb', line 15

def fields
  self['fields']
end

#foreign_keysObject



23
24
25
# File 'lib/jsontableschema/model.rb', line 23

def foreign_keys
  self['foreignKeys'] || []
end

#get_constraints(key) ⇒ Object



31
32
33
# File 'lib/jsontableschema/model.rb', line 31

def get_constraints(key)
  get_field(key)['constraints'] || {}
end

#get_field(key) ⇒ Object



46
47
48
# File 'lib/jsontableschema/model.rb', line 46

def get_field(key)
  fields.find { |f| f['name'] == key }
end

#get_fields_by_type(type) ⇒ Object



50
51
52
# File 'lib/jsontableschema/model.rb', line 50

def get_fields_by_type(type)
  fields.select { |f| f['type'] == type }
end

#get_type(key) ⇒ Object



27
28
29
# File 'lib/jsontableschema/model.rb', line 27

def get_type(key)
  get_field(key)['type']
end

#has_field?(key) ⇒ Boolean

Returns:



42
43
44
# File 'lib/jsontableschema/model.rb', line 42

def has_field?(key)
  get_field(key) != nil
end

#headersObject



9
10
11
12
13
# File 'lib/jsontableschema/model.rb', line 9

def headers
  fields.map { |f| transform(f['name']) }
rescue NoMethodError
  []
end

#primary_keysObject



19
20
21
# File 'lib/jsontableschema/model.rb', line 19

def primary_keys
  [self['primaryKey']].flatten.reject { |k| k.nil? }
end

#required_headersObject



35
36
37
38
39
40
# 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