class TableSchema::Constraints

Public Class Methods

new(field, value) click to toggle source
# File lib/tableschema/constraints/constraints.rb, line 23
def initialize(field, value)
  @field = field
  @value = value
  @constraints = ordered_constraints
end

Public Instance Methods

validate!() click to toggle source
# File lib/tableschema/constraints/constraints.rb, line 29
def validate!
  result = true
  @constraints.each do |c|
    constraint = c.first.to_s
    if is_supported_type?(constraint)
      result = self.send("check_#{underscore constraint}")
    else
      raise(TableSchema::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/tableschema/constraints/constraints.rb, line 58
def is_supported_type?(constraint)
  klass = get_class_for_type(@field[:type])
  Kernel.const_get(klass).supported_constraints.include?(constraint)
end
ordered_constraints() click to toggle source
# File lib/tableschema/constraints/constraints.rb, line 52
def ordered_constraints
  constraints = @field.fetch(:constraints, {})
  ordered_constraints = constraints.select{ |key| key == :required}
  ordered_constraints.merge!(constraints)
end
underscore(value) click to toggle source
# File lib/tableschema/constraints/constraints.rb, line 44
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