Class: FileTemplater::FileActions
- Inherits:
-
Object
- Object
- FileTemplater::FileActions
- Defined in:
- lib/file_templater/file_actions.rb
Class Method Summary collapse
- .add(path) ⇒ Object
- .combined_list ⇒ Object
- .copy(path) ⇒ Object
- .create_hub_if_necessary ⇒ Object
-
.get_class_name(file) ⇒ Object
Returns the first class name in file.
- .list ⇒ Object
- .list_bindings ⇒ Object
- .list_templates ⇒ Object
- .remove(path) ⇒ Object
- .require_binding(bind) ⇒ Object
- .unique_directory_list(path) ⇒ Object
Class Method Details
.add(path) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/file_templater/file_actions.rb', line 10 def add(path) = File.(path) # Three cases of what we are adding can arise: # file of the template being added # directory of the template being added # binding of the template being added type = File.directory?() ? :directory : (.end_with?(".rb") ? :binding : :file) hub = (type == :binding ? :binding : :template) if type == :file # If the file we are adding is a single file, # make a directory and put the file in it. = File.join(HUBS[hub], File.basename(, ".*")) FileUtils.mkdir() FileUtils.copy_entry(, File.join(, File.basename())) elsif type == :binding # If we are adding a binding, # we need to modify the code to work with our system. output_file = File.open(File.join(HUBS[hub], File.basename()), "w") output_file.print "module Bindings\n" File.open(, "r").each do |line| if line.lstrip.start_with?("class ") output_file.print(line + "def get_binding\nbinding\nend\n") else output_file.print line end end output_file.print "end\n" output_file.close # We will save the original file just in case. FileUtils.copy_entry(, File.join(HUBS[:original], File.basename())) else FileUtils.copy_entry(, File.join(HUBS[hub], File.basename())) end end |
.combined_list ⇒ Object
106 107 108 |
# File 'lib/file_templater/file_actions.rb', line 106 def combined_list list_templates + list_bindings end |
.copy(path) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/file_templater/file_actions.rb', line 94 def copy(path) if File.exist?(path) puts "A file of this name already exists.\nPlease remove it, or else this command will not work." return end # This code is very similar to that of remove. = !(path.end_with?(".rb") && File.exist?(File.join(HUBS[:binding], path))) FileUtils.copy_entry(File.join(HUBS[ ? :template : :original], path), path) end |
.create_hub_if_necessary ⇒ Object
4 5 6 7 8 |
# File 'lib/file_templater/file_actions.rb', line 4 def create_hub_if_necessary HUBS.each do |k, v| Dir.mkdir(v) unless Dir.exists?(v) end end |
.get_class_name(file) ⇒ Object
Returns the first class name in file.
123 124 125 126 127 128 129 130 |
# File 'lib/file_templater/file_actions.rb', line 123 def get_class_name(file) File.open(file, "r").each do |line| # The name will be the second word of the class statement. if line.lstrip.start_with?("class ") return line.split(" ")[1] end end end |
.list ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/file_templater/file_actions.rb', line 67 def list # In our list, we want to indicate if a template does not have a corresponding binding. # As a result, we should list like so: # Template Binding # example example.rb Terminal::Table.new do |t| templates = list_templates.sort bindings = list_bindings.sort # table header t.add_row ["Template", "Binding"] t.add_separator templates.each do |tm| bind = tm + ".rb" bind = nil unless bindings.include?(bind) bindings.delete(bind) if bind t.add_row [tm, bind] end bindings.each do |b| t.add_row [nil, b] end end end |
.list_bindings ⇒ Object
114 115 116 |
# File 'lib/file_templater/file_actions.rb', line 114 def list_bindings unique_directory_list(HUBS[:binding]) end |
.list_templates ⇒ Object
110 111 112 |
# File 'lib/file_templater/file_actions.rb', line 110 def list_templates unique_directory_list(HUBS[:template]) end |
.remove(path) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/file_templater/file_actions.rb', line 49 def remove(path) removing_template = !path.end_with?(".rb") unless removing_template # Remove the associated binding. begin FileUtils.remove_file(File.join(HUBS[:binding], path)) FileUtils.remove_file(File.join(HUBS[:original], path)) rescue StandardError # If we failed to remove one of these files, try to remove a template by the same name. removing_template = true end end # Remove the associated template. FileUtils.remove_dir(File.join(HUBS[:template], path), true) if removing_template end |
.require_binding(bind) ⇒ Object
118 119 120 |
# File 'lib/file_templater/file_actions.rb', line 118 def require_binding(bind) require File.join(HUBS[:binding], bind) end |
.unique_directory_list(path) ⇒ Object
132 133 134 |
# File 'lib/file_templater/file_actions.rb', line 132 def unique_directory_list(path) Dir.entries(path).reject { |d| d == "." || d == ".." } end |