module Mgen::Utils
Public Class Methods
source_root()
click to toggle source
# File lib/mgen/utils.rb, line 5 def self.source_root File.dirname(__FILE__) end
Public Instance Methods
base_location()
click to toggle source
# File lib/mgen/utils.rb, line 63 def base_location @location ||= Pathname.new(Dir.pwd) end
Also aliased as: location
create_directories(*dirs)
click to toggle source
# File lib/mgen/utils.rb, line 31 def create_directories(*dirs) dirs.each do |dir| log "Creating the #{dir} directory." FileUtils.mkdir_p(location.join(dir)) end end
create_new_file(name, file=nil)
click to toggle source
# File lib/mgen/utils.rb, line 9 def create_new_file(name, file=nil) log "Creating #{name}" contents = file.nil? ? '' : File.read(file) unless File.file?(location.join(name)) File.open(location.join(name), 'w') { |f| f.write(contents) } end end
create_with_template(name, template_location, contents={})
click to toggle source
# File lib/mgen/utils.rb, line 45 def create_with_template(name, template_location, contents={}) template = templates("#{template_location}.erb") eruby = Erubis::Eruby.new(File.read(template)) File.open(location.join(name.gsub(/^\//, '')), 'w') { |f| f.write(eruby.result(contents))} end
error(msg)
click to toggle source
# File lib/mgen/utils.rb, line 59 def error(msg) say msg, :red end
log(msg)
click to toggle source
# File lib/mgen/utils.rb, line 55 def log(msg) say msg, :green end
remove_directories(*names)
click to toggle source
# File lib/mgen/utils.rb, line 38 def remove_directories(*names) names.each do |name| log "Removing #{name} directory." FileUtils.rm_rf(location.join(name)) end end
remove_files(*files)
click to toggle source
# File lib/mgen/utils.rb, line 17 def remove_files(*files) files.each do |file| log "Removing #{file} file." FileUtils.rm(location.join(file)) end end
templates(path)
click to toggle source
# File lib/mgen/utils.rb, line 51 def templates(path) ::Mgen.root.join('mgen/templates').join(path) end
touch(*filenames)
click to toggle source
# File lib/mgen/utils.rb, line 24 def touch(*filenames) filenames.each do |filename| log "Creating #{filename} file." FileUtils.touch(location.join(filename)) end end
underscore(string)
click to toggle source
# File lib/mgen/utils.rb, line 69 def underscore(string) string.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end