Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created August 3, 2012 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhellberg/3247447 to your computer and use it in GitHub Desktop.
Save peterhellberg/3247447 to your computer and use it in GitHub Desktop.
Shorter way to retrieve aliases.
# encoding: utf-8
require 'minitest/spec'
require 'minitest/pride'
require 'minitest/autorun'
def long(shortcut)
case shortcut
when 'protos','proto','xorgprotos','xorg1','x1','1'
'xorg_protos'
when 'utils','xorgutils','xorg2','x2','2'
'xorg_utils'
when 'libs','libraries','xorg3','xorglibraries','x3','3'
'xorg_libraries'
when 'data','xorg4','xorgdata','x4','4'
'xorg_data'
when 'apps','xorg5','xorgapps','x5','5'
'xorg_apps'
when 'fonts','xorg6','xorgfonts','x6','6'
'xorg_fonts'
when 'server','xorg7','xorgserver','x7','7'
'xorg_server'
when 'addons','ruby_addons','rubyaddons','raddons','raddon',
'rub','r','rand','all','8'
'ruby_addons'
when 'python','mypython','pp','pyt','pyth','9'
'my_python'
when 'gnome','xorg10','10'
'gnome5'
when 'e','11','xorg11','e17'
'e17'
when 'games','gam'
'games'
when 'audio_suite','audsuite'
'aud_suite'
when 'video_base','vidbase','vid_base'
'vid_base'
when 'kde','kde3','kde4','kd','default'
'kde'
end
end
def aliases
{
'xorg_protos' => %w(protos proto xorgprotos xorg1 x1 1),
'xorg_utils' => %w(utils xorgutils xorg2 x2 2),
'xorg_libraries' => %w(libs libraries xorg3 xorglibraries x3 3),
'xorg_data' => %w(data xorg4 xorgdata x4 4),
'xorg_apps' => %w(apps xorg5 xorgapps x5 5),
'xorg_fonts' => %w(fonts xorg6 xorgfonts x6 6),
'xorg_server' => %w(server xorg7 xorgserver x7 7),
'ruby_addons' => %w(addons ruby_addons rubyaddons raddons raddon rub r rand all 8),
'my_python' => %w(python mypython pp pyt pyth 9),
'gnome5' => %w(gnome xorg10 10),
'e17' => %w(e 11 xorg11 e17),
'games' => %w(games gam),
'aud_suite' => %w(audio_suite audsuite),
'vid_base' => %w(video_base vidbase vid_base),
'kde' => %w(kde kde3 kde4 kd default)
}
end
def short(shortcut)
aliases.detect { |k| k[1].include? shortcut }[0]
end
class AliasLookup
attr_reader :table
def initialize(aliases)
@table = aliases.each_with_object({}) { |v,t| v[1].map { |k| t[k] = v[0] } }
end
def [](name)
@table[name]
end
end
describe "Aliases" do
it "both methods return the same aliases" do
long('1').must_equal short('xorg1')
long('11').must_equal short('e17')
long('vidbase').must_equal short('vid_base')
long('kde3').must_equal short('kd')
long('fonts').must_equal short('x6')
long('rub').must_equal short('r')
end
it "can be looked up using a table" do
lookup = AliasLookup.new(aliases)
10000.times do
s = aliases.to_a.sample
a = s[1].sample
puts "#{a.to_s.ljust(11)} -> #{s[0]}"
lookup[a].must_equal s[0]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment