Class: Solargraph::Arc::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/arc/schema.rb

Defined Under Namespace

Classes: ColumnData

Constant Summary collapse

RUBY_TYPES =
{
  decimal: 'BigDecimal',
  float: 'BigDecimal',
  integer: 'Integer',
  date: 'Date',
  datetime: 'ActiveSupport::TimeWithZone',
  string: 'String',
  boolean: 'Boolean',
  text: 'String',
  jsonb: 'Hash',
  bigint: 'Integer',
  inet: 'IPAddr'
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchema

Returns a new instance of Schema.



24
25
26
# File 'lib/solargraph/arc/schema.rb', line 24

def initialize
  @schema_present = File.exist?("db/schema.rb")
end

Class Method Details

.instanceObject



20
21
22
# File 'lib/solargraph/arc/schema.rb', line 20

def self.instance
  @instance ||= self.new
end

Instance Method Details

#process(source_map, ns) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/solargraph/arc/schema.rb', line 28

def process(source_map, ns)
  return [] unless @schema_present
  return [] unless source_map.filename.include?("app/models")

  table_name = infer_table_name(ns)
  table      = schema[table_name]

  return [] unless table

  pins = table.map do |column, data|
    Util.build_public_method(
      ns,
      column,
      types: [RUBY_TYPES.fetch(data.type.to_sym)],
      location: Util.build_location(data.ast, "db/schema.rb")
    )
  end

  Solargraph.logger.debug("[ARC][Schema] added #{pins.map(&:name)} to #{ns.path}") if pins.any?
  pins
end