Class: JsonTableSchema::Types::Base
- Inherits:
-
Object
- Object
- JsonTableSchema::Types::Base
- Includes:
- Helpers
- Defined in:
- lib/jsontableschema/types/base.rb
Direct Known Subclasses
Any, Array, Boolean, Date, DateTime, GeoJSON, GeoPoint, Integer, Null, Number, Object, String, Time
Instance Method Summary collapse
- #cast(value, skip_constraints = false) ⇒ Object
-
#initialize(field) ⇒ Base
constructor
A new instance of Base.
- #set_format ⇒ Object
- #test(value) ⇒ Object
Methods included from Helpers
#convert_to_boolean, #false_values, #get_class_for_type, #true_values, #type_class_lookup
Constructor Details
#initialize(field) ⇒ Base
Returns a new instance of Base.
7 8 9 10 11 12 13 |
# File 'lib/jsontableschema/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/jsontableschema/types/base.rb', line 15 def cast(value, skip_constraints = false) JsonTableSchema::Constraints.new(@field, value).validate! unless skip_constraints return nil if is_null?(value) send("cast_#{@format}", value) rescue NoMethodError => e if e..start_with?('undefined method `cast_') raise(JsonTableSchema::InvalidFormat.new("The format `#{@format}` is not supported by the type `#{@type}`")) else raise e end end |
#set_format ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/jsontableschema/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/jsontableschema/types/base.rb', line 27 def test(value) cast(value, true) true rescue JsonTableSchema::Exception false end |