class FlatKit::FieldType

Constants

CoerceFailure

Public Class Methods

best_guess(data) click to toggle source
# File lib/flat_kit/field_type.rb, line 12
def self.best_guess(data)
  candidate_types(data).sort_by { |t| t.weight }.last
end
candidate_types(data) click to toggle source
# File lib/flat_kit/field_type.rb, line 8
def self.candidate_types(data)
  find_children(:matches?, data)
end
coerce(data) click to toggle source
# File lib/flat_kit/field_type.rb, line 24
def self.coerce(data)
  raise NotImplementedError, "must implement #{self.name}.coerce(data)"
end
matches?(data) click to toggle source
# File lib/flat_kit/field_type.rb, line 20
def self.matches?(data)
  raise NotImplementedError, "must implement #{self.name}.matches?(data)"
end
type_name() click to toggle source
# File lib/flat_kit/field_type.rb, line 16
def self.type_name
  raise NotImplementedError, "must impleent #{self.type_name}"
end
weight() click to toggle source

Each type has a weight so if a value matches multiple types, then the list can be compared to see where the tie breakers are

All the weights are here so that

# File lib/flat_kit/field_type.rb, line 34
def self.weight
  # Boolean has crossover with Integer so going to let it overrule Integer
  return 5 if self == BooleanType


  # Integer could potentially overlap with Float, but it is more restrictive
  # so let it override Flaot
  return 4 if self == IntegerType
  return 3 if self == FloatType

  # Date and Timestamps string representation shouldn't intersect with anything so
  # leaving it at the same level as Null and Unkonwn
  return 2 if self == DateType
  return 2 if self == TimestampType

  # Null and Unknown shoulnd't conflict since their string representations
  # do not intersect
  return 2 if self == NullType
  return 2 if self == UnknownType

  # Stringtype is the fallback for anything that has a string
  # representation, so it should lose out on integers, floats, nulls,
  # unknowns as strings
  return 1 if self == StringType

  # at the bottom - since it should never match anywhere
  return 0 if self == GuessType

  raise NotImplementedError, "No weight assigned to type #{self} - fix immediately"
end