Class: EacRubyUtils::Templates::File

Inherits:
Object
  • Object
show all
Includes:
SimpleCache
Defined in:
lib/eac_ruby_utils/templates/file.rb

Defined Under Namespace

Classes: BaseVariablesProvider, EntriesReaderVariablesProvider, HashVariablesProvider, VariableNotFoundError

Constant Summary collapse

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

Constants included from SimpleCache

SimpleCache::UNCACHED_METHOD_PATTERN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SimpleCache

#method_missing, #reset_cache, #respond_to_missing?

Constructor Details

#initialize(path) ⇒ File

Returns a new instance of File.



16
17
18
# File 'lib/eac_ruby_utils/templates/file.rb', line 16

def initialize(path)
  @path = path
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class EacRubyUtils::SimpleCache

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



14
15
16
# File 'lib/eac_ruby_utils/templates/file.rb', line 14

def path
  @path
end

Instance Method Details

#apply(variables_source) ⇒ Object

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



21
22
23
24
25
26
# File 'lib/eac_ruby_utils/templates/file.rb', line 21

def apply(variables_source)
  variables_provider = build_variables_provider(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



28
29
30
# File 'lib/eac_ruby_utils/templates/file.rb', line 28

def apply_to_file(variables_source, output_file_path)
  ::File.write(output_file_path, apply(variables_source))
end