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



67
68
69
70
# File 'lib/rom/sql/extensions/postgres/type_builder.rb', line 67

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, **options) ⇒ Object



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

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

  type.meta(primary_key: true)
end

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



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rom/sql/extensions/postgres/type_builder.rb', line 54

def map_type(ruby_type, db_type, enum_values: nil, **_)
  if db_type.end_with?(self.class.db_array_type_matcher)
    member_name = db_type[0...-2]
    member_type = self.class.db_type_mapping[member_name]

    Types::Array(member_name, member_type)
  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)


72
73
74
# File 'lib/rom/sql/extensions/postgres/type_builder.rb', line 72

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