Module: TableSchema::Model

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

Instance Method Summary collapse

Instance Method Details

#add_field(descriptor) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/tableschema/model.rb', line 60

def add_field(descriptor)
  self[:fields].push(descriptor)
  validate!
  descriptor
rescue TableSchema::SchemaException => e
  self[:fields].pop
  raise e if @strict
  nil
end

#fieldsObject



14
15
16
# File 'lib/tableschema/model.rb', line 14

def fields
  self[:fields]
end

#foreign_keysObject



22
23
24
# File 'lib/tableschema/model.rb', line 22

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

#get_constraints(field_name) ⇒ Object



34
35
36
# File 'lib/tableschema/model.rb', line 34

def get_constraints(field_name)
  get_field(field_name)[:constraints] || {}
end

#get_field(field_name) ⇒ Object



52
53
54
# File 'lib/tableschema/model.rb', line 52

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

#get_fields_by_type(type) ⇒ Object



56
57
58
# File 'lib/tableschema/model.rb', line 56

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

#get_type(field_name) ⇒ Object



30
31
32
# File 'lib/tableschema/model.rb', line 30

def get_type(field_name)
  get_field(field_name)[:type]
end

#has_field?(field_name) ⇒ Boolean

Returns:



48
49
50
# File 'lib/tableschema/model.rb', line 48

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

#headersObject Also known as: field_names



6
7
8
9
10
# File 'lib/tableschema/model.rb', line 6

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

#missing_valuesObject



26
27
28
# File 'lib/tableschema/model.rb', line 26

def missing_values
  self.fetch(:missingValues, TableSchema::DEFAULTS[:missing_values])
end

#primary_keysObject



18
19
20
# File 'lib/tableschema/model.rb', line 18

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

#remove_field(field_name) ⇒ Object



70
71
72
73
74
75
# File 'lib/tableschema/model.rb', line 70

def remove_field(field_name)
  field = get_field(field_name)
  self[:fields].reject!{ |f| f.name == field_name }
  validate
  field
end

#required_headersObject



38
39
40
41
# File 'lib/tableschema/model.rb', line 38

def required_headers
  fields.select { |f| f.fetch(:constraints, {}).fetch(:required, nil).to_s == 'true' }
        .map { |f| transform(f[:name]) }
end

#unique_headersObject



43
44
45
46
# File 'lib/tableschema/model.rb', line 43

def unique_headers
  fields.select { |f| f.fetch(:constraints, {}).fetch(:unique, nil).to_s == 'true' }
        .map { |f| transform(f[:name]) }
end