Class: RText::DefaultLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/rtext/default_loader.rb

Overview

Loads RText files into a FragmentedModel.

A glob pattern or file provider specifies the files which should be loaded. The load method can be called to load and to reload the model. Only changed files will be reloaded.

Optionally, the loader can use a fragment cache to speed up loading.

Defined Under Namespace

Classes: ProgressMonitor

Instance Method Summary collapse

Constructor Details

#initialize(language, fragmented_model, options = {}) ⇒ DefaultLoader

Create a default loader for language, loading into fragmented_model. It will find files either by evaluating the glob pattern given with :pattern (see Dir.glob) or by means of a file_provider. Options:

:pattern
  a glob pattern or an array of glob patterns
  alternatively, a +:file_provider+ may be specified

:file_provider
  a proc which is called without any arguments and should return the files to load
  this is an alternative to providing +:pattern+

:cache
  a fragment cache to be used for loading

:dont_reload_with_errors
  if set to true, don't reload fragments which have parse errors 
  instead keep the existing fragment but attach the new problem list

:resolver
  a reference resolver responding to the methods provided by DefaultResolver
  default: DefaultResolver


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rtext/default_loader.rb', line 41

def initialize(language, fragmented_model, options={})
  @lang = language
  @model = fragmented_model
  @files_added = []
  @files_changed = []
  @files_removed = []
  @change_detector = RGen::Util::FileChangeDetector.new(
    :file_added => lambda {|f| @files_added << f }, 
    :file_removed => lambda {|f| @files_removed << f},
    :file_changed => lambda {|f| @files_changed << f})
  @cache = options[:cache]
  @fragment_by_file = {}
  pattern = options[:pattern]
  @file_provider = options[:file_provider] || proc { Dir.glob(pattern) }
  @dont_reload_with_errors = options[:dont_reload_with_errors]
  @resolver = options[:resolver] || DefaultResolver.new(language)
end

Instance Method Details

#load(options = {}) ⇒ Object

Loads or reloads model fragments from files using the file patterns or file provider specified in the constructor. Options:

:before_load
  a proc which is called before a file is actually loaded, receives the fragment to load
  into and a symbol indicating the kind of loading: :load, :load_cached, :load_update_cache
  optionally, the proc may take third argument which is the overall number of files
  default: no before load proc

:after_load
  a proc which is called after a file has been loaded, receives the fragment loaded
  optionally, the proc may take second argument which is the overall number of files
  default: no after load proc

:on_progress
  a proc which is called when some progress is made 
  receives the current fragment being loaded, the actual work done as an integer and
  the overall work to be done as an integer
  default: no on progress proc


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rtext/default_loader.rb', line 79

def load(options={})
  @before_load_proc = options[:before_load]
  @after_load_proc = options[:after_load]
  files = @file_provider.call 
  @num_files = files.size
  @files_added = []
  @files_changed = []
  @files_removed = []
  @change_detector.check_files(files)
  @progress_monitor = ProgressMonitor.new(options[:on_progress], @files_added + @files_changed)
  @files_added.each {|f| file_added(f)}
  @files_changed.each {|f| file_changed(f)}
  @files_removed.each {|f| file_removed(f)}
  @resolver.resolve_model(@model)
end