Class: NF
- Inherits:
-
Object
- Object
- NF
- Defined in:
- lib/nf.rb,
lib/nf/version.rb
Overview
Usage: NF.new.run(ARGV)
Defined Under Namespace
Classes: Template, TemplateDir
Constant Summary collapse
- VERSION =
"0.0.3"
Instance Method Summary collapse
-
#create_new_file(template_name, args) ⇒ Object
Arguments examples: nf word nf word /path/to/directory nf word /path/to/file.
-
#delete(args) ⇒ Object
–delete or -d or –remove template_name.
-
#initialize ⇒ NF
constructor
Nothing to initialize.
-
#list(args) ⇒ Object
–list or -l.
- #run(args) ⇒ Object
-
#store(args) ⇒ Object
–store or -s word /path/to/template.docx.
- #usage ⇒ Object
Constructor Details
Instance Method Details
#create_new_file(template_name, args) ⇒ Object
Arguments examples:
nf word
nf word /path/to/directory
nf word /path/to/file
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/nf.rb', line 71 def create_new_file(template_name, args) file_or_dir = args.shift abort "Too many arguments provided; see `nf --help`" unless args.empty? abort "Invalid template name" unless template_name =~ /^\w+$/ f = file_or_dir target_directory = if f.nil? then Dir.pwd elsif File.file?(f) then File.dirname(f) elsif File.directory?(f) then f else Dir.pwd # raise exception? end template = TemplateDir.resolve(template_name) if template.nil? abort "Cannot locate template named #{template_name}" end target_path = File.join(target_directory, "newfile#{template.extension}") FileUtils.cp(template.path, target_path) end |
#delete(args) ⇒ Object
–delete or -d or –remove template_name
60 61 62 63 64 65 |
# File 'lib/nf.rb', line 60 def delete(args) abort "--store takes one argument: template_name" unless args.size == 1 name = args.shift abort "Invalid name" unless name =~ /^\w+$/ TemplateDir.local.delete(name) end |
#list(args) ⇒ Object
–list or -l
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/nf.rb', line 28 def list(args) unless args.empty? puts "(Ignoring arguments)" end puts puts "Global templates:" TemplateDir.global.templates.each do |t| puts " " + t.name.ljust(15) + "(#{t.extension})" end puts puts "Local templates:" TemplateDir.local.templates.each do |t| puts " " + t.name.ljust(15) + " (#{t.extension})" end end |
#run(args) ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/nf.rb', line 11 def run(args) case word = args.shift when nil, "-h", "--help" then usage when "--list", "-l" then list(args) when "--store", "-s" then store(args) when "--delete", "-d" then delete(args) when "--remove" then delete(args) else create_new_file(word, args) end end |
#store(args) ⇒ Object
–store or -s word /path/to/template.docx
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/nf.rb', line 45 def store(args) # Need two arguments: template name, template path abort "--store takes two arguments: name, path" unless args.size == 2 name, source_path = args.shift(2) abort "Invalid name" unless name =~ /^\w+$/ abort "Can't read file: #{source_path}" unless File.readable?(source_path) extension = File.extname(source_path) abort "Path #{source_path} doesn't have an extension" if extension.empty? TemplateDir.local.store(name, source_path) # The above line is not bad, but it has a lot of error-checking and # concomitant message-printing that ill befits the TemplateDir class. # That stuff should be handled by this class. Consider relocating it. end |