Class: Rubrowser::Parser::File

Inherits:
Object
  • Object
show all
Defined in:
lib/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.



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

def initialize(file)
  @file = file
  @definitions = Set.new
  @occurences = []
end

Instance Attribute Details

#definitionsObject

Returns the value of attribute definitions.



8
9
10
# File 'lib/parser/file.rb', line 8

def definitions
  @definitions
end

#fileObject (readonly)

Returns the value of attribute file.



8
9
10
# File 'lib/parser/file.rb', line 8

def file
  @file
end

#occurencesObject

Returns the value of attribute occurences.



8
9
10
# File 'lib/parser/file.rb', line 8

def occurences
  @occurences
end

Instance Method Details

#countObject



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

def count
  1
end

#file_valid?(file) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/parser/file.rb', line 29

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

#parseObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/parser/file.rb', line 16

def parse
  if file_valid?(file)
    ::File.open(file) do |f|
      code = f.read
      ast = ::Parser::CurrentRuby.parse(code)
      constants = parse_block(ast)
      self.definitions = constants[:definitions].uniq
      self.occurences = constants[:occurences].uniq
    end
  end
  self
end