Class: MODL::Parser::FileImporter

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/modl/parser/file_importer.rb

Overview

This class handled file loading from local or remote file systems.

Constant Summary collapse

CACHE_DISABLED =
true

Instance Method Summary collapse

Constructor Details

#initializeFileImporter

Returns a new instance of FileImporter.



14
15
16
# File 'lib/modl/parser/file_importer.rb', line 14

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.



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
62
63
64
65
# File 'lib/modl/parser/file_importer.rb', line 19

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?('!')
    file_name = Sutil.head(file_name) if force
    file_name << '.modl' unless file_name.end_with?('.txt', '.modl')
    file_name, new_val = RefProcessor.deref file_name, global if file_name.include?('%')
    if force
      # Don't use the cache if we're forcing a reload.
      @cache.evict(file_name)
      parsed = nil
    else
      # Do we have a cached version?
      parsed = @cache.get(file_name)
    end

    # Did we hit the cache?
    if (parsed.nil? && CACHE_DISABLED) || (CACHE_DISABLED)
      # No.

      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) unless CACHE_DISABLED
    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