Class: TableSchema::Types::Base
- Inherits:
-
Object
- Object
- TableSchema::Types::Base
show all
- Includes:
- Helpers
- Defined in:
- lib/tableschema/types/base.rb
Direct Known Subclasses
Any, Array, Boolean, Date, DateTime, GeoJSON, GeoPoint, Integer, Null, Number, Object, String, Time
Instance Method Summary
collapse
Methods included from Helpers
#convert_to_boolean, #false_values, #get_class_for_type, #true_values, #type_class_lookup
Constructor Details
#initialize(field) ⇒ Base
7
8
9
10
11
12
13
|
# File 'lib/tableschema/types/base.rb', line 7
def initialize(field)
@field = field
@constraints = field['constraints'] || {}
@required = ['true', true].include?(@constraints['required'])
@type = @field['type']
set_format
end
|
Instance Method Details
#cast(value, skip_constraints = false) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/tableschema/types/base.rb', line 15
def cast(value, skip_constraints = false)
TableSchema::Constraints.new(@field, value).validate! unless skip_constraints
return nil if is_null?(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
|
34
35
36
37
38
39
40
|
# File 'lib/tableschema/types/base.rb', line 34
def set_format
if (@field['format'] || '').start_with?('fmt:')
@format, @format_string = *@field['format'].split(':', 2)
else
@format = @field['format'] || 'default'
end
end
|
#test(value) ⇒ Object
27
28
29
30
31
32
|
# File 'lib/tableschema/types/base.rb', line 27
def test(value)
cast(value, true)
true
rescue TableSchema::Exception
false
end
|