Description

Creates aliases for class methods, instance methods, constants, delegated methods and more. Aliases can be easily searched or saved as YAML config files to load later. Custom alias types are easy to create with the DSL Alias provides. Although Alias was created with the irb user in mind, any Ruby console program can hook into Alias for creating configurable aliases.

Setup

Install the gem with:

sudo gem install cldwalker-alias -s http://gems.github.com

The basic setup is simply drop two lines in your .irbrc:

require 'alias' 
Alias.create

This will assume a file in config/alias.yml or ~/.alias.yml. If you want it somewhere else, pass a :file option to create():

Alias.create :file=>"/path/to/my/clandestine_aliases.yml"

If you’d like to define your aliases without a config file, pass Alias.create() an :aliases option:

Alias.create :verbose=>true, :aliases=>{
  :constant=>{'Array' = 'A'},
  :instance_method=>{'String'=>{'downcase'=>'dc' }, 'Array'=>{'select'=>'s'}}
}

Usage

An example within Rails’ script/console:

bash> script/console
>> require 'alias'
=> true

# Import alias methods
>> extend Alias::Console
=> main

# First let's see what ruby code Alias generates to create an alias.
>> create_aliases :class_method, {"ActiveRecord::Base"=>{'find'=>'[]'}}, :pretend=>true

class ::ActiveRecord::Base; class<< self; alias_method :[], :find; end; end
=> true

# Create the above class method alias
>> create_aliases :class_method, "ActiveRecord::Base"=>{'find'=>'[]'}
=> true

# Create the above constant alias
>> create_aliases :constant, "ActiveRecord::Base"=>"AB"
=> true
# Verify that it worked
>> AB
=> ActiveRecord::Base

# If we try to create the constant alias again, Alias prevents us and warns us
>> create_aliases :constant, "ActiveRecord::Base"=>"AB"
Constant 'AB' not created since it already exists
=> false
# We can force Alias to override a method, class or constant that already exists
>> create_aliases :constant, {"ActiveRecord::Base"=>"AB"}, :force=>true
=> true

# Create the above instance method alias
>> create_aliases :instance_method, "ActiveRecord::Base"=>{"update_attribute"=>'ua'}
=> true

# By default aliases are saved to config/alias.yml in rails or ~/.alias.yml if not.
>> save_aliases
Saved created aliases to config/alias.yml.
=> true

Configuration

For an example config file see test/aliases.yml. For an explanation of the config file format see Alias.config_file.

Creating Custom Alias Types

See Alias::Creator.

Todo

  • Allow loading of select aliases in a file.

  • Provide a way to autogenerate aliases with a given proc.