Class: ROM::SQL::Schema::Inferrer

Inherits:
Object
  • Object
show all
Extended by:
ClassMacros
Defined in:
lib/rom/sql/schema/inferrer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dsl) ⇒ Inferrer

Returns a new instance of Inferrer.



23
24
25
# File 'lib/rom/sql/schema/inferrer.rb', line 23

def initialize(dsl)
  @dsl = dsl
end

Instance Attribute Details

#dslObject (readonly)

Returns the value of attribute dsl.



21
22
23
# File 'lib/rom/sql/schema/inferrer.rb', line 21

def dsl
  @dsl
end

Instance Method Details

#call(dataset, gateway) ⇒ 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.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rom/sql/schema/inferrer.rb', line 28

def call(dataset, gateway)
  columns = gateway.connection.schema(dataset)
  fks = fks_for(gateway, dataset)

  columns.each do |(name, definition)|
    dsl.attribute name, build_type(definition.merge(foreign_key: fks[name]))
  end

  pks = columns
    .map { |(name, definition)| name if definition.fetch(:primary_key) }
    .compact

  dsl.primary_key(*pks) if pks.any?

  dsl.attributes
end