cldwalker-alias 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT LICENSE
2
+
3
+ Copyright (c) 2008 Gabriel Horner
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,74 @@
1
+ == Description
2
+
3
+ Creates aliases for class methods, instance methods and constants by specifying
4
+ them in a YAML config file. By storing your aliases, it becomes possible to easily
5
+ share and search them. Although the default aliases are created with the irb user in mind,
6
+ it's possible to create your own alias types. See 'Customize Alias' below.
7
+
8
+ == Setup
9
+
10
+ Install the gem with:
11
+
12
+ sudo gem install cldwalker-alias -s http://gems.github.com
13
+
14
+ The easiest setup simply involves dropping two lines in your .irbrc:
15
+
16
+ require 'alias'
17
+ Alias.init
18
+
19
+ This setup assumes you have an aliases.yml in the current directory or a config/aliases.yml.
20
+ If neither is the case, pass a :file option to init():
21
+
22
+ Alias.init :file=>"/path/to/my/clandestine_aliases.yml"
23
+
24
+ If you'd like to define your aliases without a config file, pass Alias.init() a block and
25
+ call methods as if they were top level keys in the config:
26
+
27
+ Alias.init do |a|
28
+ a.verbose = true
29
+ a.constant = {'Array' = 'A'}
30
+ a.instance_method = {'String'=>{'downcase'=>'dc' }, 'Array'=>{'select'=>'s'}}
31
+ end
32
+
33
+ == Configuration
34
+
35
+ For an example config file see aliases.yml.example.
36
+
37
+ Options are:
38
+
39
+ * verbose : true or false
40
+ * constant: Alias constants no matter how many levels down. Takes a hash of constants as strings,
41
+ mapping constants to their aliases.
42
+ Example: {'Date'=>'D', 'ActiveRecord::Base'=>'AB'}
43
+ * instance_method : Alias methods for the instances of specified classes. Takes a nested hash with Ruby classes as string keys. Each class should point
44
+ to a hash of methods, with the original methods mapping to the alias method.
45
+ Example: {'String'=>{'downcase'=>'dc'}}
46
+ * class_method : Alias class methods. Takes the same format as instance_method.
47
+ Example: {'Date'=>{'day_fraction_to_time'=>'dftt'}}
48
+
49
+ == Customize Alias
50
+
51
+ You can create your own alias type by creating an Alias::*Creator Class. The * is there for the alias type name.
52
+ Suppose you want to create a my_instance_method alias type. Simply pass a my_instance_method key
53
+ with its appropriate aliases hash in the config. Then create your my_instance_method_creator class:
54
+
55
+ module Alias
56
+ class MyInstanceMethodCreator < Creator
57
+ def create_aliases(aliases_hash)
58
+ # My way of aliasing
59
+ # ...
60
+ end
61
+ end
62
+ end
63
+
64
+ Notice that the class should be a camelized version of the alias type and inherit from Alias::Creator.
65
+ At a minimum, make a create_aliases() to make custom aliases from the config data.
66
+ See creator.rb for more methods you can hook into.
67
+
68
+ == Todo
69
+
70
+ * Provide an auto_alias method for creators.
71
+ * List and search your aliases which can be easy to forget.
72
+ * Alias/forward methods from one object to another. This will be useful for creating
73
+ 'commands' in irb.
74
+ * Add more RDoc documentation.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ begin
5
+ require 'rcov/rcovtask'
6
+
7
+ Rcov::RcovTask.new do |t|
8
+ t.libs << 'test'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ t.rcov_opts = ["-T -x '/Library/Ruby/*'"]
11
+ t.verbose = true
12
+ end
13
+ rescue LoadError
14
+ puts "Rcov not available. Install it for rcov-related tasks with: sudo gem install rcov"
15
+ end
16
+
17
+ begin
18
+ require 'jeweler'
19
+ Jeweler::Tasks.new do |s|
20
+ s.name = "alias"
21
+ s.description = "Provides aliases for class names, class methods, instance methods and more. Mainly for console use."
22
+ s.email = "gabriel.horner@gmail.com"
23
+ s.homepage = "http://github.com/cldwalker/alias"
24
+ s.summary = s.description
25
+ s.authors = ["Gabriel Horner"]
26
+ s.has_rdoc = true
27
+ s.files = FileList["Rakefile", "README.rdoc", "LICENSE.txt", "aliases.yml.example", "{lib,test}/**/*"]
28
+ end
29
+
30
+ rescue LoadError
31
+ puts "Jeweler not available. Install it for jeweler-related tasks with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
32
+ end
33
+
34
+ Rake::TestTask.new do |t|
35
+ t.libs << 'lib'
36
+ t.pattern = 'test/**/*_test.rb'
37
+ t.verbose = false
38
+ end
39
+
40
+ Rake::RDocTask.new do |rdoc|
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = 'test'
43
+ rdoc.options << '--line-numbers' << '--inline-source'
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
47
+
48
+ task :default => :test
@@ -0,0 +1,16 @@
1
+ verbose: true
2
+ constant:
3
+ ActiveRecord::Base: AB
4
+ ActionView::Base: AVB
5
+
6
+ class_method:
7
+ ActiveRecord::Base :
8
+ connection: cn
9
+ find: f
10
+ delete: d
11
+
12
+ instance_method:
13
+ ActiveRecord::Base :
14
+ update_attribute: ua
15
+ Array :
16
+ select: s
@@ -0,0 +1,9 @@
1
+ module Alias
2
+ class ClassMethodCreator < Creator
3
+ include Alias::MethodCreatorHelper
4
+
5
+ def method_exists?(klass, method)
6
+ klass.respond_to?(method)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,46 @@
1
+ module Alias
2
+ class ConstantCreator < Creator
3
+
4
+ def delete_invalid_aliases(aliases_hash)
5
+ delete_invalid_class_keys(aliases_hash)
6
+ end
7
+
8
+ def delete_existing_aliases(aliases_hash)
9
+ aliases_hash.each do |k, v|
10
+ if (klass = Object.any_const_get(v)) && ! alias_map.values.include?(v)
11
+ aliases_hash.delete(k)
12
+ puts "Alias '#{v}' deleted since the constant already exists" if self.verbose
13
+ end
14
+ end
15
+ end
16
+
17
+ def create_aliases(aliases_hash)
18
+ eval_string = ''
19
+ aliases_hash.each {|k,v|
20
+ eval_string += "#{v} = #{k}\n"
21
+ }
22
+ Object.class_eval eval_string
23
+ end
24
+
25
+ def generate_aliases(array_to_alias)
26
+ make_shortest_aliases(array_to_alias)
27
+ end
28
+
29
+ def make_shortest_aliases(unaliased_strings)
30
+ shortest_aliases = {}
31
+ possible_alias = ''
32
+ unaliased_strings.each {|s|
33
+ possible_alias = ''
34
+ s.split('').each { |e|
35
+ possible_alias += e
36
+ if ! shortest_aliases.values.include?(possible_alias)
37
+ shortest_aliases[s] = possible_alias
38
+ break
39
+ end
40
+ }
41
+ }
42
+
43
+ shortest_aliases
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,52 @@
1
+ class Hash
2
+ unless self.method_defined?(:slice)
3
+ # simplified from ActiveSupport
4
+ def slice(*keys)
5
+ reject { |key,| !keys.include?(key) }
6
+ end
7
+ end
8
+
9
+ def slice_off!(*keys)
10
+ new_hash = slice(*keys)
11
+ keys.each {|e| self.delete(e)}
12
+ new_hash
13
+ end
14
+
15
+ unless self.method_defined?(:stringify_keys)
16
+ #from ActiveSupport
17
+ def stringify_keys
18
+ inject({}) do |options, (key, value)|
19
+ options[key.to_s] = value
20
+ options
21
+ end
22
+ end
23
+ end
24
+
25
+ end
26
+
27
+ class Object
28
+ def self.any_const_get(name)
29
+ begin
30
+ klass = Object
31
+ name.split('::').each {|e|
32
+ klass = klass.const_get(e)
33
+ }
34
+ klass
35
+ rescue; nil; end
36
+ end
37
+ end
38
+
39
+ class String
40
+ unless self.method_defined?(:camelize)
41
+ #simplified from ActiveSupport
42
+ def camelize
43
+ self.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
44
+ end
45
+ end
46
+ end
47
+
48
+ class OpenStruct
49
+ def to_hash
50
+ @table
51
+ end
52
+ end
@@ -0,0 +1,81 @@
1
+ # This is the base creator class from which other *Creator classes inherit.
2
+ # Methods this class provides to non-creator classes are: Creator.create()
3
+ # Methods this class provides to creator classes to be overridden: @creator.delete_invalid_aliases
4
+ # and @creator.create_aliases
5
+
6
+ module Alias
7
+ class Creator
8
+
9
+ attr_accessor :verbose, :alias_map, :force
10
+ def initialize(aliases_hash={})
11
+ @alias_map = aliases_hash
12
+ @verbose = false
13
+ @force = false
14
+ end
15
+
16
+ # Options are:
17
+ # * :auto_alias : Array of constants to alias by shortest available constant. For example,
18
+ # if the constant A already exists, then Aardvark would be aliased to Aa.
19
+ def self.create(aliases_hash, options={})
20
+ obj = new(aliases_hash)
21
+ obj.verbose = options['verbose'] if options['verbose']
22
+ obj.force = options['force'] if options['force']
23
+ obj.create(obj.alias_map)
24
+ if options['auto_alias']
25
+ obj.alias_map = obj.alias_map.merge(obj.auto_create(options['auto_alias']))
26
+ end
27
+ obj
28
+ end
29
+
30
+ #needs to return generated aliases_hash
31
+ def auto_create(array_to_alias)
32
+ aliases_hash = generate_aliases(array_to_alias)
33
+ create(aliases_hash)
34
+ aliases_hash
35
+ end
36
+
37
+ def create(aliases_hash)
38
+ delete_invalid_aliases(aliases_hash)
39
+ delete_existing_aliases(aliases_hash) unless self.force
40
+ @alias_map = aliases_hash
41
+ #td: create method for efficiently removing constants/methods in any namespace
42
+ silence_warnings {
43
+ create_aliases(aliases_hash)
44
+ }
45
+ end
46
+
47
+ # Should be overridden to delete aliases that point to invalid/nonexistent classes, methods ...
48
+ def delete_invalid_aliases(aliases_hash); end
49
+
50
+ # Should be overridden to delete aliases that already exist. This method can be bypassed by passing
51
+ # a force option to the creator.
52
+ def delete_existing_aliases(aliases_hash); end
53
+
54
+ # Must be overridden to use create()
55
+ def create_aliases(aliases_hash);
56
+ raise "This abstract method must be overridden."
57
+ end
58
+
59
+ # Must be overridden to use auto_create()
60
+ def generate_aliases(array_to_alias);
61
+ raise "This abstract method must be overridden."
62
+ end
63
+
64
+ def delete_invalid_class_keys(klass_hash)
65
+ klass_hash.each {|k,v|
66
+ if Object.any_const_get(k).nil?
67
+ puts "deleted nonexistent klass #{k} #{caller[2].split(/:/)[2]}" if self.verbose
68
+ klass_hash.delete(k)
69
+ end
70
+ }
71
+ end
72
+
73
+ private
74
+ def silence_warnings
75
+ old_verbose, $VERBOSE = $VERBOSE, nil
76
+ yield
77
+ ensure
78
+ $VERBOSE = old_verbose
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,9 @@
1
+ module Alias
2
+ class InstanceMethodCreator < Creator
3
+ include Alias::MethodCreatorHelper
4
+
5
+ def method_exists?(klass, method)
6
+ klass.method_defined?(method)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,40 @@
1
+ # This class manages creation of aliases.
2
+ module Alias
3
+ class Manager
4
+
5
+ def initialize #:nodoc:
6
+ @alias_creators = {}
7
+ @verbose = false
8
+ @force = false
9
+ end
10
+
11
+ attr_accessor :alias_creators, :verbose, :force
12
+ def alias_types; @alias_creators.keys; end
13
+
14
+ def factory_create_aliases(alias_type, aliases_hash)
15
+ creator_class_string = "Alias::#{alias_type.camelize}Creator"
16
+ create_options = aliases_hash.slice_off!('auto_alias', 'verbose', 'force')
17
+ create_options['verbose'] = @verbose unless create_options.has_key?('verbose')
18
+ if creator_class = Object.any_const_get(creator_class_string)
19
+ creator_class.create(aliases_hash, create_options)
20
+ else
21
+ puts "Creator class '#{creator_class_string}' not found." if @verbose
22
+ nil
23
+ end
24
+ end
25
+
26
+ def create_aliases(alias_type, aliases_hash)
27
+ aliases_hash = aliases_hash.dup
28
+ if obj = factory_create_aliases(alias_type.to_s, aliases_hash.dup)
29
+ @alias_creators[alias_type.to_sym] ||= obj
30
+
31
+ accessor_method = "#{alias_type}_aliases"
32
+ if ! respond_to?(accessor_method)
33
+ self.class.send :attr_accessor, accessor_method
34
+ end
35
+ self.send("#{accessor_method}=", {}) if self.send(accessor_method).nil?
36
+ self.send(accessor_method).merge! obj.alias_map
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,69 @@
1
+ module Alias
2
+ module MethodCreatorHelper
3
+ def delete_invalid_aliases(aliases_hash)
4
+ delete_invalid_class_keys(aliases_hash)
5
+ delete_invalid_method_keys(aliases_hash)
6
+ end
7
+
8
+ def delete_existing_aliases(aliases_hash)
9
+ delete_existing_method_aliases(aliases_hash)
10
+ end
11
+
12
+ def create_aliases(aliases_hash)
13
+ create_method_aliases(aliases_hash)
14
+ end
15
+
16
+ def method_exists?(klass, method)
17
+ raise "This abstract method must be overridden."
18
+ end
19
+
20
+ def delete_invalid_method_keys(alias_hash)
21
+ alias_hash.each do |k, methods|
22
+ if klass = Object.any_const_get(k)
23
+ methods.keys.each do |e|
24
+ if ! method_exists?(klass,e)
25
+ methods.delete(e)
26
+ puts "#{klass}: alias to method '#{e}' deleted since it doesn't exist" if self.verbose
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ def delete_existing_method_aliases(aliases_hash)
34
+ aliases_hash.each do |k, methods_hash|
35
+ if klass = Object.any_const_get(k)
36
+ methods_hash.each do |a,b|
37
+ if method_exists?(klass,b) && !(alias_map[k].is_a?(Hash) && alias_map[k].values.include?(b))
38
+ methods_hash.delete(a)
39
+ puts "#{klass}: alias '#{b}' deleted since the method already exists" if self.verbose
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ def create_method_aliases_per_class(klass, alias_hash)
47
+ eval_string = ""
48
+ alias_hash.each {|original_method, alias_methods|
49
+ alias_methods = [alias_methods] unless alias_methods.is_a?(Array)
50
+ alias_methods.each { |a|
51
+ eval_string += "alias_method :#{a}, :#{original_method}\n"
52
+ }
53
+ }
54
+ if self.is_a?(ClassMethodCreator)
55
+ eval_string = "class <<self\n #{eval_string}\nend"
56
+ end
57
+ klass.class_eval eval_string
58
+ end
59
+
60
+ def create_method_aliases(aliases)
61
+ aliases ||= {}
62
+ aliases.each { |k,alias_hash|
63
+ if klass = Object.any_const_get(k)
64
+ create_method_aliases_per_class(klass, alias_hash)
65
+ end
66
+ }
67
+ end
68
+ end
69
+ end
data/lib/alias.rb ADDED
@@ -0,0 +1,55 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__))
2
+ require 'yaml'
3
+ require 'ostruct'
4
+ require 'alias/manager'
5
+ require 'alias/creator'
6
+ require 'alias/constant_creator'
7
+ require 'alias/method_creator_helper'
8
+ require 'alias/instance_method_creator'
9
+ require 'alias/class_method_creator'
10
+ require 'alias/core_extensions'
11
+
12
+ module Alias
13
+ extend self
14
+
15
+ def load_config_file(file=nil)
16
+ if file.nil?
17
+ if File.exists?("config/aliases.yml")
18
+ file = "config/aliases.yml"
19
+ elsif File.exists?("aliases.yml")
20
+ file = "aliases.yml"
21
+ end
22
+ end
23
+ file ? YAML::load(File.read(file)) : {}
24
+ end
25
+
26
+ def init(options={})
27
+ config_hash = load_config_file(options[:file])
28
+ config.merge! config_hash
29
+ config['verbose'] = options[:verbose] if !options[:verbose].nil?
30
+
31
+ if block_given?
32
+ obj = OpenStruct.new
33
+ yield(obj)
34
+ block_config = obj.to_hash.stringify_keys
35
+ config.merge! block_config
36
+ end
37
+
38
+ manager.verbose = config['verbose'] if config.has_key?('verbose')
39
+ config.each do |k,v|
40
+ next if ['verbose'].include?(k)
41
+ manager.create_aliases(k, v)
42
+ end
43
+ self
44
+ end
45
+
46
+ def config
47
+ @config ||= {}
48
+ end
49
+
50
+ def config=(value); @config = value; end
51
+
52
+ def manager
53
+ @manager ||= Manager.new
54
+ end
55
+ end
@@ -0,0 +1,62 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+ class AliasTest < Test::Unit::TestCase
4
+ test "loads config file config/aliases.yml if found" do
5
+ File.expects(:exists?).with('config/aliases.yml').returns(true)
6
+ File.expects(:read).returns('')
7
+ Alias.load_config_file
8
+ end
9
+
10
+ test "loads config file aliases.yml if found" do
11
+ File.stubs(:exists?).returns(false, true)
12
+ File.stubs(:read).returns('')
13
+ Alias.load_config_file
14
+ end
15
+
16
+ test "loads given config file" do
17
+ config = Alias.load_config_file(File.join(File.dirname(__FILE__),'aliases.yml'))
18
+ assert config.is_a?(Hash)
19
+ end
20
+
21
+ test "returns hash if no config file found" do
22
+ File.stubs(:exists?).returns(false)
23
+ Alias.load_config_file.should == {}
24
+ end
25
+
26
+ context "Alias_init" do
27
+ before(:each) { Alias.config = {}}
28
+
29
+ test "with block sets config properly" do
30
+ Alias.manager.expects(:create_aliases).times(2)
31
+ Alias.init do |a|
32
+ a.verbose = true
33
+ a.constant = {'Blah'=>'B'}
34
+ a.instance_method = {'String'=>{'to_s'=>'s'}}
35
+ end
36
+ expected_config = {"instance_method"=>{"String"=>{"to_s"=>"s"}}, "constant"=>{"Blah"=>"B"}, "verbose"=>true}
37
+ Alias.config.should == expected_config
38
+ end
39
+
40
+ test "creates manager object and non-empty aliases" do
41
+ Alias.init :file=>File.join(File.dirname(__FILE__),'aliases.yml')
42
+ Alias.manager.instance_method_aliases.empty?.should_not be(true)
43
+ Alias.manager.class_method_aliases.empty?.should_not be(true)
44
+ Alias.manager.constant_aliases.empty?.should_not be(true)
45
+ end
46
+
47
+ test "with verbose option sets config and manager verbosity" do
48
+ Alias.manager.stubs(:create_aliases)
49
+ Alias.init :verbose=>true
50
+ assert Alias.config['verbose']
51
+ assert Alias.manager.verbose
52
+ end
53
+
54
+ test "with no verbose option doesn't set config and manager verbosity" do
55
+ Alias.manager.stubs(:create_aliases)
56
+ assert Alias.manager.expects(:verbose=).never
57
+ Alias.init
58
+ assert Alias.config['verbose'].nil?
59
+ end
60
+ end
61
+
62
+ end
data/test/aliases.yml ADDED
@@ -0,0 +1,11 @@
1
+ constant:
2
+ Object: O
3
+
4
+ class_method:
5
+ Object:
6
+ const_get: cg
7
+
8
+ instance_method:
9
+ String:
10
+ to_s: ts
11
+ bling: bl
@@ -0,0 +1,34 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+ class Alias::CoreExtensionsTest < Test::Unit::TestCase
4
+ test "any_const_get fetches simple class" do
5
+ Object.any_const_get("Array").should == Array
6
+ end
7
+
8
+ test "any_const_get fetches nested class" do
9
+ eval "module ::Somemodule; class Someclass; end; end"
10
+ Object.any_const_get("Somemodule::Someclass").should == Somemodule::Someclass
11
+ end
12
+
13
+ test "any_const_get returns nil for nonexistent class" do
14
+ Object.any_const_get("NonexistentClass").should == nil
15
+ end
16
+
17
+ test "slice only returns valid keys given" do
18
+ {:a=>1, :b=>2}.slice(:a, :c).should == {:a=>1}
19
+ end
20
+
21
+ test "slice_off! returns given keys but takes them off existing hash" do
22
+ h = {:a=>1, :b=>2}
23
+ h.slice_off!(:a, :c).should == {:a=>1}
24
+ h.should == {:b=>2}
25
+ end
26
+
27
+ test "camelize should uppercase non-underscored string" do
28
+ 'man'.camelize.should == 'Man'
29
+ end
30
+
31
+ test "camelize should camelize underscored string" do
32
+ 'some_test'.camelize.should == 'SomeTest'
33
+ end
34
+ end
@@ -0,0 +1,35 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+ class Alias::CreatorTest < Test::Unit::TestCase
4
+ context "AliasConstantCreator" do
5
+ before(:each) { @creator = Alias::ConstantCreator.new}
6
+
7
+ test "deletes existing aliases" do
8
+ h1 = {"Alias::ConstantCreator"=>"Alias::Creator", "Array"=>"Ar"}
9
+ @creator.delete_existing_aliases(h1)
10
+ h1.should == {"Array"=>"Ar"}
11
+ end
12
+
13
+ test "deletes existing alias unless it was created by the object" do
14
+ h1 = {"Array"=>"A"}
15
+ @creator.create(h1)
16
+ assert_not_equal A, ArgumentError
17
+ h2 = {"ArgumentError"=>"A"}
18
+ @creator.create(h2)
19
+ assert_equal A, ArgumentError
20
+ end
21
+
22
+ test "makes shortest aliases" do
23
+ eval "::Y = 'some value'"
24
+ expected_hash = {"Yo"=>"Y", "Man"=>"M", "Cool"=>"C", 'Yay'=>'Ya'}
25
+ @creator.make_shortest_aliases(['Yo','Yay','Cool','Man']).should == expected_hash
26
+ end
27
+ end
28
+
29
+ test "Creator deletes invalid class keys" do
30
+ h1 = {'Alias::Creator'=>'whoop','Yay'=>'Haha'}
31
+ @creator = Alias::Creator.new
32
+ @creator.delete_invalid_class_keys(h1)
33
+ h1.should == {'Alias::Creator'=>'whoop'}
34
+ end
35
+ end
@@ -0,0 +1,52 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+ class Alias::ManagerTest < Test::Unit::TestCase
4
+ before(:each) { @manager = Alias::Manager.new}
5
+
6
+ context "Manager" do
7
+ test "verbosity trickles down to creator objects" do
8
+ h1 = {'String'=>'Strang'}
9
+ @manager.verbose = true
10
+ @manager.create_aliases(:constant, h1)
11
+ assert @manager.alias_creators[:constant].verbose
12
+ end
13
+
14
+ test "force option sets force in creator object" do
15
+ h1 = {'force'=>true}
16
+ @manager.create_aliases(:constant, h1)
17
+ assert @manager.alias_creators[:constant].force
18
+ end
19
+
20
+ test "creates constant aliases" do
21
+ h1 = {'Time'=>'T', 'auto_alias'=>['Date']}
22
+ @manager.create_aliases(:constant, h1)
23
+ @manager.constant_aliases.should == {'Time'=>'T', 'Date'=>'D'}
24
+ end
25
+
26
+ test "creates instance method aliases" do
27
+ Kernel.eval %[
28
+ class ::SampleClass
29
+ def whoop; 'WHOOP'; end
30
+ end
31
+ ]
32
+ obj = SampleClass.new
33
+ @manager.create_aliases(:instance_method, {'SampleClass'=>{:whoop=>:can_of_wass, :blah=>:bl}})
34
+ @manager.instance_method_aliases.should == {'SampleClass'=>{:whoop=>:can_of_wass}}
35
+ SampleClass.new.whoop.should == SampleClass.new.can_of_wass
36
+ end
37
+
38
+ test "creates class method aliases" do
39
+ Kernel.eval %[
40
+ class ::SampleClass
41
+ def self.cap; 'itup'; end
42
+ end
43
+ ]
44
+ hash1 = {'SampleClass'=>{:cap=>:capohow}, 'Array'=>{:blah=>:bl}}
45
+ @manager.create_aliases(:class_method, hash1)
46
+ expected_result = {"SampleClass"=>{:cap=>:capohow}, "Array"=>{}}
47
+ assert_equal expected_result, @manager.class_method_aliases
48
+ SampleClass.capohow.should == SampleClass.cap
49
+ end
50
+ end
51
+
52
+ end
@@ -0,0 +1,54 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+ class Alias::MethodCreatorHelperTest < Test::Unit::TestCase
4
+
5
+ context "ClassMethodCreator" do
6
+ before(:each) { @creator = Alias::ClassMethodCreator.new }
7
+
8
+ test "deletes invalid class method keys" do
9
+ h1 = {'String'=>{'yaml_new'=>'yn'},'Array'=>{'blah'=>'bl'}}
10
+ @creator.delete_invalid_method_keys(h1)
11
+ h1.should == {"Array"=>{}, "String"=>{'yaml_new'=>'yn'}}
12
+ end
13
+
14
+ test "deletes existing class method aliases" do
15
+ h1 = {'Date'=>{'civil_to_jd'=>'civil', 'valid_time?'=>'vt'} }
16
+ @creator.delete_existing_method_aliases(h1)
17
+ h1.should == {'Date'=>{'valid_time?'=>'vt'} }
18
+ end
19
+
20
+ test "deletes existing class method unless it was created by the object" do
21
+ h1 = {'String'=>{'name'=>'n'}}
22
+ @creator.create(h1)
23
+ assert_not_equal 'blah', String.n
24
+ h2 = {'String'=>{'new'=>'n'}}
25
+ @creator.create(h2)
26
+ assert_equal 'blah', String.n('blah')
27
+ end
28
+ end
29
+
30
+ context "InstanceMethodCreator" do
31
+ before(:each) { @creator = Alias::InstanceMethodCreator.new }
32
+
33
+ test "deletes existing instance method aliases" do
34
+ h1 = {'String'=>{'strip'=>'st', 'chomp'=>'chop'}}
35
+ @creator.delete_existing_method_aliases(h1)
36
+ h1.should == {"String"=>{"strip"=>"st"}}
37
+ end
38
+
39
+ test "deletes existing instance method unless it was created by the object" do
40
+ h1 = {'String'=>{'downcase'=>'d'}}
41
+ @creator.create(h1)
42
+ assert_not_equal 'bh', 'blah'.d
43
+ h2 = {'String'=>{'delete'=>'d'}}
44
+ @creator.create(h2)
45
+ assert_equal 'bh', 'blah'.d('la')
46
+ end
47
+
48
+ test "deletes invalid instance method keys" do
49
+ h1 = {'String'=>{'strip'=>'st'},'Array'=>{'blah', 'bl'}}
50
+ @creator.delete_invalid_method_keys(h1)
51
+ h1.should == {"Array"=>{}, "String"=>{"strip"=>"st"}}
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'context' #gem install jeremymcanally-context -s http://gems.github.com
4
+ require 'mocha' #gem install mocha
5
+ require 'matchy' #gem install jeremymcanally-stump -s http://gems.github.com
6
+ require 'pending'
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
8
+ require 'alias'
9
+
10
+ class Test::Unit::TestCase
11
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cldwalker-alias
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Gabriel Horner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-20 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Provides aliases for class names, class methods, instance methods and more. Mainly for console use.
17
+ email: gabriel.horner@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - Rakefile
26
+ - README.rdoc
27
+ - LICENSE.txt
28
+ - aliases.yml.example
29
+ - lib/alias
30
+ - lib/alias/class_method_creator.rb
31
+ - lib/alias/constant_creator.rb
32
+ - lib/alias/core_extensions.rb
33
+ - lib/alias/creator.rb
34
+ - lib/alias/instance_method_creator.rb
35
+ - lib/alias/manager.rb
36
+ - lib/alias/method_creator_helper.rb
37
+ - lib/alias.rb
38
+ - test/alias_test.rb
39
+ - test/aliases.yml
40
+ - test/core_extensions_test.rb
41
+ - test/creator_test.rb
42
+ - test/manager_test.rb
43
+ - test/method_creator_helper_test.rb
44
+ - test/test_helper.rb
45
+ has_rdoc: true
46
+ homepage: http://github.com/cldwalker/alias
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.2.0
68
+ signing_key:
69
+ specification_version: 2
70
+ summary: Provides aliases for class names, class methods, instance methods and more. Mainly for console use.
71
+ test_files: []
72
+