Class: Slickr::Generators::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/slickr/generators/base.rb

Direct Known Subclasses

Behavior, Entity, Project, Reactor, Renderer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Base

Returns a new instance of Base.



7
8
9
10
# File 'lib/slickr/generators/base.rb', line 7

def initialize(name)
  @name = name
  @destination = Pathname.new(Dir.pwd)
end

Instance Attribute Details

#destinationObject

Returns the value of attribute destination.



5
6
7
# File 'lib/slickr/generators/base.rb', line 5

def destination
  @destination
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/slickr/generators/base.rb', line 4

def name
  @name
end

Instance Method Details

#copy_file(filename, path = "") ⇒ Object

Copy a file from ROOT/files to somewhere in the project directory.

This does not render anything, it’s just a simple cp.

Examples:

Place file in the root of the project

copy_file "Rakefile"

Place file somewhere nested in the project

copy_file "jinput.jar", "java"
# => Creates PROJECT/java/jinput.jar

Parameters:

  • filename (String)

    Name of the source file located in ROOT/files

  • path (String) (defaults to: "")

    Path to place the file in the project



38
39
40
41
42
# File 'lib/slickr/generators/base.rb', line 38

def copy_file(filename, path="")
  source = root.join("files", filename)
  dest   = destination.join(path, filename)
  FileUtils.cp(source, dest)
end

#empty_directory(path) ⇒ Object

Create an empty directory at path inside the project.

Examples:

empty_directory "lib"

Parameters:

  • path (String)

    Path in the project to create the directory.



19
20
21
# File 'lib/slickr/generators/base.rb', line 19

def empty_directory(path)
  destination.join(with_correct_path_seperator(path)).mkpath
end

#template(filename, path) ⇒ Object

Render an erb template at a specific path within the project.

Renders ERB in the context of the class calling this method. Meaning you’ll need to make whatever variables you want in your template available within the class. See Actions::Create, name is an accessor and used in the template.

Examples:

template "application.erb", "app/application.rb"

Parameters:

  • filename (String)

    Name of the erb template living in ROOT/templates

  • path (String)

    Path in the project to write the resulting file



58
59
60
61
62
# File 'lib/slickr/generators/base.rb', line 58

def template(filename, path)
  file = lib_file(with_correct_path_seperator(path))
  file.dirname.mkpath
  file.open("w+") { |f| f << render(template_root(filename)) }
end