cldwalker-alias 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- 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
data/CHANGELOG.rdoc
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
== 0.2.0
|
2
|
+
* Complete refactoring.
|
3
|
+
* New alias types can be created easily with the DSL for Creator classes.
|
4
|
+
* Added two new alias types, any_to_instance_method and class_to_instance_method.
|
5
|
+
* Better tests and docs.
|
6
|
+
|
7
|
+
== 0.1.2
|
8
|
+
* Added search and console functionality.
|
9
|
+
* Added better tests.
|
10
|
+
|
11
|
+
== 0.1.1
|
12
|
+
* Added checking for existing aliases.
|
13
|
+
* Added config block to Alias.init.
|
14
|
+
|
15
|
+
== 0.1.0
|
16
|
+
* Initial release
|
data/README.rdoc
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
== Description
|
2
2
|
|
3
|
-
Creates aliases for class methods, instance methods and
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
Creates aliases for class methods, instance methods, constants, delegated methods and more. Aliases
|
4
|
+
can be easily searched or saved as YAML config files to load later. Custom alias types are easy to
|
5
|
+
create with the DSL Alias provides. Although Alias was created with the irb user in mind, any Ruby
|
6
|
+
console program can hook into Alias for creating configurable aliases.
|
7
7
|
|
8
8
|
== Setup
|
9
9
|
|
@@ -11,99 +11,78 @@ Install the gem with:
|
|
11
11
|
|
12
12
|
sudo gem install cldwalker-alias -s http://gems.github.com
|
13
13
|
|
14
|
-
The
|
14
|
+
The basic setup is simply drop two lines in your .irbrc:
|
15
15
|
|
16
16
|
require 'alias'
|
17
|
-
Alias.
|
17
|
+
Alias.create
|
18
18
|
|
19
|
-
This
|
20
|
-
|
19
|
+
This will assume a file in config/alias.yml or ~/.alias.yml. If you want it somewhere else,
|
20
|
+
pass a :file option to create():
|
21
21
|
|
22
|
-
Alias.
|
22
|
+
Alias.create :file=>"/path/to/my/clandestine_aliases.yml"
|
23
23
|
|
24
|
-
If you'd like to define your aliases without a config file, pass Alias.
|
25
|
-
call methods as if they were top level keys in the config:
|
24
|
+
If you'd like to define your aliases without a config file, pass Alias.create() an :aliases option:
|
26
25
|
|
27
|
-
Alias.
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
end
|
26
|
+
Alias.create :verbose=>true, :aliases=>{
|
27
|
+
:constant=>{'Array' = 'A'},
|
28
|
+
:instance_method=>{'String'=>{'downcase'=>'dc' }, 'Array'=>{'select'=>'s'}}
|
29
|
+
}
|
32
30
|
|
33
|
-
==
|
31
|
+
== Usage
|
34
32
|
|
35
|
-
|
33
|
+
An example within Rails' script/console:
|
36
34
|
|
37
|
-
|
38
|
-
|
35
|
+
bash> script/console
|
36
|
+
>> require 'alias'
|
37
|
+
=> true
|
39
38
|
|
40
|
-
|
41
|
-
|
39
|
+
# Import alias methods
|
40
|
+
>> extend Alias::Console
|
41
|
+
=> main
|
42
42
|
|
43
|
-
|
44
|
-
|
45
|
-
irb>>create :constant, "Alias"=>"A"
|
46
|
-
=>nil
|
43
|
+
# First let's see what ruby code Alias generates to create an alias.
|
44
|
+
>> create_aliases :class_method, {"ActiveRecord::Base"=>{'find'=>'[]'}}, :pretend=>true
|
47
45
|
|
48
|
-
|
49
|
-
=>
|
50
|
-
|
51
|
-
Search:
|
52
|
-
|
53
|
-
# Searches all alias types for an Alias
|
54
|
-
irb>>search :alias=>'A'
|
55
|
-
A = Alias (type: constant)
|
46
|
+
class ::ActiveRecord::Base; class<< self; alias_method :[], :find; end; end
|
47
|
+
=> true
|
56
48
|
|
57
|
-
#
|
58
|
-
|
59
|
-
|
60
|
-
AB = ActiveRecord::Base (type: constant)
|
49
|
+
# Create the above class method alias
|
50
|
+
>> create_aliases :class_method, "ActiveRecord::Base"=>{'find'=>'[]'}
|
51
|
+
=> true
|
61
52
|
|
62
|
-
#
|
63
|
-
|
64
|
-
|
65
|
-
|
53
|
+
# Create the above constant alias
|
54
|
+
>> create_aliases :constant, "ActiveRecord::Base"=>"AB"
|
55
|
+
=> true
|
56
|
+
# Verify that it worked
|
57
|
+
>> AB
|
58
|
+
=> ActiveRecord::Base
|
66
59
|
|
60
|
+
# If we try to create the constant alias again, Alias prevents us and warns us
|
61
|
+
>> create_aliases :constant, "ActiveRecord::Base"=>"AB"
|
62
|
+
Constant 'AB' not created since it already exists
|
63
|
+
=> false
|
64
|
+
# We can force Alias to override a method, class or constant that already exists
|
65
|
+
>> create_aliases :constant, {"ActiveRecord::Base"=>"AB"}, :force=>true
|
66
|
+
=> true
|
67
67
|
|
68
|
-
|
68
|
+
# Create the above instance method alias
|
69
|
+
>> create_aliases :instance_method, "ActiveRecord::Base"=>{"update_attribute"=>'ua'}
|
70
|
+
=> true
|
69
71
|
|
70
|
-
|
72
|
+
# By default aliases are saved to config/alias.yml in rails or ~/.alias.yml if not.
|
73
|
+
>> save_aliases
|
74
|
+
Saved created aliases to config/alias.yml.
|
75
|
+
=> true
|
71
76
|
|
72
|
-
|
77
|
+
== Configuration
|
73
78
|
|
74
|
-
|
75
|
-
|
76
|
-
mapping constants to their aliases.
|
77
|
-
Example: {'Date'=>'D', 'ActiveRecord::Base'=>'AB'}
|
78
|
-
* instance_method : Alias methods for the instances of specified classes. Takes a nested hash with Ruby classes as string keys. Each class should point
|
79
|
-
to a hash of methods, with the original methods mapping to the alias method.
|
80
|
-
Example: {'String'=>{'downcase'=>'dc'}}
|
81
|
-
* class_method : Alias class methods. Takes the same format as instance_method.
|
82
|
-
Example: {'Date'=>{'day_fraction_to_time'=>'dftt'}}
|
79
|
+
For an example config file see test/aliases.yml.
|
80
|
+
For an explanation of the config file format see Alias.config_file.
|
83
81
|
|
84
|
-
==
|
85
|
-
|
86
|
-
You can create your own alias type by creating an Alias::*Creator Class. The * is there for the alias type name.
|
87
|
-
Suppose you want to create a my_instance_method alias type. Simply pass a my_instance_method key
|
88
|
-
with its appropriate aliases hash in the config. Then create your my_instance_method_creator class:
|
82
|
+
== Creating Custom Alias Types
|
89
83
|
|
90
|
-
|
91
|
-
class MyInstanceMethodCreator < Creator
|
92
|
-
def create_aliases(aliases_hash)
|
93
|
-
# My way of aliasing
|
94
|
-
# ...
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
Notice that the class should be a camelized version of the alias type and inherit from Alias::Creator.
|
100
|
-
At a minimum, make a create_aliases() to make custom aliases from the config data.
|
101
|
-
See creator.rb for more methods you can hook into.
|
84
|
+
See Alias::Creator.
|
102
85
|
|
103
86
|
== Todo
|
104
|
-
|
105
|
-
*
|
106
|
-
'commands' in irb.
|
107
|
-
* Provide an auto_alias method for creators.
|
108
|
-
* Save aliases to file (for console use).
|
109
|
-
* Add more RDoc documentation.
|
87
|
+
* Allow loading of select aliases in a file.
|
88
|
+
* Provide a way to autogenerate aliases with a given proc.
|
data/Rakefile
CHANGED
@@ -18,14 +18,14 @@ begin
|
|
18
18
|
require 'jeweler'
|
19
19
|
Jeweler::Tasks.new do |s|
|
20
20
|
s.name = "alias"
|
21
|
-
s.
|
21
|
+
s.summary = "Creates, manages and saves aliases for class methods, instance methods, constants, delegated methods and more."
|
22
|
+
s.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."
|
22
23
|
s.email = "gabriel.horner@gmail.com"
|
23
|
-
s.homepage = "http://
|
24
|
-
s.summary = s.description
|
24
|
+
s.homepage = "http://tagaholic.me/alias/"
|
25
25
|
s.authors = ["Gabriel Horner"]
|
26
26
|
s.has_rdoc = true
|
27
27
|
s.extra_rdoc_files = ["README.rdoc", "LICENSE.txt"]
|
28
|
-
s.files = FileList["Rakefile", "VERSION.yml", "README.rdoc", "
|
28
|
+
s.files = FileList["Rakefile", "VERSION.yml", "README.rdoc", "CHANGELOG.rdoc", "LICENSE.txt", "{lib,test}/**/*"]
|
29
29
|
end
|
30
30
|
|
31
31
|
rescue LoadError
|
data/VERSION.yml
CHANGED
data/lib/alias/console.rb
CHANGED
@@ -1,56 +1,20 @@
|
|
1
|
-
#Usage: include Alias::Console wherever you want to use these methods
|
2
1
|
module Alias
|
2
|
+
# This module contains the main methods to be accessed from a ruby shell i.e. irb. Simply extend Alias::Console in your ruby shell.
|
3
3
|
module Console
|
4
|
-
|
5
|
-
|
4
|
+
# See Alias::Manager.create_aliases for usage.
|
5
|
+
def create_aliases(*args)
|
6
|
+
Alias.manager.console_create_aliases(*args)
|
6
7
|
end
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
|
9
|
+
# Saves aliases to a file. If no file is given, defaults to config/alias.yml if the config directory exists (for Rails).
|
10
|
+
# Otherwise defaults to ~/.alias.yml.
|
11
|
+
def save_aliases(file=nil)
|
12
|
+
Alias.manager.save_aliases(file)
|
10
13
|
end
|
11
|
-
|
12
|
-
#
|
13
|
-
|
14
|
-
|
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
|
14
|
+
|
15
|
+
# Searches aliases with a search term as defined by Alias::Manager.search. If no arguments given, all aliases are listed.
|
16
|
+
def search_aliases(*args)
|
17
|
+
args.empty? ? Alias.manager.all_aliases : Alias.manager.search(*args)
|
41
18
|
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
19
|
end
|
56
20
|
end
|
data/lib/alias/creator.rb
CHANGED
@@ -1,91 +1,112 @@
|
|
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
1
|
module Alias
|
2
|
+
# Namespace for subclasses of Alias::Creator.
|
3
|
+
class Creators; end
|
4
|
+
# This is the base creator class. To be a valid subclass, the creator must define Alias::Creator.map and Alias::Creator.generate.
|
5
|
+
# Although not required, creators should enforce validation of their aliases with Alias::Creator.valid. Also, the creator should
|
6
|
+
# be named in the format Alias::Creators::*Creator where the asterisk stands for any unique string. Since that string is converted
|
7
|
+
# to an underscored version when referenced in the console, it's recommended to make it camel case. For example,
|
8
|
+
# Alias::Creators::InstanceMethodCreator is referenced by it's underscored version :instance_method.
|
9
|
+
#
|
10
|
+
# To better understand how a creator works, here's the steps a creator goes through when creating aliases:
|
11
|
+
# * map() : Maps the hash from a config file or console input into an array of alias hashes.
|
12
|
+
# * valid() : Defines a validation that each alias hash must pass.
|
13
|
+
# * generate() : Given the array of alias hashes, generates the string of ruby code to be evaled for alias creation.
|
7
14
|
class Creator
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
15
|
+
class AbstractMethodError < StandardError; end
|
16
|
+
class FailedAliasCreationError < StandardError; end
|
17
|
+
class<<self
|
18
|
+
# Creates a validation expectation for the creator by giving it an Alias::Validator object aka validator.
|
19
|
+
# This method must be given an :if or :unless option. If the :if option returns false or :unless option
|
20
|
+
# returns true for an alias, the alias is skipped.
|
21
|
+
# ==== Options:
|
22
|
+
# [:if] Takes a proc or a symbol referencing a registered validator. This proc must evaluate to true
|
23
|
+
# for the validation to pass. See Alias::Validator.validate for what arguments this proc receives by default. See
|
24
|
+
# Alias::Validator.default_validators for validators that can be referenced by symbol.
|
25
|
+
# [:unless] Same as :if option but the result is negated.
|
26
|
+
# [:message] A proc to print a message if the creator's verbose flag is set. Receives same arguments as :if and :unless procs.
|
27
|
+
# If a previous validator is referenced in :unless or :if, then their :message is inherited.
|
28
|
+
# [:with] An array of alias attributes/keys which specify the current alias attributes to pass to the validator procs.
|
29
|
+
# Overrides default argument a validator proc receives.
|
30
|
+
# [:optional] When set to true, this option can be overridden in conjunction with a creator's force flag. Default is false.
|
31
|
+
def valid(key, options={})
|
32
|
+
begin
|
33
|
+
validators[key] = Validator.new(options.merge(:key=>key, :creator=>self))
|
34
|
+
rescue Validator::MissingConditionError
|
35
|
+
raise ArgumentError, "A :unless or :if option is required."
|
36
|
+
rescue Validator::InvalidValidatorError
|
37
|
+
$stderr.puts "Validator not set for #{key}"
|
38
|
+
@validators.delete(key)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Stores validators per alias attribute/key.
|
43
|
+
def validators #:nodoc:
|
44
|
+
@validators ||= {}
|
45
|
+
end
|
46
|
+
|
47
|
+
def maps_config(config) #:nodoc:
|
48
|
+
@map ? @map.call(config) : raise(AbstractMethodError, "No map() defined for #{self}")
|
49
|
+
end
|
50
|
+
|
51
|
+
# Takes a block which converts the creator's config to an array of aliases.
|
52
|
+
def map(&block)
|
53
|
+
@map = block
|
54
|
+
end
|
55
|
+
|
56
|
+
def generates_aliases(aliases) #:nodoc:
|
57
|
+
@generate ? @generate.call(aliases) : raise(AbstractMethodError, "No generate() defined for #{self}")
|
58
|
+
end
|
59
|
+
|
60
|
+
# Takes a block which converts aliases to a string of ruby code to run through Kernel#eval.
|
61
|
+
def generate(&block)
|
62
|
+
@generate = block
|
63
|
+
end
|
64
|
+
|
65
|
+
def class_or_module(klass) #:nodoc:
|
66
|
+
Util.any_const_get(klass).is_a?(Class) ? 'class' : 'module'
|
67
|
+
end
|
68
|
+
|
69
|
+
def inherited(subclass) #:nodoc:
|
70
|
+
@creators ||= []
|
71
|
+
@creators << subclass
|
72
|
+
end
|
73
|
+
|
74
|
+
# Array of all Creator subclasses.
|
75
|
+
def creators; @creators; end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Same purpose as Alias::Manager.verbose and Alias::Manager.force but unlike them these only take a boolean.
|
79
|
+
attr_accessor :verbose, :force
|
80
|
+
# Array of alias hashes that have been created.
|
81
|
+
attr_accessor :aliases
|
82
|
+
|
83
|
+
def initialize(options={}) #:nodoc:
|
12
84
|
@verbose = false
|
13
85
|
@force = false
|
86
|
+
@aliases = []
|
14
87
|
end
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
create(aliases_hash)
|
28
|
-
aliases_hash
|
29
|
-
end
|
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'])
|
88
|
+
|
89
|
+
# Main method used to create aliases. Handles mapping, validation and creation of aliases.
|
90
|
+
def create(aliases_hash, pretend=false)
|
91
|
+
aliases_array = self.class.maps_config(aliases_hash)
|
92
|
+
delete_invalid_aliases(aliases_array)
|
93
|
+
self.aliases = aliases + aliases_array unless pretend
|
94
|
+
begin
|
95
|
+
#td: create method for efficiently removing constants/methods in any namespace
|
96
|
+
eval_string = Util.silence_warnings { self.class.generates_aliases(aliases_array) }
|
97
|
+
pretend ? puts("\n", eval_string) : Kernel.eval(eval_string)
|
98
|
+
rescue
|
99
|
+
raise FailedAliasCreationError, $!
|
40
100
|
end
|
41
101
|
end
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
silence_warnings {
|
50
|
-
create_aliases(aliases_hash)
|
51
|
-
}
|
52
|
-
end
|
53
|
-
|
54
|
-
# Should be overridden to delete aliases that point to invalid/nonexistent classes, methods ...
|
55
|
-
def delete_invalid_aliases(aliases_hash); end
|
56
|
-
|
57
|
-
# Should be overridden to delete aliases that already exist. This method can be bypassed by passing
|
58
|
-
# a force option to the creator.
|
59
|
-
def delete_existing_aliases(aliases_hash); end
|
60
|
-
|
61
|
-
# Must be overridden to use create()
|
62
|
-
def create_aliases(aliases_hash);
|
63
|
-
raise "This abstract method must be overridden."
|
64
|
-
end
|
65
|
-
|
66
|
-
# Must be overridden to use auto_create()
|
67
|
-
def generate_aliases(array_to_alias);
|
68
|
-
raise "This abstract method must be overridden."
|
69
|
-
end
|
70
|
-
|
71
|
-
#Should be overridden to support search
|
72
|
-
def to_searchable_array; []; end
|
73
|
-
|
74
|
-
def delete_invalid_class_keys(klass_hash)
|
75
|
-
klass_hash.each {|k,v|
|
76
|
-
if Object.any_const_get(k).nil?
|
77
|
-
puts "deleted nonexistent klass #{k} #{caller[2].split(/:/)[2]}" if self.verbose
|
78
|
-
klass_hash.delete(k)
|
79
|
-
end
|
102
|
+
|
103
|
+
# Deletes invalid alias hashes that fail defined validators for a creator.
|
104
|
+
def delete_invalid_aliases(arr)
|
105
|
+
arr.delete_if {|alias_hash|
|
106
|
+
!self.class.validators.all? {|attribute, validator|
|
107
|
+
validator.validate(self, alias_hash, attribute)
|
108
|
+
}
|
80
109
|
}
|
81
110
|
end
|
82
|
-
|
83
|
-
private
|
84
|
-
def silence_warnings
|
85
|
-
old_verbose, $VERBOSE = $VERBOSE, nil
|
86
|
-
yield
|
87
|
-
ensure
|
88
|
-
$VERBOSE = old_verbose
|
89
|
-
end
|
90
111
|
end
|
91
|
-
end
|
112
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# Creates instance methods which can call any string of ruby code which ends in a method. This class provides the same
|
2
|
+
# functionality that Alias::Creators::ClassToInstanceMethodCreator provides and more but at the cost of less validation.
|
3
|
+
# Expects a hash of classes/modules of the instance method mapped to a hash of ruby code strings and the instance method names.
|
4
|
+
# For example, the hash {"MyDate"=>{'Date.today.to_s.gsub'=>'t'}} creates a MyDate.t method which directly calls Date.today.to_s.gsub.
|
5
|
+
class Alias::Creators::AnyToInstanceMethodCreator < Alias::Creator
|
6
|
+
map do |config|
|
7
|
+
config.inject([]) {|t,(klass,hash)|
|
8
|
+
t += hash.map {|k,v|
|
9
|
+
{:class=>klass, :any_method=>k, :alias=>v}
|
10
|
+
}
|
11
|
+
}
|
12
|
+
end
|
13
|
+
|
14
|
+
valid :class, :if=>:class
|
15
|
+
valid :alias, :unless=>:instance_method, :with=>[:class, :alias], :optional=>true
|
16
|
+
|
17
|
+
generate do |aliases|
|
18
|
+
aliases.map {|e|
|
19
|
+
%[#{class_or_module(e[:class])} ::#{e[:class]}; def #{e[:alias]}(*args, &block); #{e[:any_method]}(*args, &block); end; end]
|
20
|
+
}.join("\n")
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# Creates aliases of class methods. Expects a hash of classes/modules mapped to a hash of class methods and their aliases
|
2
|
+
# i.e. {'Date'=>{'today'=>'t'}}.
|
3
|
+
class Alias::Creators::ClassMethodCreator < Alias::Creator
|
4
|
+
map do |config|
|
5
|
+
config.inject([]) {|t,(klass,aliases)|
|
6
|
+
t += aliases.map {|k,v| {:class=>klass, :name=>k, :alias=>v} }
|
7
|
+
}
|
8
|
+
end
|
9
|
+
|
10
|
+
valid :class, :if=>:class
|
11
|
+
valid :class_method, :if=>:class_method, :with=>[:class, :name]
|
12
|
+
valid :alias, :unless=>:class_method, :with=>[:class, :alias], :optional=>true
|
13
|
+
|
14
|
+
generate do |aliases|
|
15
|
+
aliases.map {|e|
|
16
|
+
"#{class_or_module(e[:class])} ::#{e[:class]}; class<<self; alias_method :#{e[:alias]}, :#{e[:name]}; end; end"
|
17
|
+
}.join("\n")
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# Creates instance methods which call class methods. These are delegations rather than aliases.
|
2
|
+
# Expects a hash of classes/modules of the instance method mapped
|
3
|
+
# to a hash of the class methods and the instance method names.
|
4
|
+
# For example, the hash {"MyDate"=>{'Date.today'=>'t'}} would create a MyDate.t instance method
|
5
|
+
# which directly calls Date.today.
|
6
|
+
class Alias::Creators::ClassToInstanceMethodCreator < Alias::Creator
|
7
|
+
map do |config|
|
8
|
+
config.inject([]) {|t,(klass,hash)|
|
9
|
+
t += hash.map {|k,v|
|
10
|
+
{:class=>klass, :to_class=>k.split('.')[0], :name=>k.split('.')[1], :alias=>v}
|
11
|
+
}
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
valid :class, :if=>:class
|
16
|
+
valid :to_class, :if=>:class
|
17
|
+
valid :alias, :unless=>:instance_method, :with=>[:class, :alias], :optional=>true
|
18
|
+
valid :to_method, :if=>:class_method, :with=>[:to_class, :name]
|
19
|
+
|
20
|
+
generate do |aliases|
|
21
|
+
aliases.map {|e|
|
22
|
+
%[#{class_or_module(e[:class])} ::#{e[:class]}; def #{e[:alias]}(*args, &block); #{e[:to_class]}.__send__(:#{e[:name]}, *args, &block); end; end]
|
23
|
+
}.join("\n")
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# Creates constants of aliases expecting a hash of existing constants mapped to their aliases.
|
2
|
+
class Alias::Creators::ConstantCreator < Alias::Creator
|
3
|
+
map {|config| config.map {|k,v| {:name=>k, :alias=>v}} }
|
4
|
+
|
5
|
+
valid :alias, :unless=>:constant, :optional=>true
|
6
|
+
valid :name, :if=>:constant
|
7
|
+
|
8
|
+
generate do |aliases|
|
9
|
+
aliases.map {|e| "::#{e[:alias]} = ::#{e[:name]}"}.join("\n")
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# Creates aliases of instance methods. Expects a hash of classes/modules mapped to a hash of instance methods and their aliases
|
2
|
+
# i.e. {"String"=>{'to_s'=>'s'}}.
|
3
|
+
class Alias::Creators::InstanceMethodCreator < Alias::Creator
|
4
|
+
map do |config|
|
5
|
+
config.inject([]) {|t,(klass,aliases)|
|
6
|
+
t += aliases.map {|k,v| {:class=>klass, :name=>k, :alias=>v} }
|
7
|
+
}
|
8
|
+
end
|
9
|
+
|
10
|
+
valid :class, :if=>:class
|
11
|
+
valid :instance_method, :if=>:instance_method, :with=>[:class, :name]
|
12
|
+
valid :alias, :unless=>:instance_method, :with=>[:class, :alias], :optional=>true
|
13
|
+
|
14
|
+
generate do |aliases|
|
15
|
+
aliases.map {|e|
|
16
|
+
"#{class_or_module(e[:class])} ::#{e[:class]}; alias_method :#{e[:alias]}, :#{e[:name]}; end"
|
17
|
+
}.join("\n")
|
18
|
+
end
|
19
|
+
end
|