class Crudboy::Template

Attributes

base_path[RW]
context[RW]
path[RW]
type[RW]

Public Class Methods

new(path, base_path, context) click to toggle source
# File lib/crudboy/template.rb, line 7
def initialize(path, base_path, context)
  @path = path
  @base_path = base_path.blank? ? '.' : base_path
  @type = if File.directory?(@path)
            :directory
          elsif @path.end_with?('.erb')
            :erb
          else
            :plain
          end
  @context = context
end

Public Instance Methods

directory?() click to toggle source
# File lib/crudboy/template.rb, line 64
def directory?
  @type == :directory
end
erb?() click to toggle source
# File lib/crudboy/template.rb, line 68
def erb?
  @type == :erb
end
make_base_directory!(destination) click to toggle source
# File lib/crudboy/template.rb, line 29
def make_base_directory!(destination)
  @context.eval(@base_path).tap do |path|
    File.dirname(path).tap do |base_dir|
      File.join(destination, base_dir).tap do |full_path|
        FileUtils.mkdir_p(full_path)
        puts "mkdir -p: #{full_path}"
      end
    end
  end
end
make_directory!(destination) click to toggle source
# File lib/crudboy/template.rb, line 20
def make_directory!(destination)
  @context.eval(@base_path).tap do |path|
    File.join(destination, path).tap do |full_path|
      FileUtils.mkdir_p(full_path)
      puts "mkdir -p: #{full_path}"
    end
  end
end
plain?() click to toggle source
# File lib/crudboy/template.rb, line 72
def plain?
  @type == :plain
end
render!(destination) click to toggle source
# File lib/crudboy/template.rb, line 50
def render!(destination)
  if directory?
    make_directory!(destination)
  else
    make_base_directory!(destination)
    render_file.tap do |file_content|
      File.join(destination, @context.eval(@base_path.delete_suffix('.erb'))).tap do |path|
        IO.write(path, file_content)
        puts "Write file: #{path}"
      end
    end
  end
end
render_file() click to toggle source
# File lib/crudboy/template.rb, line 40
def render_file
  if erb?
    erb = ERB.new(IO.read(path))
    erb.filename = path
    erb.result(@context.binding)
  else
    IO.read(path)
  end
end