Class: Solargraph::Arc::RailsApi

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



4
5
6
# File 'lib/solargraph/arc/rails_api.rb', line 4

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

Instance Method Details

#global(yard_map) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/solargraph/arc/rails_api.rb', line 8

def global yard_map
  return [] if yard_map.required.empty?

  ann    = File.read(File.dirname(__FILE__) + "/annotations.rb")
  source = Solargraph::Source.load_string(ann, "annotations.rb")
  map    = Solargraph::SourceMap.map(source)

  Solargraph.logger.debug("[Arc][Rails] found #{map.pins.size} pins in annotations")

  overrides = YAML.load_file(File.dirname(__FILE__) + "/types.yml").map do |meth, data|
    if data["return"]
      Util.method_return(meth, data["return"])
    elsif data["yieldself"]
      Solargraph::Pin::Reference::Override.from_comment(
        meth,
        "@yieldself [#{data['yieldself'].join(',')}]"
      )
    elsif data["yieldparam"]
      Solargraph::Pin::Reference::Override.from_comment(
        meth,
        "@yieldparam [#{data['yieldparam'].join(',')}]"
      )
    end
  end

  ns = Solargraph::Pin::Namespace.new(
    name:  "ActionController::Base",
    gates: ["ActionController::Base"]
  )

  definitions = [
    Util.build_public_method(
      ns,
      "response",
      types: ["ActionDispatch::Response"],
      location: Util.dummy_location("whatever.rb")
    ),
    Util.build_public_method(
      ns,
      "request",
      types: ["ActionDispatch::Request"],
      location: Util.dummy_location("whatever.rb")
    ),
    Util.build_public_method(
      ns,
      "session",
      types: ["ActionDispatch::Request::Session"],
      location: Util.dummy_location("whatever.rb")
    ),
    Util.build_public_method(
      ns,
      "flash",
      types: ["ActionDispatch::Flash::FlashHash"],
      location: Util.dummy_location("whatever.rb")
    )
  ]

  map.pins + definitions + overrides
end

#local(source_map, ns) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/solargraph/arc/rails_api.rb', line 68

def local(source_map, ns)
  return [] unless source_map.filename.include?("db/migrate")
  node = Walker.normalize_ast(source_map.source)

  pins = [
    Util.build_module_include(
      ns,
      "ActiveRecord::ConnectionAdapters::SchemaStatements",
      Util.build_location(node, ns.filename)
    ),
    Util.build_module_extend(
      ns,
      "ActiveRecord::ConnectionAdapters::SchemaStatements",
      Util.build_location(node, ns.filename)
    )
  ]

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