lib/templater/actions/file.rb in templater-0.1.6 vs lib/templater/actions/file.rb in templater-0.2
- old
+ new
@@ -1,31 +1,24 @@
module Templater
module Actions
- class File
+ class File < Action
- attr_accessor :name, :source, :destination
-
- # Builds a new file, given the name of the file and its source and destination.
+ # Builds a new file.
#
# === Parameters
- # name<Symbol>:: The name of this template
- # source<String>:: Full path to the source of this template
- # destination<String>:: Full path to the destination of this template
- def initialize(name, source, destination)
- @name = name
- @source = source
- @destination = destination
+ # generator<Object>:: The generator
+ # name<Symbol>:: The name of this file
+ # source<String>:: Full path to the source of this file
+ # destination<String>:: Full path to the destination of this file
+ # options<Hash{Symbol=>Symbol}:: Options, including callbacks.
+ def initialize(generator, name, source, destination, options={})
+ self.generator = generator
+ self.name = name
+ self.source = source
+ self.destination = destination
+ self.options = options
end
-
- # Returns the destination path relative to Dir.pwd. This is useful for prettier output in interfaces
- # where the destination root is Dir.pwd.
- #
- # === Returns
- # String:: The destination relative to Dir.pwd
- def relative_destination
- @destination.sub(::Dir.pwd + ::File::SEPARATOR, '')
- end
# Returns the contents of the source file as a String
#
# === Returns
# String:: The source file.
@@ -49,17 +42,19 @@
exists? && ::FileUtils.identical?(source, destination)
end
# Renders the template and copies it to the destination.
def invoke!
+ callback(:before)
::FileUtils.mkdir_p(::File.dirname(destination))
::FileUtils.copy_file(source, destination)
+ callback(:after)
end
# removes the destination file
def revoke!
::FileUtils.rm(destination, :force => true)
end
-
+
end
end
end