Class: Aka::AliasList
- Inherits:
-
Object
- Object
- Aka::AliasList
- Includes:
- Methadone::CLILogging
- Defined in:
- lib/aka.rb
Instance Method Summary collapse
- #add(string, command) ⇒ Object
- #backup ⇒ Object
- #change_alias_file(new_file_name) ⇒ Object
- #change_defaults(name, value) ⇒ Object
- #empty ⇒ Object
- #get_defaults_from_file ⇒ Object
-
#initialize(incoming_alias_file = nil) ⇒ AliasList
constructor
A new instance of AliasList.
- #list ⇒ Object
- #remove(string) ⇒ Object
- #show(string) ⇒ Object
- #showAll ⇒ Object
- #writeOut ⇒ Object
Constructor Details
#initialize(incoming_alias_file = nil) ⇒ AliasList
Returns a new instance of AliasList.
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 |
# File 'lib/aka.rb', line 10 def initialize incoming_alias_file = nil @aliasPattern = /alias (\S+)="(.+)"/ @aliases = {} @defaults_file = "#{ENV["HOME"]}/.aka" @defaults = get_defaults_from_file debug "Incoming Alias file is #{incoming_alias_file}" if incoming_alias_file @fileName = "#{ENV["HOME"]}/#{incoming_alias_file}" if incoming_alias_file != @defaults['alias-file'] change_alias_file incoming_alias_file change_defaults "alias-file", incoming_alias_file end else @fileName = "#{ENV["HOME"]}/.alias" end debug "The File we're looking at is #{@fileName}" if !File.exists? @fileName FileUtils.touch @fileName end debug ("made it to the class. fileName is #{@fileName}, aliasPattern is #{@aliasPattern}.") File.foreach(@fileName) do |line| @aliasPattern.match(line)do |match| #read them all into an array @aliases[match[1]] = match[2] end end end |
Instance Method Details
#add(string, command) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/aka.rb', line 39 def add string, command debug "string: #{string} command: #{command}" #add a new alias to the list @aliases[string] = command writeOut info "#{string} added to your alias list." end |
#backup ⇒ Object
95 96 97 98 |
# File 'lib/aka.rb', line 95 def backup FileUtils.copy(@fileName,"#{@fileName}.bak") info "Backup created as #{@fileName}.bak" end |
#change_alias_file(new_file_name) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/aka.rb', line 106 def change_alias_file new_file_name new_file_name = "#{ENV['HOME']}/#{new_file_name}" info "Copy current aliases into the new file?(Yna)" copy = gets.chomp.downcase if copy == "n" FileUtils.touch(new_file_name) @fileName = new_file_name elsif copy == "a" "creation of new aliases aborted!" else debug "Copying aliases from #{@defaults['alias-file']} to #{new_file_name}" FileUtils.copy("#{ENV['HOME']}/#{@defaults['alias-file']}",new_file_name) @fileName = new_file_name end end |
#change_defaults(name, value) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/aka.rb', line 131 def change_defaults name, value if @defaults[name] == nil ||@defaults[name] != value @defaults[name] = value File.open(@defaults_file,"w"){|file| file.write @defaults.to_yaml} info "Changed the default for #{name} to #{value}" true else debug "No changes required!" false end end |
#empty ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/aka.rb', line 76 def empty info "Would you like to create a backup before deleting? (y/n/a)" backConfirm = gets.chomp.downcase if backConfirm.start_with? "y" backup @aliases = {} writeOut info "Alias list deleted." elsif backConfirm.start_with? "a" info "Alias list deletion aborted." elsif backConfirm.start_with? "n" @aliases = {} writeOut info "Alias list deleted." else info "I didn't understand your response; so your file was not deleted." end end |
#get_defaults_from_file ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/aka.rb', line 123 def get_defaults_from_file if File.exists? @defaults_file YAML::load(File.read(@defaults_file)) else {'alias-file'=>".alias"} end end |
#list ⇒ Object
53 54 55 56 57 |
# File 'lib/aka.rb', line 53 def list keyList = "" @aliases.keys.each{|key| keyList = keyList+" #{key}" } info(keyList) end |
#remove(string) ⇒ Object
47 48 49 50 51 |
# File 'lib/aka.rb', line 47 def remove string @aliases.delete string writeOut info "#{string} removed from your alias list." end |
#show(string) ⇒ Object
72 73 74 |
# File 'lib/aka.rb', line 72 def show string info(@aliases[string]) end |
#showAll ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/aka.rb', line 59 def showAll # A whole bunch of rigamarole to make all the line items the same width. length = 0 @aliases.keys.each do |k| length = k.length if k.length > length end eq_string = "=" * (length + 7) #7 is the length of the string ": Value" length = length * -1 keyList = sprintf("\n\n%1$*2$s: %3$s\n%4$s\n","Alias",length,"Value",eq_string) @aliases.each{|key, value| keyList = keyList + sprintf("%1$*2$s: %3$s\n",key, length, value)} info keyList end |
#writeOut ⇒ Object
100 101 102 103 104 |
# File 'lib/aka.rb', line 100 def writeOut filestring = "" @aliases.sort.each { |key, value| filestring = filestring + "alias #{key}=\"#{value}\"\n" } File.open(@fileName, "w") { |file| file.write filestring } end |