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

Overview

Internal: The base class for all field types

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

rubocop:disable Style/RedundantSort We need the stable sort, max_by(&:weight) returns the wrong one



47
48
49
# File 'lib/flat_kit/field_type.rb', line 47

def self.best_guess(data)
  candidate_types(data).sort_by(&:weight).last
end

.candidate_types(data) ⇒ Object



41
42
43
# File 'lib/flat_kit/field_type.rb', line 41

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

.coerce(data) ⇒ Object

Raises:

  • (NotImplementedError)


60
61
62
# File 'lib/flat_kit/field_type.rb', line 60

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

.matches?(data) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


56
57
58
# File 'lib/flat_kit/field_type.rb', line 56

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

.type_nameObject

rubocop:enable Style/RedundantSort

Raises:

  • (NotImplementedError)


52
53
54
# File 'lib/flat_kit/field_type.rb', line 52

def self.type_name
  raise NotImplementedError, "must impleent #{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 we can see the order of precedence



69
70
71
# File 'lib/flat_kit/field_type.rb', line 69

def self.weight
  weights.fetch(self) { raise NotImplementedError, "No weight assigned to type #{self} - fix immediately" }
end

.weightsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/flat_kit/field_type.rb', line 11

def self.weights
  @weights ||= {
    # Boolean has crossover with Integer so going to let it overrule Integer
    BooleanType => 5,

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

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

    # Null and Unknown shoulnd't conflict since their string representations
    # do not intersect
    NullType => 2,
    UnknownType => 2,

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

    # at the bottom - since it should never match anywhere
    GuessType => 0,
  }
end