class Biran::ERBConfig

Constants

DEFAULT_TEMPLATE_CONFIG_INDEX

Attributes

bindings[RW]
config[R]
extension[R]
name[R]
output_dir[R]
output_name[RW]
source_dir[R]
template_config_index[RW]
template_contents[R]

Public Class Methods

new(config, name, extension, source, output_dir, output_name) click to toggle source
# File lib/biran/erb_config.rb, line 8
def initialize(config, name, extension, source, output_dir, output_name)
  before_process_erb do
    @name       = name
    @extension  = extension
    @config     = config
    @source_dir = source
    @output_dir = output_dir
    @output_name = output_name
  end
end

Public Instance Methods

save!() click to toggle source
# File lib/biran/erb_config.rb, line 19
def save!
  File.open(File.join(output_dir, "#{output_name}#{extension}"), 'w') do |f|
    f.print template_contents.result(build_erb_env.call)
  end
end

Private Instance Methods

assign_instance_vars() click to toggle source
# File lib/biran/erb_config.rb, line 62
def assign_instance_vars
  lambda do |bindable|
    instance_variable_set(:"@#{bindable}", config[bindable.to_sym])
  end
end
before_process_erb() { || ... } click to toggle source
# File lib/biran/erb_config.rb, line 27
def before_process_erb
  yield
  raise ArgumentError unless process_erb_ready?
  @template_contents = process_erb
rescue ArgumentError
  e_message = "Settings required to determine template name for #{name} are not configured"
  raise Biran::ConfigSyntaxError, e_message
end
build_erb_env() click to toggle source
# File lib/biran/erb_config.rb, line 49
def build_erb_env
  proc do
    @environment = config[:env]
    @app_config  = config
    @config_index =  template_config_index || DEFAULT_TEMPLATE_CONFIG_INDEX

    @bindings.each(&assign_instance_vars) unless @bindings.nil?

    # This pulls these variables into a binding object which is returned
    binding
  end
end
process_erb() click to toggle source
# File lib/biran/erb_config.rb, line 42
def process_erb
  config_erb_file = File.join(source_dir, "_#{name}#{extension}.erb")
  ERB.new(File.read(config_erb_file), nil, '-')
rescue Errno::ENOENT
  raise Biran::ConfigSyntaxError, "Missing template file for #{name}: #{config_erb_file}"
end
process_erb_ready?() click to toggle source
# File lib/biran/erb_config.rb, line 36
def process_erb_ready?
  @name &&
  @extension &&
  @source_dir
end