rdp-arguments 0.6.1 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -0
- data/lib/arguments.rb +9 -4
- data/lib/arguments/class.rb +11 -2
- data/lib/arguments/mri.rb +2 -1
- data/lib/arguments/vm.rb +9 -2
- data/spec/arguments_spec.rb +49 -6
- data/spec/klass.rb +18 -0
- metadata +3 -4
- data/arguments.gemspec +0 -42
data/Rakefile
CHANGED
@@ -11,6 +11,7 @@ Hoe.plugin :newgem
|
|
11
11
|
# Generate all the Rake tasks
|
12
12
|
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
13
13
|
$hoe = Hoe.spec 'rdp-arguments' do
|
14
|
+
raise 'broken with 1.3.5 on doze'
|
14
15
|
self.developer 'Macario Ortega', 'macarui@gmail.com'
|
15
16
|
self.rubyforge_name = self.name # TODO this is default value
|
16
17
|
self.extra_deps = [
|
data/lib/arguments.rb
CHANGED
@@ -9,12 +9,17 @@ RUBY_VERSION.to_f >= 1.9 ? require( 'arguments/vm' ) : require( 'arguments/mri'
|
|
9
9
|
module Arguments
|
10
10
|
VERSION = '0.6.1'
|
11
11
|
|
12
|
-
def self.names klass, method
|
13
|
-
args = ast_for_method(klass, method).assoc(:args)
|
12
|
+
def self.names klass, method, am_self
|
13
|
+
args = ast_for_method(klass, method, am_self).assoc(:args)
|
14
14
|
args = args[1..-1]
|
15
15
|
|
16
|
-
return [] if args.empty
|
17
|
-
|
16
|
+
return [] if args.empty?# or args.last.is_a?(Symbol)
|
17
|
+
if args.last.is_a?(Symbol)
|
18
|
+
# no optionals, so don't pop them off the list
|
19
|
+
vals = []
|
20
|
+
else
|
21
|
+
vals = args.pop[1..-1]
|
22
|
+
end
|
18
23
|
|
19
24
|
args.collect do |arg|
|
20
25
|
if val = vals.find{ |v| v[1] == arg }
|
data/lib/arguments/class.rb
CHANGED
@@ -12,7 +12,7 @@ module Arguments
|
|
12
12
|
else
|
13
13
|
klass = self
|
14
14
|
end
|
15
|
-
names
|
15
|
+
names = Arguments.names klass, meth, am_self
|
16
16
|
next if names.empty? or names.inject(false) { |bol, pair| bol || /^\*/ === pair.first.to_s }
|
17
17
|
assigns = []
|
18
18
|
names.pop if /^&/ === names[-1][0].to_s
|
@@ -38,7 +38,6 @@ module Arguments
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
-
|
42
41
|
it = <<-RUBY_EVAL, __FILE__, __LINE__
|
43
42
|
#{ "class << self" if am_self }
|
44
43
|
def __#{ meth }_with_keyword_arguments *args, &block
|
@@ -54,6 +53,16 @@ module Arguments
|
|
54
53
|
alias #{ meth } __#{ meth }_with_keyword_arguments
|
55
54
|
#{ "end" if am_self }
|
56
55
|
RUBY_EVAL
|
56
|
+
if $DEBUG
|
57
|
+
code, file, line = *it
|
58
|
+
file = "cached_methods/#{meth}"
|
59
|
+
line = 1
|
60
|
+
Dir.mkdir 'cached_methods' rescue nil
|
61
|
+
File.open(file, 'w') do |f|
|
62
|
+
f.write code
|
63
|
+
end
|
64
|
+
it = [code, file, 1]
|
65
|
+
end
|
57
66
|
original_klass.class_eval *it
|
58
67
|
end
|
59
68
|
end
|
data/lib/arguments/mri.rb
CHANGED
@@ -2,7 +2,8 @@ gem 'ParseTree', '>= 3.0.3'
|
|
2
2
|
require 'parse_tree'
|
3
3
|
|
4
4
|
module Arguments
|
5
|
-
def self.ast_for_method klass, method
|
5
|
+
def self.ast_for_method klass, method, am_self
|
6
|
+
# don't care about am_self for 1.8.x
|
6
7
|
ParseTree.translate( klass, method ).assoc(:scope).assoc(:block)
|
7
8
|
end
|
8
9
|
end
|
data/lib/arguments/vm.rb
CHANGED
@@ -12,10 +12,17 @@ module Arguments
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
-
def self.ast_for_method klass, method
|
15
|
+
def self.ast_for_method klass, method, am_self
|
16
16
|
source, line = klass.instance_method(method).source_location
|
17
17
|
str = IO.readlines( source )[ (line-1)..-1 ].join
|
18
|
+
|
18
19
|
ast = PermissiveRubyParser.new.parse( str )
|
19
|
-
|
20
|
+
if ast
|
21
|
+
if am_self
|
22
|
+
return (ast.assoc( :defs ) or ast)
|
23
|
+
else
|
24
|
+
return (ast.assoc( :defn ) or ast)
|
25
|
+
end
|
26
|
+
end
|
20
27
|
end
|
21
28
|
end
|
data/spec/arguments_spec.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
-
require
|
1
|
+
require 'rubygems'
|
2
2
|
require 'benchmark'
|
3
|
+
require "#{ dir = File.dirname __FILE__ }/../lib/arguments"
|
4
|
+
require 'spec/autorun'
|
3
5
|
|
4
6
|
# TODO: Refactor specs for clarity and better coverage
|
5
7
|
describe Arguments do
|
@@ -7,6 +9,7 @@ describe Arguments do
|
|
7
9
|
before do
|
8
10
|
Object.send(:remove_const, 'Klass') rescue nil
|
9
11
|
load "#{ dir }/klass.rb"
|
12
|
+
load "#{ dir }/klass_big.rb"
|
10
13
|
@instance = Klass.new
|
11
14
|
end
|
12
15
|
|
@@ -48,10 +51,41 @@ describe Arguments do
|
|
48
51
|
|
49
52
|
it "should allow for class arguments in class methods" do
|
50
53
|
Klass.send( :named_arguments_for, :'self.klass_defaults_with_class')
|
51
|
-
Klass.klass_defaults_with_class(1,
|
52
|
-
Klass.klass_defaults_with_class(:a =>
|
54
|
+
Klass.klass_defaults_with_class(1, 4).should == 4
|
55
|
+
Klass.klass_defaults_with_class(:a => 4).should == 4
|
53
56
|
Klass.klass_defaults_with_class().should == 3
|
54
57
|
end
|
58
|
+
|
59
|
+
it "should allow for class arguments in class methods" do
|
60
|
+
Klass.send( :named_arguments_for, :'self.klass_method')
|
61
|
+
Klass.klass_method(1, 2).should == 3
|
62
|
+
Klass.klass_method(:a => 3).should == 3
|
63
|
+
Klass.klass_method(1,2,3,5).should == 5
|
64
|
+
Klass.klass_method(1,2,3,5, 6).should == 5
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should allow for method within a class' self block to be used with a class name" do
|
68
|
+
Klass.send(:named_arguments_for, :'self.klass_method3')
|
69
|
+
Klass.klass_method3(1,4).should == 4
|
70
|
+
Klass.klass_method3(:a => 1, :b => 4).should == 4
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should parse larger methods" do
|
74
|
+
Klass.send( :named_arguments_for, :'self.startCSWithP2PEM')
|
75
|
+
Klass.startCSWithP2PEM 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n' # 3
|
76
|
+
|
77
|
+
Klass.startCSWithP2PEM 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', :dhtClassToUse => 44, :completion_proc => nil # 44
|
78
|
+
|
79
|
+
Klass.startCSWithP2PEM 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', :dhtClassToUse => 44, :completion_proc => nil, :use_this_shared_logger => nil, :do_not_shutdown_logger => false, :termination_proc => nil # 44
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should allow for class arguments in class methods defined with like Name.method" do
|
84
|
+
Klass.send( :named_arguments_for, :'self.klass_defaults_with_class2')
|
85
|
+
Klass.klass_defaults_with_class2(1, 3).should == 3
|
86
|
+
Klass.klass_defaults_with_class2(:a => 3).should == 3
|
87
|
+
Klass.klass_defaults_with_class2().should == 3
|
88
|
+
end
|
55
89
|
|
56
90
|
it "should allow overriding with nil" do
|
57
91
|
Klass.send( :named_arguments_for, :two )
|
@@ -109,7 +143,7 @@ describe Arguments do
|
|
109
143
|
rescue ArgumentError => e
|
110
144
|
e
|
111
145
|
end
|
112
|
-
error.to_s.should == "`four, five` are not recognized argument keywords"
|
146
|
+
error.to_s.should == "`four, five` are not recognized argument keywords" rescue error.to_s.should == "`five, four` are not recognized argument keywords"
|
113
147
|
end
|
114
148
|
|
115
149
|
it "should not patch methods that accept no args" do
|
@@ -133,11 +167,18 @@ describe Arguments do
|
|
133
167
|
@instance.splatted4(1, :b => 2, :args => 1).should == [1, {:b => 2, :args => 1}, []]
|
134
168
|
end
|
135
169
|
|
136
|
-
it "should
|
170
|
+
it "should patch methods with no optionals" do
|
137
171
|
Klass.send( :named_arguments_for, :no_opts )
|
138
|
-
@instance.method(:no_opts).arity.should ==
|
172
|
+
@instance.method(:no_opts).arity.should == -1
|
139
173
|
end
|
140
174
|
|
175
|
+
it "should handle named blocks" do
|
176
|
+
@instance.with_block3(3, 3).should==3
|
177
|
+
Klass.send(:named_args, :with_block3)
|
178
|
+
@instance.with_block3(3, nil, 4).should==4
|
179
|
+
@instance.with_block3(3).should==3
|
180
|
+
end
|
181
|
+
|
141
182
|
it "should patch all methods" do
|
142
183
|
Klass.send( :named_args )
|
143
184
|
@instance.two(1, :three => 3).should == [1, 2, 3]
|
@@ -168,4 +209,6 @@ describe Arguments do
|
|
168
209
|
end
|
169
210
|
}
|
170
211
|
end
|
212
|
+
|
213
|
+
|
171
214
|
end
|
data/spec/klass.rb
CHANGED
@@ -27,6 +27,24 @@ class Klass
|
|
27
27
|
def self.klass_defaults_with_class b = 1, a = @@go
|
28
28
|
a
|
29
29
|
end
|
30
|
+
|
31
|
+
def Klass.klass_defaults_with_class2 b = 1, a = @@go
|
32
|
+
a
|
33
|
+
end
|
34
|
+
|
35
|
+
def Klass.klass_method a = 1, b = 2, c = 3, d = @@go, e = nil
|
36
|
+
d
|
37
|
+
end
|
38
|
+
|
39
|
+
class << self
|
40
|
+
def klass_method3 a, b = 2
|
41
|
+
b
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def with_block3( value, round_id = nil, this_many_repeats_left = 3, &block)
|
46
|
+
this_many_repeats_left
|
47
|
+
end
|
30
48
|
|
31
49
|
def no_args
|
32
50
|
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rdp-arguments
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
- Macario Ortega
|
7
|
+
- Macario Ortega, rogerdpack
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-11-12 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -69,7 +69,6 @@ files:
|
|
69
69
|
- Manifest.txt
|
70
70
|
- README.rdoc
|
71
71
|
- Rakefile
|
72
|
-
- arguments.gemspec
|
73
72
|
- lib/arguments.rb
|
74
73
|
- lib/arguments/class.rb
|
75
74
|
- lib/arguments/mri.rb
|
data/arguments.gemspec
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
Gem::Specification.new do |s|
|
4
|
-
s.name = %q{arguments}
|
5
|
-
s.version = "0.6.1"
|
6
|
-
|
7
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
-
s.authors = ["Macario Ortega"]
|
9
|
-
s.date = %q{2009-10-29}
|
10
|
-
s.description = %q{You don't have to wait until Ruby 2.0 to get (named|keyword) arguments support.
|
11
|
-
Arguments has been tested with Ruby 1.8.6 and ruby 1.9.1 and eventually will work with JRuby (if someone is interested in contributing, I guess is possible since merb-action-args works with JRuby)}
|
12
|
-
s.email = ["macarui@gmail.com"]
|
13
|
-
s.extra_rdoc_files = ["History.txt", "Manifest.txt"]
|
14
|
-
s.files = ["History.txt", "Manifest.txt", "README.rdoc", "Rakefile", "arguments.gemspec", "lib/arguments.rb", "lib/arguments/class.rb", "lib/arguments/mri.rb", "lib/arguments/vm.rb", "spec/arguments_spec.rb", "spec/klass.rb", "spec/module.rb"]
|
15
|
-
s.rdoc_options = ["--main", "README.rdoc"]
|
16
|
-
s.require_paths = ["lib"]
|
17
|
-
s.rubyforge_project = %q{arguments}
|
18
|
-
s.rubygems_version = %q{1.3.5}
|
19
|
-
s.summary = %q{You don't have to wait until Ruby 2.0 to get (named|keyword) arguments support}
|
20
|
-
|
21
|
-
if s.respond_to? :specification_version then
|
22
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
-
s.specification_version = 3
|
24
|
-
|
25
|
-
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
26
|
-
s.add_runtime_dependency(%q<ruby_parser>, [">= 2.0.2"])
|
27
|
-
s.add_runtime_dependency(%q<ParseTree>, [">= 3.0.3"])
|
28
|
-
s.add_runtime_dependency(%q<ruby2ruby>, ["= 1.1.9"])
|
29
|
-
s.add_development_dependency(%q<hoe>, [">= 2.3.3"])
|
30
|
-
else
|
31
|
-
s.add_dependency(%q<ruby_parser>, [">= 2.0.2"])
|
32
|
-
s.add_dependency(%q<ParseTree>, [">= 3.0.3"])
|
33
|
-
s.add_dependency(%q<ruby2ruby>, ["= 1.1.9"])
|
34
|
-
s.add_dependency(%q<hoe>, [">= 2.3.3"])
|
35
|
-
end
|
36
|
-
else
|
37
|
-
s.add_dependency(%q<ruby_parser>, [">= 2.0.2"])
|
38
|
-
s.add_dependency(%q<ParseTree>, [">= 3.0.3"])
|
39
|
-
s.add_dependency(%q<ruby2ruby>, ["= 1.1.9"])
|
40
|
-
s.add_dependency(%q<hoe>, [">= 2.3.3"])
|
41
|
-
end
|
42
|
-
end
|