class ROM::SQL::Schema::TypeBuilder

@api private

Constants

DECIMAL_REGEX

Public Class Methods

[](db_type) click to toggle source
# File lib/rom/sql/schema/type_builder.rb, line 16
def self.[](db_type)
  registry[db_type]
end
register(db_type, builder) click to toggle source
# File lib/rom/sql/schema/type_builder.rb, line 12
def self.register(db_type, builder)
  registry[db_type] = builder
end

Public Instance Methods

call(primary_key:, db_type:, type:, allow_null:, **rest) click to toggle source
# 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) click to toggle source

@api private

# 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, **) click to toggle source

@api private

# 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) click to toggle source

@api private

# 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