Class: ROM::SQL::Schema::TypeBuilder Private

Inherits:
Object
  • Object
show all
Extended by:
Dry::Core::ClassAttributes
Defined in:
lib/rom/sql/schema/type_builder.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

DECIMAL_REGEX =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/(?:decimal|numeric)\((\d+)(?:,\s*(\d+))?\)/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](db_type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



16
17
18
# File 'lib/rom/sql/schema/type_builder.rb', line 16

def self.[](db_type)
  registry[db_type]
end

.register(db_type, builder) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



12
13
14
# File 'lib/rom/sql/schema/type_builder.rb', line 12

def self.register(db_type, builder)
  registry[db_type] = builder
end

Instance Method Details

#call(primary_key:, db_type:, type:, allow_null:, **rest) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rom/sql/schema/type_builder.rb', line 40

def call(primary_key:, db_type:, type:, allow_null:, **rest)
  if primary_key
    map_pk_type(type, db_type, **rest)
  else
    mapped_type = map_type(type, db_type, **rest)

    if mapped_type
      read_type = mapped_type.meta[:read]

      if read_type && allow_null
        mapped_type.optional.meta(read: read_type.optional)
      elsif allow_null
        mapped_type.optional
      else
        mapped_type
      end
    end
  end
end

#map_decimal_type(type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rom/sql/schema/type_builder.rb', line 79

def map_decimal_type(type)
  precision = DECIMAL_REGEX.match(type)

  if precision
    prcsn, scale = precision[1..2].map(&:to_i)

    self.class.ruby_type_mapping[:decimal].meta(
      precision: prcsn,
      scale: scale
    )
  else
    self.class.ruby_type_mapping[:decimal]
  end
end

#map_pk_type(_ruby_type, _db_type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



61
62
63
# File 'lib/rom/sql/schema/type_builder.rb', line 61

def map_pk_type(_ruby_type, _db_type, **)
  self.class.numeric_pk_type.meta(primary_key: true)
end

#map_type(ruby_type, db_type, **kw) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rom/sql/schema/type_builder.rb', line 66

def map_type(ruby_type, db_type, **kw)
  type = self.class.ruby_type_mapping[ruby_type]

  if db_type.is_a?(String) && db_type.include?('numeric') || db_type.include?('decimal')
    map_decimal_type(db_type)
  elsif db_type.is_a?(String) && db_type.include?('char') && kw[:max_length]
    type.meta(limit: kw[:max_length])
  else
    type
  end
end