rogerdpack-arguments 0.4.7.4 → 0.4.7.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/arguments/class.rb +12 -2
- data/lib/arguments/vm.rb +1 -1
- data/spec/arguments_spec.rb +15 -0
- data/spec/klass.rb +12 -0
- metadata +1 -1
data/lib/arguments/class.rb
CHANGED
@@ -3,7 +3,16 @@ module NamedArgs
|
|
3
3
|
methods = instance_methods - Object.methods if methods.empty?
|
4
4
|
|
5
5
|
methods.each do |meth|
|
6
|
-
|
6
|
+
if(meth.to_s.include?('.'))
|
7
|
+
# they may have passed in a parameter like :klass.method for a class method
|
8
|
+
klass, meth = meth.to_s.split('.')
|
9
|
+
klass = eval(klass) # from "Klass" to Klass
|
10
|
+
klass = (class << klass; self; end)
|
11
|
+
names = Arguments.names klass, meth
|
12
|
+
else
|
13
|
+
names = Arguments.names self, meth
|
14
|
+
klass = self
|
15
|
+
end
|
7
16
|
next if names.empty? || names.any?{|name| name[0] == :"*args"}
|
8
17
|
|
9
18
|
assigns = []
|
@@ -30,7 +39,7 @@ module NamedArgs
|
|
30
39
|
end
|
31
40
|
end
|
32
41
|
|
33
|
-
|
42
|
+
klass.module_eval <<-RUBY_EVAL, __FILE__, __LINE__
|
34
43
|
def __new_#{ meth } *args, &block
|
35
44
|
opts = args.last.kind_of?( Hash ) ? args.pop : {}
|
36
45
|
#{ assigns.join("\n") }
|
@@ -42,6 +51,7 @@ module NamedArgs
|
|
42
51
|
RUBY_EVAL
|
43
52
|
end
|
44
53
|
end
|
54
|
+
|
45
55
|
alias :named_args_for :named_arguments_for
|
46
56
|
alias :named_args :named_arguments_for
|
47
57
|
|
data/lib/arguments/vm.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
|
2
2
|
module Arguments
|
3
3
|
def self.names klass, method
|
4
|
-
source, line = klass.instance_method( method ).source_location
|
4
|
+
source, line = klass.instance_method( method ).source_location rescue klass.method( method ).source_location
|
5
5
|
return [] unless source and line
|
6
6
|
str = IO.readlines(source)[ line - 1 ]
|
7
7
|
return [] if str.match(/\*\w+/)
|
data/spec/arguments_spec.rb
CHANGED
@@ -68,6 +68,21 @@ describe Arguments do
|
|
68
68
|
Klass.asr(0, 1, 2, :curve => 3).should == [0,1,2,3]
|
69
69
|
end
|
70
70
|
|
71
|
+
it "should work with class methods" do
|
72
|
+
Klass.send( :named_arguments_for, :"Klass.class_method")
|
73
|
+
Klass.class_method(:a => 1).should == 1
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should work with class methods called like :self.method_name" do
|
77
|
+
Klass.send( :named_arguments_for, :"self.class_method2")
|
78
|
+
Klass.class_method2(:a => 1).should == 1
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should work with class methods passed in by string instead of symboL" do
|
82
|
+
Klass.send( :named_arguments_for, "self.class_method3")
|
83
|
+
Klass.class_method3(:a => 1).should == 1
|
84
|
+
end
|
85
|
+
|
71
86
|
it "should not patch methods that accept no args" do
|
72
87
|
# Arguments.should_not_receive(:names)
|
73
88
|
Klass.send( :named_arguments_for, :no_args )
|
data/spec/klass.rb
CHANGED