Class: Rubrowser::Parser::File

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

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.



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

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

Instance Attribute Details

#definitionsObject (readonly)

Returns the value of attribute definitions.



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

def definitions
  @definitions
end

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

#relationsObject (readonly)

Returns the value of attribute relations.



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

def relations
  @relations
end

Instance Method Details

#parseObject



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

def parse
  if valid_file?(file)
    code = ::File.read(file)
    ast = ::Parser::CurrentRuby.parse(code)
    constants = parse_block(ast)

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

#valid_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


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

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