Class: ERB::Processor::ForSingleFile

Inherits:
Object
  • Object
show all
Defined in:
lib/erb/processor/for_single_file.rb

Overview

Processes a single file template

Constant Summary collapse

ERB_TEMPLATE_REGEX =
/.erb$/i
FOR_WRITING =
"w+"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_path) ⇒ ForSingleFile

Returns a new instance of ForSingleFile.



19
20
21
22
# File 'lib/erb/processor/for_single_file.rb', line 19

def initialize(template_path)
  @logger = Logging.logger[self]
  @template_path = template_path
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



17
18
19
# File 'lib/erb/processor/for_single_file.rb', line 17

def logger
  @logger
end

#template_pathObject (readonly)

Returns the value of attribute template_path.



17
18
19
# File 'lib/erb/processor/for_single_file.rb', line 17

def template_path
  @template_path
end

Class Method Details

.template_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/erb/processor/for_single_file.rb', line 13

def self.template_file?(path)
  ERB_TEMPLATE_REGEX.match?(path)
end

Instance Method Details

#commented_processed_headerObject



45
46
47
# File 'lib/erb/processor/for_single_file.rb', line 45

def commented_processed_header
  LanguageCommenter.new(processed_path, processed_header).commented_text
end

#processed_contentObject



37
38
39
40
41
42
43
# File 'lib/erb/processor/for_single_file.rb', line 37

def processed_content
  erb_processor = self

  ERB.new(template_content, trim_mode: "<>-").result binding
rescue StandardError => e
  raise e.class, "\n#{template_path}:0: #{e}", e.backtrace
end

#processed_headerObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/erb/processor/for_single_file.rb', line 49

def processed_header
  "\n    WARNING: DO NOT EDIT directly\n\n    HOWTO Modify this file\n    1. Edit the file \#{template_path}\n    2. $ erb-processor .\n\n  PROCESSED_HEADER\nend\n"

#processed_pathObject



33
34
35
# File 'lib/erb/processor/for_single_file.rb', line 33

def processed_path
  template_path.sub(ERB_TEMPLATE_REGEX, "")
end

#runObject



25
26
27
28
29
30
31
# File 'lib/erb/processor/for_single_file.rb', line 25

def run
  logger.info { "Processing #{template_path}..." }
  File.open(processed_path, FOR_WRITING) do |output|
    output.print processed_content
  end
  logger.info { "Wrote #{processed_path}" }
end

#template_contentObject



61
62
63
# File 'lib/erb/processor/for_single_file.rb', line 61

def template_content
  File.read(template_path)
end