cldwalker-alias 0.1.2 → 0.2.0
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/CHANGELOG.rdoc +16 -0
- data/README.rdoc +56 -77
- data/Rakefile +4 -4
- data/VERSION.yml +2 -2
- data/lib/alias/console.rb +13 -49
- data/lib/alias/creator.rb +102 -81
- data/lib/alias/creators/any_to_instance_method_creator.rb +22 -0
- data/lib/alias/creators/class_method_creator.rb +19 -0
- data/lib/alias/creators/class_to_instance_method_creator.rb +25 -0
- data/lib/alias/creators/constant_creator.rb +11 -0
- data/lib/alias/creators/instance_method_creator.rb +19 -0
- data/lib/alias/manager.rb +107 -49
- data/lib/alias/util.rb +86 -0
- data/lib/alias/validator.rb +94 -0
- data/lib/alias.rb +70 -36
- data/test/alias_test.rb +54 -53
- data/test/aliases.yml +8 -4
- data/test/any_to_instance_method_creator_test.rb +39 -0
- data/test/class_method_creator_test.rb +42 -0
- data/test/class_to_instance_method_creator_test.rb +48 -0
- data/test/console_test.rb +60 -0
- data/test/constant_creator_test.rb +27 -22
- data/test/creator_test.rb +24 -65
- data/test/instance_method_creator_test.rb +41 -0
- data/test/manager_test.rb +92 -97
- data/test/test_helper.rb +23 -3
- data/test/util_test.rb +42 -0
- data/test/validator_test.rb +72 -0
- metadata +40 -24
- data/aliases.yml.example +0 -16
- data/lib/alias/class_method_creator.rb +0 -9
- data/lib/alias/constant_creator.rb +0 -50
- data/lib/alias/core_extensions.rb +0 -46
- data/lib/alias/instance_method_creator.rb +0 -9
- data/lib/alias/method_creator_helper.rb +0 -77
- data/lib/config_struct.rb +0 -17
- data/test/core_extensions_test.rb +0 -34
- data/test/method_creator_helper_test.rb +0 -59
@@ -0,0 +1,72 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper.rb')
|
2
|
+
|
3
|
+
class Alias::ValidatorTest < Test::Unit::TestCase
|
4
|
+
context "Validator" do
|
5
|
+
before(:all) { eval "class ::TestCreator < Alias::Creator; end"}
|
6
|
+
before(:each) { Alias::Validator.instance_eval "@validators = {}"}
|
7
|
+
|
8
|
+
def validate(options={})
|
9
|
+
creator = TestCreator.new
|
10
|
+
options.each {|k,v| creator.send("#{k}=",v)}
|
11
|
+
@validator.validate(creator, {}, :blah)
|
12
|
+
end
|
13
|
+
|
14
|
+
def validator_message
|
15
|
+
@validator.create_message(:blah)
|
16
|
+
end
|
17
|
+
|
18
|
+
def create_validator(options)
|
19
|
+
@validator = TestCreator.valid :num, options
|
20
|
+
end
|
21
|
+
|
22
|
+
def create_parent_validator(key)
|
23
|
+
Alias::Validator.register_validators [{:key=>key, :if=>lambda {|e| 'yo'}, :message=>lambda {|e| 'cool'}}]
|
24
|
+
@parent_validator = Alias::Validator.validators[key]
|
25
|
+
end
|
26
|
+
|
27
|
+
test "copies a validator when using a previous one" do
|
28
|
+
create_parent_validator :num
|
29
|
+
create_validator :if=>:num
|
30
|
+
@parent_validator.validate(TestCreator.new, {}, :blah).should == validate
|
31
|
+
end
|
32
|
+
|
33
|
+
test "inherits a validator's message when using a previous one" do
|
34
|
+
create_parent_validator :num
|
35
|
+
create_validator :if=>:num
|
36
|
+
validator_message.should == 'cool'
|
37
|
+
end
|
38
|
+
|
39
|
+
test "overrides an inherited message with explicit message" do
|
40
|
+
create_parent_validator :num
|
41
|
+
create_validator :if=>:num, :message=>lambda {|e| 'cooler'}
|
42
|
+
validator_message.should == 'cooler'
|
43
|
+
end
|
44
|
+
|
45
|
+
test "sets a default message if an invalid one is given" do
|
46
|
+
create_validator :if=>lambda {|e| 'yo'}, :message=>:blah
|
47
|
+
validator_message.should =~ /Validation failed/
|
48
|
+
end
|
49
|
+
|
50
|
+
test "with :with option sets proc arg" do
|
51
|
+
create_validator :if=>lambda {|e| 'yo'}, :with=>[:a, :b]
|
52
|
+
@validator.validation_proc.expects(:call).with(['a','c'])
|
53
|
+
@validator.validate(TestCreator.new, {:a=>'a', :b=>'c', :c=>'d'}, :c)
|
54
|
+
end
|
55
|
+
|
56
|
+
test "with :unless option negates result and changes message" do
|
57
|
+
create_validator :unless=>lambda {|e| true }, :message=>lambda {|e| "yo doesn't exist"}
|
58
|
+
validate.should == false
|
59
|
+
validator_message.should == 'yo already exists'
|
60
|
+
end
|
61
|
+
|
62
|
+
test "with :optional option can be forced" do
|
63
|
+
create_validator :if=>lambda { false }, :optional=>true
|
64
|
+
validate(:force=>true).should == true
|
65
|
+
end
|
66
|
+
|
67
|
+
test "without :optional option cannot be forced" do
|
68
|
+
create_validator :if=>lambda { false }
|
69
|
+
validate(:force=>true).should == false
|
70
|
+
end
|
71
|
+
end
|
72
|
+
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.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabriel Horner
|
@@ -9,49 +9,54 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-07-07 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
16
|
-
description:
|
16
|
+
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.
|
17
17
|
email: gabriel.horner@gmail.com
|
18
18
|
executables: []
|
19
19
|
|
20
20
|
extensions: []
|
21
21
|
|
22
22
|
extra_rdoc_files:
|
23
|
-
- README.rdoc
|
24
23
|
- LICENSE.txt
|
24
|
+
- README.rdoc
|
25
25
|
files:
|
26
|
+
- CHANGELOG.rdoc
|
27
|
+
- LICENSE.txt
|
28
|
+
- README.rdoc
|
26
29
|
- Rakefile
|
27
30
|
- VERSION.yml
|
28
|
-
-
|
29
|
-
- LICENSE.txt
|
30
|
-
- aliases.yml.example
|
31
|
-
- lib/alias
|
32
|
-
- lib/alias/class_method_creator.rb
|
31
|
+
- lib/alias.rb
|
33
32
|
- lib/alias/console.rb
|
34
|
-
- lib/alias/constant_creator.rb
|
35
|
-
- lib/alias/core_extensions.rb
|
36
33
|
- lib/alias/creator.rb
|
37
|
-
- lib/alias/
|
34
|
+
- lib/alias/creators/any_to_instance_method_creator.rb
|
35
|
+
- lib/alias/creators/class_method_creator.rb
|
36
|
+
- lib/alias/creators/class_to_instance_method_creator.rb
|
37
|
+
- lib/alias/creators/constant_creator.rb
|
38
|
+
- lib/alias/creators/instance_method_creator.rb
|
38
39
|
- lib/alias/manager.rb
|
39
|
-
- lib/alias/
|
40
|
-
- lib/alias.rb
|
41
|
-
- lib/config_struct.rb
|
40
|
+
- lib/alias/util.rb
|
41
|
+
- lib/alias/validator.rb
|
42
42
|
- test/alias_test.rb
|
43
43
|
- test/aliases.yml
|
44
|
+
- test/any_to_instance_method_creator_test.rb
|
45
|
+
- test/class_method_creator_test.rb
|
46
|
+
- test/class_to_instance_method_creator_test.rb
|
47
|
+
- test/console_test.rb
|
44
48
|
- test/constant_creator_test.rb
|
45
|
-
- test/core_extensions_test.rb
|
46
49
|
- test/creator_test.rb
|
50
|
+
- test/instance_method_creator_test.rb
|
47
51
|
- test/manager_test.rb
|
48
|
-
- test/method_creator_helper_test.rb
|
49
52
|
- test/test_helper.rb
|
53
|
+
- test/util_test.rb
|
54
|
+
- test/validator_test.rb
|
50
55
|
has_rdoc: true
|
51
|
-
homepage: http://
|
56
|
+
homepage: http://tagaholic.me/alias/
|
52
57
|
post_install_message:
|
53
|
-
rdoc_options:
|
54
|
-
|
58
|
+
rdoc_options:
|
59
|
+
- --charset=UTF-8
|
55
60
|
require_paths:
|
56
61
|
- lib
|
57
62
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -71,7 +76,18 @@ requirements: []
|
|
71
76
|
rubyforge_project:
|
72
77
|
rubygems_version: 1.2.0
|
73
78
|
signing_key:
|
74
|
-
specification_version:
|
75
|
-
summary:
|
76
|
-
test_files:
|
77
|
-
|
79
|
+
specification_version: 3
|
80
|
+
summary: Creates, manages and saves aliases for class methods, instance methods, constants, delegated methods and more.
|
81
|
+
test_files:
|
82
|
+
- test/alias_test.rb
|
83
|
+
- test/any_to_instance_method_creator_test.rb
|
84
|
+
- test/class_method_creator_test.rb
|
85
|
+
- test/class_to_instance_method_creator_test.rb
|
86
|
+
- test/console_test.rb
|
87
|
+
- test/constant_creator_test.rb
|
88
|
+
- test/creator_test.rb
|
89
|
+
- test/instance_method_creator_test.rb
|
90
|
+
- test/manager_test.rb
|
91
|
+
- test/test_helper.rb
|
92
|
+
- test/util_test.rb
|
93
|
+
- test/validator_test.rb
|
data/aliases.yml.example
DELETED
@@ -1,16 +0,0 @@
|
|
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
|
@@ -1,50 +0,0 @@
|
|
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 to_searchable_array
|
30
|
-
@alias_map.map {|k,v| {:name=>k, :alias=>v}}
|
31
|
-
end
|
32
|
-
|
33
|
-
def make_shortest_aliases(unaliased_strings)
|
34
|
-
shortest_aliases = {}
|
35
|
-
possible_alias = ''
|
36
|
-
unaliased_strings.each {|s|
|
37
|
-
possible_alias = ''
|
38
|
-
s.split('').each { |e|
|
39
|
-
possible_alias += e
|
40
|
-
if ! shortest_aliases.values.include?(possible_alias)
|
41
|
-
shortest_aliases[s] = possible_alias
|
42
|
-
break
|
43
|
-
end
|
44
|
-
}
|
45
|
-
}
|
46
|
-
|
47
|
-
shortest_aliases
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
@@ -1,46 +0,0 @@
|
|
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
|
@@ -1,77 +0,0 @@
|
|
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 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
|
-
|
54
|
-
def create_method_aliases_per_class(klass, alias_hash)
|
55
|
-
eval_string = ""
|
56
|
-
alias_hash.each {|original_method, alias_methods|
|
57
|
-
alias_methods = [alias_methods] unless alias_methods.is_a?(Array)
|
58
|
-
alias_methods.each { |a|
|
59
|
-
eval_string += "alias_method :#{a}, :#{original_method}\n"
|
60
|
-
}
|
61
|
-
}
|
62
|
-
if self.is_a?(ClassMethodCreator)
|
63
|
-
eval_string = "class <<self\n #{eval_string}\nend"
|
64
|
-
end
|
65
|
-
klass.class_eval eval_string
|
66
|
-
end
|
67
|
-
|
68
|
-
def create_method_aliases(aliases)
|
69
|
-
aliases ||= {}
|
70
|
-
aliases.each { |k,alias_hash|
|
71
|
-
if klass = Object.any_const_get(k)
|
72
|
-
create_method_aliases_per_class(klass, alias_hash)
|
73
|
-
end
|
74
|
-
}
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
data/lib/config_struct.rb
DELETED
@@ -1,34 +0,0 @@
|
|
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
|
@@ -1,59 +0,0 @@
|
|
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
|
-
|
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
|
33
|
-
end
|
34
|
-
|
35
|
-
context "InstanceMethodCreator" do
|
36
|
-
before(:each) { @creator = Alias::InstanceMethodCreator.new }
|
37
|
-
|
38
|
-
test "deletes existing instance method aliases" do
|
39
|
-
h1 = {'String'=>{'strip'=>'st', 'chomp'=>'chop'}}
|
40
|
-
@creator.delete_existing_method_aliases(h1)
|
41
|
-
h1.should == {"String"=>{"strip"=>"st"}}
|
42
|
-
end
|
43
|
-
|
44
|
-
test "deletes existing instance method unless it was created by the object" do
|
45
|
-
h1 = {'String'=>{'downcase'=>'d'}}
|
46
|
-
@creator.create(h1)
|
47
|
-
assert_not_equal 'bh', 'blah'.d
|
48
|
-
h2 = {'String'=>{'delete'=>'d'}}
|
49
|
-
@creator.create(h2)
|
50
|
-
assert_equal 'bh', 'blah'.d('la')
|
51
|
-
end
|
52
|
-
|
53
|
-
test "deletes invalid instance method keys" do
|
54
|
-
h1 = {'String'=>{'strip'=>'st'},'Array'=>{'blah', 'bl'}}
|
55
|
-
@creator.delete_invalid_method_keys(h1)
|
56
|
-
h1.should == {"Array"=>{}, "String"=>{"strip"=>"st"}}
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|