Class: Expansions::TemplateVisitor

Inherits:
Object
  • Object
show all
Defined in:
lib/expansions/template_visitor.rb

Constant Summary collapse

DOT_FILE_PATTERN =
/\.dotfile/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ TemplateVisitor

Returns a new instance of TemplateVisitor.



7
8
9
10
# File 'lib/expansions/template_visitor.rb', line 7

def initialize(args = {})
  @processors = args.fetch(:processor_registry, TemplateProcessors.instance)
  @file = args.fetch(:file, File)
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



4
5
6
# File 'lib/expansions/template_visitor.rb', line 4

def file
  @file
end

#processorsObject (readonly)

Returns the value of attribute processors.



3
4
5
# File 'lib/expansions/template_visitor.rb', line 3

def processors
  @processors
end

Instance Method Details

#get_file_names(file_name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/expansions/template_visitor.rb', line 26

def get_file_names(file_name)
    base_name = File.basename(file_name, File.extname(file_name))
    dir_name = File.dirname(file_name)
    generated_name = base_name.gsub(DOT_FILE_PATTERN, "") 
    generated_name = ".#{generated_name}" if (DOT_FILE_PATTERN =~ file_name) 
    output_file_name = File.join(dir_name, generated_name)
    settings_file = File.join(dir_name, "#{base_name}.settings")

    {
      settings_file: settings_file,
      output_file_name: output_file_name
    }
end

#load_settings_file(settings_file) ⇒ Object



40
41
42
# File 'lib/expansions/template_visitor.rb', line 40

def load_settings_file(settings_file)
  load settings_file if File.exist?(settings_file)
end

#run_using(file_name) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/expansions/template_visitor.rb', line 12

def run_using(file_name)
  processor = processors.get_processor_for(file_name)
  names = get_file_names(file_name)
  load_settings_file(names[:settings_file])
  output = names[:output_file_name]
  file.delete(output) if file.exist?(output)

  begin
    processor.process(:input => file_name,:output => output)
  rescue Exception => e
    raise "Error processing template file: #{file_name}"
  end
end