Class: Rubrowser::Parser::File

Inherits:
Object
  • Object
show all
Defined in:
lib/rubrowser/parser/file.rb,
lib/rubrowser/parser/file/builder.rb

Defined Under Namespace

Classes: Builder

Constant Summary collapse

FILE_SIZE_LIMIT =
2 * 1024 * 1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ File

Returns a new instance of File.



14
15
16
17
18
# File 'lib/rubrowser/parser/file.rb', line 14

def initialize(file)
  @file = ::File.absolute_path(file)
  @definitions = []
  @relations = []
end

Instance Attribute Details

#definitionsObject (readonly)

Returns the value of attribute definitions.



12
13
14
# File 'lib/rubrowser/parser/file.rb', line 12

def definitions
  @definitions
end

#fileObject (readonly)

Returns the value of attribute file.



12
13
14
# File 'lib/rubrowser/parser/file.rb', line 12

def file
  @file
end

#relationsObject (readonly)

Returns the value of attribute relations.



12
13
14
# File 'lib/rubrowser/parser/file.rb', line 12

def relations
  @relations
end

Instance Method Details

#constants_from_fileObject



31
32
33
34
35
36
37
38
39
# File 'lib/rubrowser/parser/file.rb', line 31

def constants_from_file
  contents = ::File.read(file)

  buffer = ::Parser::Source::Buffer.new(file, 1)
  buffer.source = contents.force_encoding(Encoding::UTF_8)

  ast = parser.parse(buffer)
  parse_block(ast)
end

#parseObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/rubrowser/parser/file.rb', line 20

def parse
  return unless valid_file?(file)

  constants = constants_from_file

  @definitions = constants[:definitions]
  @relations = constants[:relations]
rescue ::Parser::SyntaxError
  warn "SyntaxError in #{file}"
end

#parserObject



41
42
43
44
45
46
# File 'lib/rubrowser/parser/file.rb', line 41

def parser
  parser = ::Parser::CurrentRuby.new(Builder.new)
  parser.diagnostics.ignore_warnings = true
  parser.diagnostics.all_errors_are_fatal = false
  parser
end

#valid_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
# File 'lib/rubrowser/parser/file.rb', line 48

def valid_file?(file)
  !::File.symlink?(file) &&
    ::File.file?(file) &&
    ::File.size(file) <= FILE_SIZE_LIMIT
end