class JsonTableSchema::Constraints
Public Class Methods
new(field, value)
click to toggle source
# File lib/jsontableschema/constraints/constraints.rb, line 21 def initialize(field, value) @field = field @value = value @constraints = @field['constraints'] || {} end
Public Instance Methods
validate!()
click to toggle source
# File lib/jsontableschema/constraints/constraints.rb, line 27 def validate! result = true @constraints.each do |c| constraint = c.first if is_supported_type?(constraint) result = self.send("check_#{underscore constraint}") else raise(JsonTableSchema::ConstraintNotSupported.new("The field type `#{@field['type']}` does not support the `#{constraint}` constraint")) end end result end
Private Instance Methods
is_supported_type?(constraint)
click to toggle source
# File lib/jsontableschema/constraints/constraints.rb, line 50 def is_supported_type?(constraint) klass = get_class_for_type(@field['type']) Kernel.const_get(klass).supported_constraints.include?(constraint) end
parse_constraint(constraint)
click to toggle source
# File lib/jsontableschema/constraints/constraints.rb, line 55 def parse_constraint(constraint) if @value.is_a?(::Integer) && constraint.is_a?(::String) constraint.to_i elsif @value.is_a?(::Tod::TimeOfDay) Tod::TimeOfDay.parse(constraint) elsif @value.is_a?(::DateTime) DateTime.parse(constraint) elsif @value.is_a?(::Date) && constraint.is_a?(::String) Date.parse(constraint) elsif @value.is_a?(::Float) && constraint.is_a?(Array) constraint.map { |c| Float(c) } elsif @value.is_a?(Boolean) && constraint.is_a?(Array) constraint.map { |c| convert_to_boolean(c) } elsif @value.is_a?(Date) && constraint.is_a?(Array) constraint.map { |c| Date.parse(c) } else constraint end end
underscore(value)
click to toggle source
# File lib/jsontableschema/constraints/constraints.rb, line 42 def underscore(value) value.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end