Module: Linkage::Utils

Defined in:
lib/linkage/utils.rb

Constant Summary collapse

TYPE_CONVERSION_TREE =

A “tree” used to find compatible types.

{
  TrueClass => [Integer],
  Integer => [Bignum, Float],
  Bignum => [BigDecimal],
  Float => [BigDecimal],
  BigDecimal => [String],
  String => nil,
  DateTime => nil,
  Date => nil,
  Time => nil,
  File => nil
}

Instance Method Summary collapse

Instance Method Details

#merge_fields(field_1, field_2) ⇒ Array

Create field information for a field that can hold data from two other fields. If the fields have different types, the resulting type is determined via a type-conversion tree.

Parameters:

  • field_1 (Array)

    Schema information for the first field

  • field_2 (Array)

    Schema information for the second field

Returns:

  • (Array)

    Schema information for the new field



24
25
26
27
28
29
30
31
32
33
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/linkage/utils.rb', line 24

def merge_fields(field_1, field_2)
  schema_1 = column_schema_to_ruby_type(field_1)
  schema_1.delete_if { |k, v| v.nil? }
  schema_2 = column_schema_to_ruby_type(field_2)
  schema_2.delete_if { |k, v| v.nil? }
  if schema_1 == schema_2
    result = schema_1
  else
    result = schema_1.dup

    # type
    if schema_1[:type] != schema_2[:type]
      result[:type] = first_common_type(schema_1[:type], schema_2[:type])
    end

    # text
    if schema_1[:text] != schema_2[:text]
      # This can only be of type String.
      result[:text] = true
      result.delete(:size)
    end

    # size
    if !result[:text] && schema_1[:size] != schema_2[:size]
      types = [schema_1[:type], schema_2[:type]].uniq
      if types.length == 1 && types[0] == BigDecimal
        # Two decimals
        if schema_1.has_key?(:size) && schema_2.has_key?(:size)
          s_1 = schema_1[:size]
          s_2 = schema_2[:size]
          result[:size] = [ s_1[0] > s_2[0] ? s_1[0] : s_2[0] ]

          if s_1[1] && s_2[1]
            result[:size][1] = s_1[1] > s_2[1] ? s_1[1] : s_2[1]
          else
            result[:size][1] = s_1[1] ? s_1[1] : s_2[1]
          end
        else
          result[:size] = schema_1.has_key?(:size) ? schema_1[:size] : schema_2[:size]
        end
      elsif types.include?(String) && types.include?(BigDecimal)
        # Add one to the precision of the BigDecimal (for the dot)
        if schema_1.has_key?(:size) && schema_2.has_key?(:size)
          s_1 = schema_1[:size].is_a?(Array) ? schema_1[:size][0] + 1 : schema_1[:size]
          s_2 = schema_2[:size].is_a?(Array) ? schema_2[:size][0] + 1 : schema_2[:size]
          result[:size] = s_1 > s_2 ? s_1 : s_2
        elsif schema_1.has_key?(:size)
          result[:size] = schema_1[:size].is_a?(Array) ? schema_1[:size][0] + 1 : schema_1[:size]
        elsif schema_2.has_key?(:size)
          result[:size] = schema_2[:size].is_a?(Array) ? schema_2[:size][0] + 1 : schema_2[:size]
        end
      else
        # Treat as two strings
        if schema_1.has_key?(:size) && schema_2.has_key?(:size)
          result[:size] = schema_1[:size] > schema_2[:size] ? schema_1[:size] : schema_2[:size]
        elsif schema_1.has_key?(:size)
          result[:size] = schema_1[:size]
        else
          result[:size] = schema_2[:size]
        end
      end
    end

    # fixed
    if schema_1[:fixed] != schema_2[:fixed]
      # This can only be of type String.
      result[:fixed] = true
    end
  end

  {:type => result.delete(:type), :opts => result}
end