class TreeRenderer

Render a tree of files using ERB

example:

TreeRenderer.new("/template/path", "/target/path", variables).save

Constants

VERSION

Version of the gem

Attributes

target_path[R]

@return [String] the target path where to render the template

template_path[R]

@return [String] the path to the template directory to be rendered

var_binding[R]

@return [Binding] binding for the variables to use for rendering

Public Class Methods

new(template_path, target_path, variables = {}) click to toggle source

Initialize the paths and variables for rendering

@param template_path [String] the path to the template directory to be rendered @param target_path [String] the target path where to render the template @param variables [Hash, Object, Binding] the variables to use for rendering

# File lib/tree_renderer.rb, line 31
def initialize(template_path, target_path, variables = {})
  @template_path = template_path
  @target_path   = target_path
  @var_binding   = calculate_binding(variables)
end

Public Instance Methods

save() click to toggle source

Run the rendering process

# File lib/tree_renderer.rb, line 38
def save
  all_files.each{ |file| parse_and_save(file) }
end

Private Instance Methods

all_files() click to toggle source

get list of files in the `template_path`

@return [Array<String>] list of files in the `template_path` dir

# File lib/tree_renderer.rb, line 65
def all_files
  Dir.glob("#{template_path}/**/*", File::FNM_DOTMATCH).reject{|path| File.directory?(path) }.sort
end
calculate_binding(variables) click to toggle source

Take the initialize variables thing and transforms it to Binding

@param variables [Hash, Object, Binding] the variables to use for rendering @return [Binding] binding for the input variables

# File lib/tree_renderer.rb, line 49
def calculate_binding(variables)
  case variables
  when Hash
    require "ostruct"
    OpenStruct.new(variables).instance_eval { binding }
  when Binding
    variables
  else
    variables.instance_eval { binding }
  end
end
parse_and_save(file) click to toggle source

read `file` from `template_path`, render it, amd save it in `target_path`

@param file [String] path to file to render

# File lib/tree_renderer.rb, line 73
def parse_and_save(file)
  save_file(
    transform_path(file),
    parse_template(file)
  )
end
parse_template(path) click to toggle source

read and render template from `path`

@param path [String] path to file to read @return [String] rendered content of the file

# File lib/tree_renderer.rb, line 107
def parse_template(path)
  render(File.read(path))
end
render(content) click to toggle source

render given content

@param content [String] the content to render @return [String] the rendered content

# File lib/tree_renderer.rb, line 116
def render(content)
  ERB.new(content).result(var_binding)
end
save_file(path, content) click to toggle source

write `content` to the given `path`, it will create required directories

@param path [String] the path of file to save to @param content [String] the content to save

# File lib/tree_renderer.rb, line 85
def save_file(path, content)
  FileUtils.mkdir_p(File.expand_path("..", path))
  File.open(path, "w") do |f|
    f.write(content)
  end
end
transform_path(file) click to toggle source

translates path to `file` from `template_path` to `target_path` it will also render it to replace file names if needed

@param file [String] the file name to transform from `template_path` @return [String] translated file in `target_path`

# File lib/tree_renderer.rb, line 98
def transform_path(file)
  render(file.sub(template_path, target_path))
end