Class: ROM::SQL::Postgres::TypeBuilder

Inherits:
Schema::TypeBuilder show all
Defined in:
lib/rom/sql/extensions/postgres/type_builder.rb

Constant Summary

Constants inherited from Schema::TypeBuilder

Schema::TypeBuilder::DECIMAL_REGEX

Instance Method Summary collapse

Methods inherited from Schema::TypeBuilder

[], #call, #map_decimal_type, register

Instance Method Details

#map_db_type(db_type) ⇒ Object



55
56
57
58
# File 'lib/rom/sql/extensions/postgres/type_builder.rb', line 55

def map_db_type(db_type)
  self.class.db_type_mapping[db_type] ||
    (db_type.start_with?('timestamp') ? SQL::Types::Time : nil)
end

#map_pk_type(type, db_type) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/rom/sql/extensions/postgres/type_builder.rb', line 35

def map_pk_type(type, db_type)
  if numeric?(type, db_type)
    type = self.class.numeric_pk_type
  else
    type = map_type(type, db_type)
  end

  type.meta(primary_key: true)
end

#map_type(ruby_type, db_type, enum_values: nil, **_) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/rom/sql/extensions/postgres/type_builder.rb', line 45

def map_type(ruby_type, db_type, enum_values: nil, **_)
  if db_type.end_with?(self.class.db_array_type_matcher)
    Types::Array(db_type[0...-2])
  elsif enum_values
    SQL::Types::String.enum(*enum_values)
  else
    map_db_type(db_type) || super
  end
end

#numeric?(ruby_type, db_type) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/rom/sql/extensions/postgres/type_builder.rb', line 60

def numeric?(ruby_type, db_type)
  self.class.db_numeric_types.include?(db_type) || ruby_type == :integer
end