Class: Transpec::ProcessedSource

Inherits:
Object
  • Object
show all
Defined in:
lib/transpec/processed_source.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer, ast, path = nil, syntax_error = nil) ⇒ ProcessedSource

Returns a new instance of ProcessedSource.



37
38
39
40
41
42
# File 'lib/transpec/processed_source.rb', line 37

def initialize(buffer, ast, path = nil, syntax_error = nil)
  @buffer = buffer
  @ast = ast
  @path = path
  @syntax_error = syntax_error
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



15
16
17
# File 'lib/transpec/processed_source.rb', line 15

def ast
  @ast
end

#bufferObject (readonly)

Returns the value of attribute buffer.



15
16
17
# File 'lib/transpec/processed_source.rb', line 15

def buffer
  @buffer
end

#pathObject (readonly)

Returns the value of attribute path.



15
16
17
# File 'lib/transpec/processed_source.rb', line 15

def path
  @path
end

#syntax_errorObject (readonly)

Returns the value of attribute syntax_error.



15
16
17
# File 'lib/transpec/processed_source.rb', line 15

def syntax_error
  @syntax_error
end

Class Method Details

.parse(source, path = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/transpec/processed_source.rb', line 22

def self.parse(source, path = nil)
  buffer = Parser::Source::Buffer.new(path || '(string)')
  buffer.source = source

  builder = AST::Builder.new
  parser = Parser::CurrentRuby.new(builder)

  begin
    ast = parser.parse(buffer)
    new(buffer, ast, path)
  rescue Parser::SyntaxError => error
    new(buffer, nil, path, error)
  end
end

.parse_file(path) ⇒ Object



17
18
19
20
# File 'lib/transpec/processed_source.rb', line 17

def self.parse_file(path)
  source = File.read(path)
  parse(source, path)
end

Instance Method Details

#to_sObject



44
45
46
# File 'lib/transpec/processed_source.rb', line 44

def to_s
  buffer.source
end