Class: Rucoa::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/rucoa/source.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content:, uri:) ⇒ Source

Returns a new instance of Source.

Parameters:

  • content (String)
  • uri (String)


15
16
17
18
19
20
21
# File 'lib/rucoa/source.rb', line 15

def initialize(
  content:,
  uri:
)
  @content = content
  @uri = uri
end

Instance Attribute Details

#contentString (readonly)

Returns:

  • (String)


8
9
10
# File 'lib/rucoa/source.rb', line 8

def content
  @content
end

#uriString (readonly)

Returns:

  • (String)


11
12
13
# File 'lib/rucoa/source.rb', line 11

def uri
  @uri
end

Instance Method Details

#definitionsArray<Rucoa::Definition::Base>

Examples:

returns definitions from given source

content = <<~RUBY
  class Foo
    def bar
    end
  end
RUBY
source = Rucoa::Source.new(
  content: content,
  uri: 'file:///path/to/foo.rb'
)
expect(source.definitions).to match(
  [
    a_kind_of(Rucoa::Definitions::ClassDefinition),
    a_kind_of(Rucoa::Definitions::MethodDefinition)
  ]
)

returns empty array when failed to parse

source = Rucoa::Source.new(
  content: 'class Foo',
  uri: 'file:///path/to/foo.rb'
)
expect(source.definitions).to eq([])

returns empty array when no node is found from content

source = Rucoa::Source.new(
  content: '',
  uri: 'file:///path/to/foo.rb'
)
expect(source.definitions).to eq([])

Returns:

  • (Array<Rucoa::Definition::Base>)


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rucoa/source.rb', line 53

def definitions
  @definitions ||=
    if parse_result.failed? || root_node.nil?
      []
    else
      Yard::DefinitionsLoader.call(
        associations: parse_result.associations,
        root_node: parse_result.root_node
      )
    end
end

#failed_to_parse?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/rucoa/source.rb', line 66

def failed_to_parse?
  parse_result.failed?
end

#nameString?

Examples:

returns path for file URI

source = Rucoa::Source.new(
  content: '',
  uri: 'file:///path/to/foo.rb'
)
expect(source.name).to eq('/path/to/foo.rb')

returns opaque for untitled URI

source = Rucoa::Source.new(
  content: '',
  uri: 'untitled:Untitled-1'
)
expect(source.name).to eq('Untitled-1')

Returns:

  • (String, nil)


83
84
85
86
87
88
89
# File 'lib/rucoa/source.rb', line 83

def name
  if untitled?
    uri_object.opaque
  else
    uri_object.path
  end
end

#node_at(position) ⇒ Rucoa::Nodes::Base?

Parameters:

Returns:



93
94
95
96
97
# File 'lib/rucoa/source.rb', line 93

def node_at(position)
  root_and_descendant_nodes.reverse.find do |node|
    node.include_position?(position)
  end
end

#root_nodeRucoa::Nodes::Base?

Returns:



100
101
102
# File 'lib/rucoa/source.rb', line 100

def root_node
  parse_result.root_node
end

#untitled?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/rucoa/source.rb', line 105

def untitled?
  uri_object.scheme == 'untitled'
end