Class: EacTemplates::Variables::Content

Inherits:
Object
  • Object
show all
Defined in:
lib/eac_templates/variables/content.rb

Constant Summary collapse

VARIABLE_DELIMITER =
::Regexp.quote('%%')
VARIABLE_PATTERN =
/#{VARIABLE_DELIMITER}([a-z0-9\._]*)#{VARIABLE_DELIMITER}/i.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_file(path) ⇒ EacTemplates::Variables::Content

Parameters:

  • path (Pathname)

Returns:



15
16
17
# File 'lib/eac_templates/variables/content.rb', line 15

def from_file(path)
  new(path.to_pathname.read)
end

Instance Method Details

#apply(variables_source) ⇒ Object

variables_provider A [Hash] or object which responds to read_entry(entry_name).



24
25
26
27
28
29
# File 'lib/eac_templates/variables/content.rb', line 24

def apply(variables_source)
  variables_provider = ::EacTemplates::Variables::Providers.build(variables_source)
  variables.inject(content) do |a, e|
    a.gsub(variable_pattern(e), variables_provider.variable_value(e).to_s)
  end
end

#apply_to_file(variables_source, output_file_path) ⇒ Object



31
32
33
# File 'lib/eac_templates/variables/content.rb', line 31

def apply_to_file(variables_source, output_file_path)
  output_file_path.to_pathname.write(apply(variables_source))
end