Class: FlatKit::FieldType

Inherits:
Object
  • Object
show all
Extended by:
DescendantTracker
Defined in:
lib/flat_kit/field_type.rb,
lib/flat_kit/field_type/date_type.rb,
lib/flat_kit/field_type/null_type.rb,
lib/flat_kit/field_type/float_type.rb,
lib/flat_kit/field_type/guess_type.rb,
lib/flat_kit/field_type/string_type.rb,
lib/flat_kit/field_type/boolean_type.rb,
lib/flat_kit/field_type/integer_type.rb,
lib/flat_kit/field_type/unknown_type.rb,
lib/flat_kit/field_type/timestamp_type.rb

Defined Under Namespace

Classes: BooleanType, DateType, FloatType, GuessType, IntegerType, NullType, StringType, TimestampType, UnknownType

Constant Summary collapse

CoerceFailure =
Class.new(::Object).freeze

Class Method Summary collapse

Methods included from DescendantTracker

children, find_child, find_children, inherited

Class Method Details

.best_guess(data) ⇒ Object



12
13
14
# 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) ⇒ Object



8
9
10
# File 'lib/flat_kit/field_type.rb', line 8

def self.candidate_types(data)
  find_children(:matches?, data)
end

.coerce(data) ⇒ Object

Raises:

  • (NotImplementedError)


24
25
26
# File 'lib/flat_kit/field_type.rb', line 24

def self.coerce(data)
  raise NotImplementedError, "must implement #{self.name}.coerce(data)"
end

.matches?(data) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


20
21
22
# File 'lib/flat_kit/field_type.rb', line 20

def self.matches?(data)
  raise NotImplementedError, "must implement #{self.name}.matches?(data)"
end

.type_nameObject

Raises:

  • (NotImplementedError)


16
17
18
# File 'lib/flat_kit/field_type.rb', line 16

def self.type_name
  raise NotImplementedError, "must impleent #{self.type_name}"
end

.weightObject

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

Raises:

  • (NotImplementedError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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