Class Alias::Manager

  1. lib/alias/manager.rb
Parent: Object

This class manages creation, searching and saving of aliases. Aliases are created as follows:

Attributes

created_aliases [R] A hash of created aliases by creator type.
creators [R] A hash of creator objects that have been used by creator type.
force [RW] When true, optional validations will be skipped. Takes an array of creator type symbols or a boolean to set all creators.
verbose [RW] When true, all failed validations print their message. Takes an array of creator type symbols or a boolean to set all creators.

Public instance methods

all_aliases ()

Returns an array of all created alias hashes. The alias hash will have a :type key which contains the creator type it belongs to.

[show source]
# File lib/alias/manager.rb, line 96
    def all_aliases
      @all_aliases ||= @creators.inject([]) do |t, (type, creator)|
        t += creator.aliases.each {|e| e[:type] = type.to_s}
      end
    end
console_create_aliases (creator_type, aliases_hash, options={})

Creates aliases in the same way as create_aliases while keeping track of what’s created. But differs in that creator types can be accessed with just the first few unique letters of a type. For example, you can pass :in to mean :instance_method. Also, the verbose flag is set by default. Examples:

console_create_aliases :in, "String"=>{"to_s"=>"s"}
console_create_aliases :con, {"ActiveRecord::Base"=>"AB"}, :pretend=>true
[show source]
# File lib/alias/manager.rb, line 50
    def console_create_aliases(creator_type, aliases_hash, options={})
      options = {:verbose=>true}.update(options)
      @created_aliases ||= {}
      creator_type = (all_creator_types.sort.find {|e| e[/^#{creator_type}/] } || creator_type).to_sym
      if create_aliases(creator_type, aliases_hash, options)
        @created_aliases[creator_type] = aliases_hash
        true
      else
        false
      end
    end
create_aliases (creator_type, aliases_hash, options={})

The main method for creating aliases. Takes a creator type, a hash of aliases whose format is defined per creator and the following options:

:verbose
Sets the verbose flag to print a message whenever an alias validation fails. Default is the creator’s verbose flag.
:force
Sets the force flag to bypass optional validations. Default is the creator’s manager flag.
:pretend
Instead of creating aliases, prints out the ruby code that would be evaluated by Kernel.eval to create the aliases. Default is false.
[show source]
# File lib/alias/manager.rb, line 32
    def create_aliases(creator_type, aliases_hash, options={})
      return unless (creator = create_creator(creator_type))
      creator.verbose = options[:verbose] ? options[:verbose] : verbose_creator?(creator_type)
      creator.force = options[:force] ? options[:force] : force_creator?(creator_type)
      creator.create(aliases_hash.dup, options[:pretend] || false)
      true
    rescue Creator::AbstractMethodError
      $stderr.puts $!.message
    rescue Creator::FailedAliasCreationError
      $stderr.puts "'#{creator.class}' failed to create aliases with error:\n#{$!.message}"
    end
save_aliases (file=nil)

Saves aliases that were created by console_create_aliases. Can take an optional file to save to. See Alias::Console.save_aliases for default files this method saves to.

[show source]
# File lib/alias/manager.rb, line 64
    def save_aliases(file=nil)
      if @created_aliases
        Alias.add_to_config_file(@created_aliases, file)
        true
      else
        puts "Didn't save. No created aliases detected."
        false
      end
    end
search (search_hash)

Searches all created alias hashes with a hash or a string. If a string, the alias key searched is :name. If a hash, the key should should be an alias key and the value the search term. All values are treated as regular expressions. Alias keys vary per creator but some of the common ones are :name, :class and :alias. Multiple keys for a hash will AND the searches. Examples:

search 'to_'
search :class=>"Array", :name=>'to'
[show source]
# File lib/alias/manager.rb, line 81
    def search(search_hash)
      result = nil
      reset_all_aliases
      search_hash = {:name=>search_hash} unless search_hash.is_a?(Hash)
      search_hash.each do |k,v|
        new_result = simple_search(k,v)
        #AND's searches
        result = intersection_of_two_arrays(new_result, result)
      end
      #duplicate results in case they are modified
      result = result.map {|e| e.dup} if result
      result
    end