Class: TableSchema::Types::Base

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/tableschema/types/base.rb

Instance Method Summary collapse

Methods included from Helpers

#deep_symbolize_keys, #get_class_for_type, #type_class_lookup

Constructor Details

#initialize(field) ⇒ Base

Returns a new instance of Base.



8
9
10
11
# File 'lib/tableschema/types/base.rb', line 8

def initialize(field)
  @field = field
  set_format
end

Instance Method Details

#cast(value) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/tableschema/types/base.rb', line 13

def cast(value)
  send("cast_#{@format}", value)
rescue NoMethodError => e
  if e.message.start_with?('undefined method `cast_')
    raise(TableSchema::InvalidFormat.new("The format `#{@format}` is not supported by the type `#{@type}`"))
  else
    raise e
  end
end

#test(value) ⇒ Object



23
24
25
26
27
28
# File 'lib/tableschema/types/base.rb', line 23

def test(value)
  cast(value)
  true
rescue TableSchema::Exception
  false
end