sugar-high 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/sugar-high/alias.rb +27 -8
- data/spec/sugar-high/alias_spec.rb +26 -10
- data/sugar-high.gemspec +1 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
data/lib/sugar-high/alias.rb
CHANGED
@@ -9,16 +9,35 @@ class Module
|
|
9
9
|
# insert_into_xxx becomes inject_into_xxx and update_xxx
|
10
10
|
# read_xxx becomes xxx_content (overriding default :after action to insert at the X)
|
11
11
|
|
12
|
-
def multi_alias
|
13
|
-
|
12
|
+
def multi_alias *args
|
13
|
+
name = case args.first
|
14
|
+
when Symbol, String
|
15
|
+
args.first.to_s
|
16
|
+
when Hash
|
17
|
+
# default is :after
|
18
|
+
args.first[:_before_] ? :before : :after
|
19
|
+
end
|
20
|
+
|
21
|
+
if name.kind_of? Symbol
|
22
|
+
config_options = name
|
23
|
+
options = args.first
|
24
|
+
name = options[:"_#{name}_"]
|
25
|
+
else
|
26
|
+
options = args[1]
|
27
|
+
end
|
28
|
+
|
29
|
+
raise ArgumentError, "Name of method pattern to alias not specified. Please pass name as either first argument or as :before or :after option" if !name
|
30
|
+
|
31
|
+
options.delete(:_after_)
|
32
|
+
options.delete(:_before_)
|
33
|
+
|
14
34
|
options.each_pair do |original, aliases|
|
15
|
-
|
16
|
-
alias_methods name, original, [aliases].flatten, config_options
|
35
|
+
alias_methods name.to_sym, original, [aliases].flatten, config_options
|
17
36
|
end
|
18
37
|
end
|
19
38
|
|
20
39
|
def alias_methods name, original, aliases, config_options
|
21
|
-
aliases.each do |alias_name|
|
40
|
+
aliases.each do |alias_name|
|
22
41
|
new_alias = make_name(name, alias_name.to_s, config_options)
|
23
42
|
original_name = make_name(name, original.to_s, config_options)
|
24
43
|
begin
|
@@ -32,12 +51,12 @@ class Module
|
|
32
51
|
protected
|
33
52
|
|
34
53
|
def make_name name, alias_name, config_options
|
35
|
-
return alias_name.gsub(/X/, name.to_s) if alias_name =~ /X/
|
54
|
+
return alias_name.gsub(/X/, name.to_s) if alias_name =~ /X/
|
36
55
|
case config_options
|
37
|
-
when :after
|
38
|
-
"#{alias_name}_#{name}"
|
39
56
|
when :before
|
40
57
|
"#{name}_#{alias_name}"
|
58
|
+
else # default
|
59
|
+
"#{alias_name}_#{name}"
|
41
60
|
end
|
42
61
|
end
|
43
62
|
end
|
@@ -9,25 +9,41 @@ require 'spec_helper'
|
|
9
9
|
require 'sugar-high/alias'
|
10
10
|
|
11
11
|
class Abc
|
12
|
+
def hello_kristian
|
13
|
+
'hello'
|
14
|
+
end
|
15
|
+
multi_alias :_after_ => :kristian, :hello => :howdy
|
16
|
+
end
|
17
|
+
|
18
|
+
class Xyz
|
12
19
|
def hello_kristian
|
13
20
|
'hi'
|
14
21
|
end
|
15
|
-
|
22
|
+
|
23
|
+
multi_alias :kristian, :hello => :alloha
|
24
|
+
end
|
25
|
+
|
26
|
+
class Ged
|
27
|
+
def kristian_hello
|
28
|
+
'hejsa'
|
29
|
+
end
|
30
|
+
|
31
|
+
multi_alias :_before_ => :kristian, :hello => :hejsa
|
16
32
|
end
|
17
33
|
|
18
34
|
describe "SugarHigh" do
|
19
|
-
describe "Arguments" do
|
20
|
-
|
21
|
-
@obj = Abc.new
|
22
|
-
end
|
23
|
-
|
24
|
-
context 'Aliased :hello_kristian with :howdy_kristian ' do
|
35
|
+
describe "Arguments" do
|
36
|
+
context 'hould alias :hello_kristian with :howdy_kristian ' do
|
25
37
|
it "should find alias" do
|
26
|
-
|
38
|
+
Abc.new.respond_to?(:howdy_kristian).should be_true
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should find -alloha alias method for kristian" do
|
42
|
+
Xyz.new.respond_to?(:alloha_kristian).should be_true
|
27
43
|
end
|
28
44
|
|
29
|
-
it "should find
|
30
|
-
|
45
|
+
it "should find -hejsa alias method for kristian" do
|
46
|
+
Ged.new.respond_to?(:kristian_hejsa).should be_true
|
31
47
|
end
|
32
48
|
end
|
33
49
|
end
|
data/sugar-high.gemspec
CHANGED