lib/alias/console.rb in cldwalker-alias-0.1.2 vs lib/alias/console.rb in cldwalker-alias-0.2.0

- old
+ new

@@ -1,56 +1,20 @@ -#Usage: include Alias::Console wherever you want to use these methods module Alias + # This module contains the main methods to be accessed from a ruby shell i.e. irb. Simply extend Alias::Console in your ruby shell. module Console - def self.included(base) - base.extend self + # See Alias::Manager.create_aliases for usage. + def create_aliases(*args) + Alias.manager.console_create_aliases(*args) end - - def create(*args) - Alias.manager.create_aliases(*args) + + # Saves aliases to a file. If no file is given, defaults to config/alias.yml if the config directory exists (for Rails). + # Otherwise defaults to ~/.alias.yml. + def save_aliases(file=nil) + Alias.manager.save_aliases(file) end - - #options: type, raw, class, sort - #s 'man', :type=>'instance_method' - #s /ma/, :raw=>true - def search(*args) - options = args[-1].is_a?(Hash) ? args[-1].slice_off!(:raw, :sort) : {} - if args[0] && ! (args[0].is_a?(Hash) && args[0].empty?) - if args[0].is_a?(String) or args[0].is_a?(Regexp) - search_hash = {:name=>args[0]} - search_hash.merge!(args[1]) if args[1].is_a?(Hash) - elsif args[0].is_a?(Hash) - search_hash = args[0] - end - result = Alias.manager.search(search_hash) - else - result = Alias.manager.list - end - - if options[:sort] - result = result.sort {|a,b| - (a[options[:sort]].nil? || b[options[:sort]].nil?) ? 1 : - (a[options[:sort]]) <=> b[options[:sort]] - } - end - if options[:raw] - result - else - format_search(result, options) - nil - end + + # Searches aliases with a search term as defined by Alias::Manager.search. If no arguments given, all aliases are listed. + def search_aliases(*args) + args.empty? ? Alias.manager.all_aliases : Alias.manager.search(*args) end - - def format_search(result, options) - body = '' - if result.empty? - body = "No results" - else - result.each do |e| - h = e.slice_off!(:name, :alias) - body += "#{h[:alias]} = #{h[:name]} (" + e.map {|k,v| "#{k}: #{v}"}.join(", ") + ")\n" - end - end - puts body - end end end \ No newline at end of file