class Begin::Repository

Provides centralised access to the local repository of templates on the machine

Public Class Methods

new(name = '.begin', parent_dir = '~') click to toggle source
# File lib/begin/repository.rb, line 11
def initialize(name = '.begin', parent_dir = '~')
  @parent_dir = Path.new(parent_dir, '.', 'Repository Parent')
  @parent_dir.ensure_dir_exists
  @repo_dir = Path.new(name, @parent_dir, 'Repository directory')
  @template_dir = Path.new('templates', @repo_dir, 'Templates directory')
end

Public Instance Methods

each() { |template_name x| ... } click to toggle source
# File lib/begin/repository.rb, line 39
def each
  templates = @template_dir.dir_contents
  templates.each { |x| yield template_name x }
end
install(source_uri, name) click to toggle source
# File lib/begin/repository.rb, line 18
def install(source_uri, name)
  path = install_prerequisites(name)
  begin
    return GitTemplate.install source_uri, path
  rescue
    unless source_uri.include? '://'
      return SymlinkTemplate.install source_uri, path
    end
    raise
  end
end
install_prerequisites(name) click to toggle source
# File lib/begin/repository.rb, line 30
def install_prerequisites(name)
  @repo_dir.make_dir
  @template_dir.make_dir
  path = template_path name
  raise "A template is already installed at: #{path}" if path.exists?
  Output.info "Installing to '#{path}'"
  path
end
template(name) click to toggle source
# File lib/begin/repository.rb, line 44
def template(name)
  path = template_path name
  template_from_path path
end
template_from_path(path) click to toggle source
# File lib/begin/repository.rb, line 63
def template_from_path(path)
  return SymlinkTemplate.new(path) if File.symlink? path
  GitTemplate.new path
end
template_name(uri) click to toggle source
# File lib/begin/repository.rb, line 49
def template_name(uri)
  uri = URI(uri)
  path_bits = uri.path.split '/'
  name = path_bits.last
  name.slice! 'begin-'
  name.slice! 'begin_'
  name.chomp! '.git'
  name
end
template_path(template_name) click to toggle source
# File lib/begin/repository.rb, line 59
def template_path(template_name)
  Path.new template_name, @template_dir, 'Template directory'
end