Class: Modl::Parser::FileImporter
- Inherits:
-
Object
- Object
- Modl::Parser::FileImporter
- Defined in:
- lib/modl/parser/file_importer.rb
Overview
This class handled file loading from local or remote file systems.
Instance Method Summary collapse
-
#import_files(files, global) ⇒ Object
Supply a single file name as a string or an array of file names.
-
#initialize ⇒ FileImporter
constructor
A new instance of FileImporter.
Constructor Details
#initialize ⇒ FileImporter
Returns a new instance of FileImporter.
10 11 12 |
# File 'lib/modl/parser/file_importer.rb', line 10 def initialize @cache = ObjectCache.new end |
Instance Method Details
#import_files(files, global) ⇒ Object
Supply a single file name as a string or an array of file names.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/modl/parser/file_importer.rb', line 15 def import_files(files, global) file_names = [] file_names += files if files.is_a? Array file_names << files if files.is_a? String file_names.each do |file_name| force = file_name.end_with?('!') if force # Don't use the cache if we're forcing a reload. @cache.evict(file_name) file_name = Sutil.head(file_name) parsed = nil else # Do we have a cached version? parsed = @cache.get(file_name) end # Did we hit the cache? unless parsed # No. file_name << '.modl' unless file_name.end_with?('.txt', '.modl') file_name, new_val = RefProcessor.deref file_name, global if file_name.include?('%') begin uri = URI(file_name) txt = Net::HTTP.get(uri) rescue begin txt = File.readlines(file_name).join rescue raise InterpreterError, 'File not found: ' + file_name end end global.loaded_file(file_name) # Parse the downloaded file ands extract the classes parsed = Modl::Parser::Parser.parse txt, global # Save it for next time @cache.put(file_name, parsed) end # Extract the JSON content and add the classes and pairs to the existing GlobalParseContext hashes. parsed.extract_hash global.merge_classes(parsed.global) global.merge_pairs(parsed.global) end end |