Class: Rubrowser::Parser::File
- Inherits:
-
Object
- Object
- Rubrowser::Parser::File
- 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
-
#definitions ⇒ Object
readonly
Returns the value of attribute definitions.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#relations ⇒ Object
readonly
Returns the value of attribute relations.
Instance Method Summary collapse
- #constants_from_file ⇒ Object
-
#initialize(file) ⇒ File
constructor
A new instance of File.
- #parse ⇒ Object
- #parser ⇒ Object
- #valid_file?(file) ⇒ Boolean
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
#definitions ⇒ Object (readonly)
Returns the value of attribute definitions.
12 13 14 |
# File 'lib/rubrowser/parser/file.rb', line 12 def definitions @definitions end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
12 13 14 |
# File 'lib/rubrowser/parser/file.rb', line 12 def file @file end |
#relations ⇒ Object (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_file ⇒ Object
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 |
#parse ⇒ Object
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 |
#parser ⇒ Object
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
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 |