cldwalker-alias 0.1.1 → 0.1.2
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 +1 -1
- data/README.rdoc +37 -2
- data/Rakefile +2 -1
- data/VERSION.yml +4 -0
- data/lib/alias/console.rb +56 -0
- data/lib/alias/constant_creator.rb +4 -0
- data/lib/alias/core_extensions.rb +0 -6
- data/lib/alias/creator.rb +26 -16
- data/lib/alias/manager.rb +55 -13
- data/lib/alias/method_creator_helper.rb +8 -0
- data/lib/alias.rb +6 -10
- data/lib/config_struct.rb +17 -0
- data/test/alias_test.rb +3 -3
- data/test/constant_creator_test.rb +33 -0
- data/test/creator_test.rb +60 -25
- data/test/manager_test.rb +98 -40
- data/test/method_creator_helper_test.rb +5 -0
- data/test/test_helper.rb +2 -2
- metadata +9 -4
data/LICENSE.txt
CHANGED
data/README.rdoc
CHANGED
@@ -30,6 +30,41 @@ call methods as if they were top level keys in the config:
|
|
30
30
|
a.instance_method = {'String'=>{'downcase'=>'dc' }, 'Array'=>{'select'=>'s'}}
|
31
31
|
end
|
32
32
|
|
33
|
+
== Irb/Console Use
|
34
|
+
|
35
|
+
To use in irb or your own console, simply include the console class:
|
36
|
+
|
37
|
+
irb>>include Alias::Console
|
38
|
+
=> Object
|
39
|
+
|
40
|
+
Currently only two methods are imported: search and create.
|
41
|
+
Search lists your aliases and create, well, creates aliases.
|
42
|
+
|
43
|
+
Create:
|
44
|
+
|
45
|
+
irb>>create :constant, "Alias"=>"A"
|
46
|
+
=>nil
|
47
|
+
|
48
|
+
irb>>A
|
49
|
+
=>Alias
|
50
|
+
|
51
|
+
Search:
|
52
|
+
|
53
|
+
# Searches all alias types for an Alias
|
54
|
+
irb>>search :alias=>'A'
|
55
|
+
A = Alias (type: constant)
|
56
|
+
|
57
|
+
#Searches all alias types for any aliases with an A
|
58
|
+
irb>>search :alias=>/A/
|
59
|
+
A = Alias (type: constant)
|
60
|
+
AB = ActiveRecord::Base (type: constant)
|
61
|
+
|
62
|
+
#Searches only class_method types named 'find'
|
63
|
+
irb>>search :type=>'class_method', :name=>'find'
|
64
|
+
[] = find (type: class_method, class: ActiveRecord::Base)
|
65
|
+
|
66
|
+
|
67
|
+
|
33
68
|
== Configuration
|
34
69
|
|
35
70
|
For an example config file see aliases.yml.example.
|
@@ -67,8 +102,8 @@ See creator.rb for more methods you can hook into.
|
|
67
102
|
|
68
103
|
== Todo
|
69
104
|
|
70
|
-
* Provide an auto_alias method for creators.
|
71
|
-
* List and search your aliases which can be easy to forget.
|
72
105
|
* Alias/forward methods from one object to another. This will be useful for creating
|
73
106
|
'commands' in irb.
|
107
|
+
* Provide an auto_alias method for creators.
|
108
|
+
* Save aliases to file (for console use).
|
74
109
|
* Add more RDoc documentation.
|
data/Rakefile
CHANGED
@@ -24,7 +24,8 @@ begin
|
|
24
24
|
s.summary = s.description
|
25
25
|
s.authors = ["Gabriel Horner"]
|
26
26
|
s.has_rdoc = true
|
27
|
-
s.
|
27
|
+
s.extra_rdoc_files = ["README.rdoc", "LICENSE.txt"]
|
28
|
+
s.files = FileList["Rakefile", "VERSION.yml", "README.rdoc", "LICENSE.txt", "aliases.yml.example", "{lib,test}/**/*"]
|
28
29
|
end
|
29
30
|
|
30
31
|
rescue LoadError
|
data/VERSION.yml
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
#Usage: include Alias::Console wherever you want to use these methods
|
2
|
+
module Alias
|
3
|
+
module Console
|
4
|
+
def self.included(base)
|
5
|
+
base.extend self
|
6
|
+
end
|
7
|
+
|
8
|
+
def create(*args)
|
9
|
+
Alias.manager.create_aliases(*args)
|
10
|
+
end
|
11
|
+
|
12
|
+
#options: type, raw, class, sort
|
13
|
+
#s 'man', :type=>'instance_method'
|
14
|
+
#s /ma/, :raw=>true
|
15
|
+
def search(*args)
|
16
|
+
options = args[-1].is_a?(Hash) ? args[-1].slice_off!(:raw, :sort) : {}
|
17
|
+
if args[0] && ! (args[0].is_a?(Hash) && args[0].empty?)
|
18
|
+
if args[0].is_a?(String) or args[0].is_a?(Regexp)
|
19
|
+
search_hash = {:name=>args[0]}
|
20
|
+
search_hash.merge!(args[1]) if args[1].is_a?(Hash)
|
21
|
+
elsif args[0].is_a?(Hash)
|
22
|
+
search_hash = args[0]
|
23
|
+
end
|
24
|
+
result = Alias.manager.search(search_hash)
|
25
|
+
else
|
26
|
+
result = Alias.manager.list
|
27
|
+
end
|
28
|
+
|
29
|
+
if options[:sort]
|
30
|
+
result = result.sort {|a,b|
|
31
|
+
(a[options[:sort]].nil? || b[options[:sort]].nil?) ? 1 :
|
32
|
+
(a[options[:sort]]) <=> b[options[:sort]]
|
33
|
+
}
|
34
|
+
end
|
35
|
+
if options[:raw]
|
36
|
+
result
|
37
|
+
else
|
38
|
+
format_search(result, options)
|
39
|
+
nil
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def format_search(result, options)
|
44
|
+
body = ''
|
45
|
+
if result.empty?
|
46
|
+
body = "No results"
|
47
|
+
else
|
48
|
+
result.each do |e|
|
49
|
+
h = e.slice_off!(:name, :alias)
|
50
|
+
body += "#{h[:alias]} = #{h[:name]} (" + e.map {|k,v| "#{k}: #{v}"}.join(", ") + ")\n"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
puts body
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/lib/alias/creator.rb
CHANGED
@@ -6,38 +6,45 @@
|
|
6
6
|
module Alias
|
7
7
|
class Creator
|
8
8
|
|
9
|
-
attr_accessor :verbose, :
|
9
|
+
attr_accessor :verbose, :force, :searched_at, :modified_at, :alias_map
|
10
10
|
def initialize(aliases_hash={})
|
11
|
-
|
11
|
+
self.alias_map = aliases_hash
|
12
12
|
@verbose = false
|
13
13
|
@force = false
|
14
14
|
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|
16
|
+
def modified_since_last_search?
|
17
|
+
(@searched_at && @modified_at) ? (@modified_at > @searched_at) : true
|
18
|
+
end
|
19
|
+
|
20
|
+
def alias_map=(value)
|
21
|
+
@modified_at = Time.now
|
22
|
+
@alias_map = value
|
28
23
|
end
|
29
24
|
|
30
|
-
#needs to return generated aliases_hash
|
31
25
|
def auto_create(array_to_alias)
|
32
26
|
aliases_hash = generate_aliases(array_to_alias)
|
33
27
|
create(aliases_hash)
|
34
28
|
aliases_hash
|
35
29
|
end
|
36
30
|
|
31
|
+
# Options are:
|
32
|
+
# * :auto_alias : Array of constants to alias by shortest available constant. For example,
|
33
|
+
# if the constant A already exists, then Aardvark would be aliased to Aa.
|
34
|
+
def manager_create(aliases_hash, options = {})
|
35
|
+
self.verbose = options['verbose'] if options['verbose']
|
36
|
+
self.force = options['force'] if options['force']
|
37
|
+
create(aliases_hash)
|
38
|
+
if options['auto_alias']
|
39
|
+
auto_create(options['auto_alias'])
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
37
43
|
def create(aliases_hash)
|
38
44
|
delete_invalid_aliases(aliases_hash)
|
39
45
|
delete_existing_aliases(aliases_hash) unless self.force
|
40
|
-
|
46
|
+
self.alias_map = alias_map.merge aliases_hash
|
47
|
+
|
41
48
|
#td: create method for efficiently removing constants/methods in any namespace
|
42
49
|
silence_warnings {
|
43
50
|
create_aliases(aliases_hash)
|
@@ -61,6 +68,9 @@ module Alias
|
|
61
68
|
raise "This abstract method must be overridden."
|
62
69
|
end
|
63
70
|
|
71
|
+
#Should be overridden to support search
|
72
|
+
def to_searchable_array; []; end
|
73
|
+
|
64
74
|
def delete_invalid_class_keys(klass_hash)
|
65
75
|
klass_hash.each {|k,v|
|
66
76
|
if Object.any_const_get(k).nil?
|
data/lib/alias/manager.rb
CHANGED
@@ -8,33 +8,75 @@ module Alias
|
|
8
8
|
@force = false
|
9
9
|
end
|
10
10
|
|
11
|
-
attr_accessor :alias_creators, :verbose, :force
|
11
|
+
attr_accessor :alias_creators, :verbose, :force, :indexed_aliases
|
12
12
|
def alias_types; @alias_creators.keys; end
|
13
|
+
def alias_creator_objects; @alias_creators.values; end
|
14
|
+
def alias_map(type)
|
15
|
+
@alias_creators[type] && @alias_creators[type].alias_map
|
16
|
+
end
|
13
17
|
|
14
|
-
def
|
18
|
+
def create_creator(alias_type)
|
15
19
|
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
20
|
if creator_class = Object.any_const_get(creator_class_string)
|
19
|
-
creator_class.
|
21
|
+
creator_class.new
|
20
22
|
else
|
21
23
|
puts "Creator class '#{creator_class_string}' not found." if @verbose
|
22
24
|
nil
|
23
25
|
end
|
26
|
+
|
24
27
|
end
|
25
28
|
|
26
29
|
def create_aliases(alias_type, aliases_hash)
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
+
if (obj = @alias_creators[alias_type.to_sym] ||= create_creator(alias_type.to_s))
|
31
|
+
aliases_hash = aliases_hash.dup
|
32
|
+
create_options = aliases_hash.slice_off!('auto_alias', 'verbose', 'force')
|
33
|
+
create_options['verbose'] = @verbose unless create_options.has_key?('verbose')
|
34
|
+
obj.manager_create(aliases_hash, create_options)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def search(search_hash)
|
39
|
+
result = nil
|
40
|
+
search_hash.each do |k,v|
|
41
|
+
new_result = simple_search(k,v)
|
42
|
+
#AND's searches
|
43
|
+
result = intersection_of_two_arrays(new_result, result)
|
44
|
+
end
|
45
|
+
#duplicate results in case they are modified
|
46
|
+
result = result.map {|e| e.dup} if result
|
47
|
+
alias_creator_objects.each {|e| e.searched_at = Time.now }
|
48
|
+
result
|
49
|
+
end
|
50
|
+
|
51
|
+
def list
|
52
|
+
indexed_aliases.map {|e| e.dup}
|
53
|
+
end
|
54
|
+
|
55
|
+
def simple_search(field, search_term)
|
56
|
+
result = indexed_aliases.select {|e|
|
57
|
+
search_term.is_a?(Regexp) ? e[field] =~ search_term : e[field] == search_term
|
58
|
+
}
|
59
|
+
result
|
60
|
+
end
|
61
|
+
|
62
|
+
def intersection_of_two_arrays(arr1, arr2)
|
63
|
+
arr2.nil? ? arr1 : arr1.select {|e| arr2.include?(e)}
|
64
|
+
end
|
65
|
+
|
66
|
+
def indexed_aliases(reindex=true)
|
67
|
+
reindex ? @indexed_aliases = reindex_aliases(@indexed_aliases) : @indexed_aliases
|
68
|
+
end
|
30
69
|
|
31
|
-
|
32
|
-
|
33
|
-
|
70
|
+
def reindex_aliases(searchable_array=nil)
|
71
|
+
searchable_array ||= []
|
72
|
+
@alias_creators.map do |type, creator|
|
73
|
+
if creator.modified_since_last_search?
|
74
|
+
searchable_array.delete_if {|e| e[:type] == type.to_s}
|
75
|
+
new_arr = creator.to_searchable_array.each {|e| e[:type] = type.to_s}
|
76
|
+
searchable_array += new_arr
|
34
77
|
end
|
35
|
-
self.send("#{accessor_method}=", {}) if self.send(accessor_method).nil?
|
36
|
-
self.send(accessor_method).merge! obj.alias_map
|
37
78
|
end
|
79
|
+
searchable_array
|
38
80
|
end
|
39
81
|
end
|
40
82
|
end
|
@@ -43,6 +43,14 @@ module Alias
|
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
+
def to_searchable_array
|
47
|
+
@alias_map.map {|klass,method_hash|
|
48
|
+
method_hash.map {|k,v|
|
49
|
+
{:class=>klass, :name=>k, :alias=>v}
|
50
|
+
}
|
51
|
+
}.flatten
|
52
|
+
end
|
53
|
+
|
46
54
|
def create_method_aliases_per_class(klass, alias_hash)
|
47
55
|
eval_string = ""
|
48
56
|
alias_hash.each {|original_method, alias_methods|
|
data/lib/alias.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__))
|
2
2
|
require 'yaml'
|
3
|
-
require '
|
3
|
+
require 'config_struct'
|
4
4
|
require 'alias/manager'
|
5
5
|
require 'alias/creator'
|
6
6
|
require 'alias/constant_creator'
|
@@ -8,6 +8,7 @@ require 'alias/method_creator_helper'
|
|
8
8
|
require 'alias/instance_method_creator'
|
9
9
|
require 'alias/class_method_creator'
|
10
10
|
require 'alias/core_extensions'
|
11
|
+
require 'alias/console'
|
11
12
|
|
12
13
|
module Alias
|
13
14
|
extend self
|
@@ -23,18 +24,13 @@ module Alias
|
|
23
24
|
file ? YAML::load(File.read(file)) : {}
|
24
25
|
end
|
25
26
|
|
26
|
-
def init(options={})
|
27
|
+
def init(options={}, &block)
|
27
28
|
config_hash = load_config_file(options[:file])
|
28
29
|
config.merge! config_hash
|
29
30
|
config['verbose'] = options[:verbose] if !options[:verbose].nil?
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
yield(obj)
|
34
|
-
block_config = obj.to_hash.stringify_keys
|
35
|
-
config.merge! block_config
|
36
|
-
end
|
37
|
-
|
32
|
+
block_config = ConfigStruct.block_to_hash(block).stringify_keys
|
33
|
+
config.merge! block_config
|
38
34
|
manager.verbose = config['verbose'] if config.has_key?('verbose')
|
39
35
|
config.each do |k,v|
|
40
36
|
next if ['verbose'].include?(k)
|
@@ -52,4 +48,4 @@ module Alias
|
|
52
48
|
def manager
|
53
49
|
@manager ||= Manager.new
|
54
50
|
end
|
55
|
-
end
|
51
|
+
end
|
data/test/alias_test.rb
CHANGED
@@ -39,9 +39,9 @@ class AliasTest < Test::Unit::TestCase
|
|
39
39
|
|
40
40
|
test "creates manager object and non-empty aliases" do
|
41
41
|
Alias.init :file=>File.join(File.dirname(__FILE__),'aliases.yml')
|
42
|
-
Alias.manager.
|
43
|
-
Alias.manager.
|
44
|
-
Alias.manager.
|
42
|
+
Alias.manager.alias_map(:instance_method)
|
43
|
+
Alias.manager.alias_map(:class_method)
|
44
|
+
Alias.manager.alias_map(:constant)
|
45
45
|
end
|
46
46
|
|
47
47
|
test "with verbose option sets config and manager verbosity" do
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper.rb')
|
2
|
+
|
3
|
+
class Alias::ConstantCreatorTest < 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
|
+
|
28
|
+
test "to_searchable_array is an array of hashes" do
|
29
|
+
@creator.alias_map = {'Alias'=>'A'}
|
30
|
+
@creator.to_searchable_array.should == [{:name=>'Alias', :alias=>'A'}]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/test/creator_test.rb
CHANGED
@@ -1,35 +1,70 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'test_helper.rb')
|
2
2
|
|
3
3
|
class Alias::CreatorTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
|
4
|
+
test "Creator deletes invalid class keys" do
|
5
|
+
h1 = {'Alias::Creator'=>'whoop','Yay'=>'Haha'}
|
6
|
+
@creator = Alias::Creator.new
|
7
|
+
@creator.delete_invalid_class_keys(h1)
|
8
|
+
h1.should == {'Alias::Creator'=>'whoop'}
|
9
|
+
end
|
10
|
+
|
11
|
+
context "Creator" do
|
12
|
+
before(:each) { @creator = Alias::Creator.new}
|
13
|
+
test "calls delete_existing_aliases when no force" do
|
14
|
+
@creator.force = false
|
15
|
+
@creator.expects(:delete_existing_aliases)
|
16
|
+
@creator.expects(:create_aliases)
|
17
|
+
@creator.create({})
|
18
|
+
end
|
19
|
+
|
20
|
+
test "doesn't call delete_existing_aliases when force" do
|
21
|
+
@creator.force = true
|
22
|
+
@creator.expects(:delete_existing_aliases).never
|
23
|
+
@creator.expects(:create_aliases)
|
24
|
+
@creator.create({})
|
25
|
+
end
|
26
|
+
|
27
|
+
test "sets modified_at timestamp when creating aliases" do
|
28
|
+
stub_time = Time.new
|
29
|
+
Time.expects(:now).returns(stub_time)
|
30
|
+
@creator.expects(:create_aliases)
|
31
|
+
@creator.create({})
|
32
|
+
@creator.modified_at.should == stub_time
|
33
|
+
end
|
6
34
|
|
7
|
-
test "
|
8
|
-
|
9
|
-
@creator.
|
10
|
-
|
35
|
+
test "with modified_at > searched_at has been modified_since_last_search?" do
|
36
|
+
some_time = Time.new
|
37
|
+
@creator.modified_at = some_time + 100
|
38
|
+
@creator.searched_at = some_time
|
39
|
+
assert @creator.modified_since_last_search?
|
11
40
|
end
|
12
41
|
|
13
|
-
test "
|
14
|
-
|
15
|
-
@creator.
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
42
|
+
test "with modified_at greater than searched_at has been modified_since_last_search?" do
|
43
|
+
some_time = Time.new
|
44
|
+
@creator.modified_at = some_time + 100
|
45
|
+
@creator.searched_at = some_time
|
46
|
+
assert @creator.modified_since_last_search?
|
47
|
+
end
|
48
|
+
|
49
|
+
test "with modified_at less than searched_at has not been modified_since_last_search?" do
|
50
|
+
some_time = Time.new
|
51
|
+
@creator.modified_at = some_time
|
52
|
+
@creator.searched_at = some_time + 100
|
53
|
+
assert !@creator.modified_since_last_search?
|
54
|
+
end
|
55
|
+
|
56
|
+
test "with no searched_at has been modified_since_last_search?" do
|
57
|
+
@creator.modified_at = Time.new
|
58
|
+
@creator.searched_at = nil
|
59
|
+
assert @creator.modified_since_last_search?
|
60
|
+
end
|
61
|
+
|
62
|
+
test "sets modified_at when calling alias_map=" do
|
63
|
+
stub_time = Time.new
|
64
|
+
Time.expects(:now).returns(stub_time)
|
65
|
+
@creator.alias_map = {'blah'=>'b'}
|
66
|
+
@creator.modified_at.should == stub_time
|
20
67
|
end
|
21
68
|
|
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
69
|
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
70
|
end
|
data/test/manager_test.rb
CHANGED
@@ -4,49 +4,107 @@ class Alias::ManagerTest < Test::Unit::TestCase
|
|
4
4
|
before(:each) { @manager = Alias::Manager.new}
|
5
5
|
|
6
6
|
context "Manager" do
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
20
|
+
test "creates constant aliases" do
|
21
|
+
h1 = {'Time'=>'T', 'auto_alias'=>['Date']}
|
22
|
+
@manager.create_aliases(:constant, h1)
|
23
|
+
@manager.alias_map(:constant).should == {'Time'=>'T', 'Date'=>'D'}
|
24
|
+
end
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
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.alias_map(:instance_method).should == {'SampleClass'=>{:whoop=>:can_of_wass}}
|
35
|
+
SampleClass.new.whoop.should == SampleClass.new.can_of_wass
|
36
|
+
end
|
37
37
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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.alias_map(:class_method)
|
48
|
+
SampleClass.capohow.should == SampleClass.cap
|
49
|
+
end
|
50
|
+
|
51
|
+
context "search" do
|
52
|
+
def setup_search
|
53
|
+
@manager.alias_creators = {:constant=>Alias::ConstantCreator.new}
|
54
|
+
@manager.expects(:indexed_aliases).returns([{:name=>'Array', :alias=>'A'}, {:name=>'Abbrev', :alias=>'Ab'}])
|
55
|
+
end
|
56
|
+
|
57
|
+
test "sets creator's searched_at" do
|
58
|
+
setup_search
|
59
|
+
assert @manager.alias_creators[:constant].searched_at.nil?
|
60
|
+
@manager.search :name=>'blah'
|
61
|
+
assert @manager.alias_creators[:constant].searched_at.is_a?(Time)
|
62
|
+
end
|
63
|
+
|
64
|
+
test "with string returns exact match" do
|
65
|
+
setup_search
|
66
|
+
@manager.search(:name=>'Array').should == [{:name=>'Array', :alias=>'A'}]
|
67
|
+
end
|
68
|
+
|
69
|
+
test "with regex returns multiple matches " do
|
70
|
+
setup_search
|
71
|
+
@manager.search(:name=>/A/).should == [{:name=>'Array', :alias=>'A'}, {:name=>'Abbrev', :alias=>'Ab'}]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context "when indexing search" do
|
76
|
+
def setup_index
|
77
|
+
@creator = Alias::ConstantCreator.new
|
78
|
+
@creator.alias_map = {'Array'=>'A', 'Abbrev'=>'Ab'}
|
79
|
+
@manager.alias_creators = {:constant=>@creator}
|
80
|
+
end
|
81
|
+
|
82
|
+
test "works when first initialized" do
|
83
|
+
setup_index
|
84
|
+
@creator.stubs(:modified_since_last_search?).returns(true)
|
85
|
+
expected_result = [{:type=>"constant", :name=>"Array", :alias=>"A"}, {:type=>"constant", :name=>"Abbrev", :alias=>"Ab"}]
|
86
|
+
assert @manager.indexed_aliases(false).nil?
|
87
|
+
@manager.indexed_aliases.should == expected_result
|
88
|
+
end
|
89
|
+
|
90
|
+
test "with modified creator, deletes old + adds new aliases" do
|
91
|
+
original_aliases = [{:type=>"constant", :name=>"Enumerable", :alias=>"E"}]
|
92
|
+
@manager.indexed_aliases = original_aliases
|
93
|
+
setup_index
|
94
|
+
@creator.stubs(:modified_since_last_search?).returns(true)
|
95
|
+
expected_result = [{:type=>"constant", :name=>"Array", :alias=>"A"}, {:type=>"constant", :name=>"Abbrev", :alias=>"Ab"}]
|
96
|
+
assert @manager.indexed_aliases(false).include?(original_aliases[0])
|
97
|
+
@manager.indexed_aliases.should == expected_result
|
98
|
+
assert !@manager.indexed_aliases.include?(original_aliases[0])
|
99
|
+
end
|
100
|
+
|
101
|
+
test "with unmodified creator, doesn't change any aliases" do
|
102
|
+
setup_index
|
103
|
+
@manager.indexed_aliases = [{:type=>"constant", :name=>"Enumerable", :alias=>"E"}]
|
104
|
+
@creator.stubs(:modified_since_last_search?).returns(false)
|
105
|
+
@manager.indexed_aliases.should == [{:type=>"constant", :name=>"Enumerable", :alias=>"E"}]
|
106
|
+
end
|
107
|
+
end
|
50
108
|
end
|
51
|
-
|
109
|
+
|
52
110
|
end
|
@@ -25,6 +25,11 @@ class Alias::MethodCreatorHelperTest < Test::Unit::TestCase
|
|
25
25
|
@creator.create(h2)
|
26
26
|
assert_equal 'blah', String.n('blah')
|
27
27
|
end
|
28
|
+
|
29
|
+
test "to_searchable_array is an array of hashes" do
|
30
|
+
@creator.alias_map = {'String'=>{'name'=>'n'}}
|
31
|
+
@creator.to_searchable_array.should == [{:name=>'name', :alias=>'n', :class=>'String'}]
|
32
|
+
end
|
28
33
|
end
|
29
34
|
|
30
35
|
context "InstanceMethodCreator" do
|
data/test/test_helper.rb
CHANGED
@@ -4,8 +4,8 @@ require 'context' #gem install jeremymcanally-context -s http://gems.github.com
|
|
4
4
|
require 'mocha' #gem install mocha
|
5
5
|
require 'matchy' #gem install jeremymcanally-stump -s http://gems.github.com
|
6
6
|
require 'pending'
|
7
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__)
|
7
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
8
8
|
require 'alias'
|
9
9
|
|
10
10
|
class Test::Unit::TestCase
|
11
|
-
end
|
11
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cldwalker-alias
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabriel Horner
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-02-23 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -19,15 +19,18 @@ executables: []
|
|
19
19
|
|
20
20
|
extensions: []
|
21
21
|
|
22
|
-
extra_rdoc_files:
|
23
|
-
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
- LICENSE.txt
|
24
25
|
files:
|
25
26
|
- Rakefile
|
27
|
+
- VERSION.yml
|
26
28
|
- README.rdoc
|
27
29
|
- LICENSE.txt
|
28
30
|
- aliases.yml.example
|
29
31
|
- lib/alias
|
30
32
|
- lib/alias/class_method_creator.rb
|
33
|
+
- lib/alias/console.rb
|
31
34
|
- lib/alias/constant_creator.rb
|
32
35
|
- lib/alias/core_extensions.rb
|
33
36
|
- lib/alias/creator.rb
|
@@ -35,8 +38,10 @@ files:
|
|
35
38
|
- lib/alias/manager.rb
|
36
39
|
- lib/alias/method_creator_helper.rb
|
37
40
|
- lib/alias.rb
|
41
|
+
- lib/config_struct.rb
|
38
42
|
- test/alias_test.rb
|
39
43
|
- test/aliases.yml
|
44
|
+
- test/constant_creator_test.rb
|
40
45
|
- test/core_extensions_test.rb
|
41
46
|
- test/creator_test.rb
|
42
47
|
- test/manager_test.rb
|