named_parameter 1.0.1 → 1.0.2
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/README.md +1 -1
- data/lib/named_parameter/defaults_extractor/default_parameters_extractor.rb +43 -0
- data/lib/named_parameter/defaults_extractor/signature_extractor.rb +48 -0
- data/lib/named_parameter/defaults_extractor.rb +2 -0
- data/lib/named_parameter/parameters_adapter.rb +3 -41
- data/lib/named_parameter/version.rb +1 -1
- data/lib/named_parameter.rb +1 -0
- data/spec/bug_fixing/parameters_order_spec.rb +1 -1
- data/spec/unit/defaults_extractor/default_parameters_extractor_spec.rb +35 -0
- data/spec/unit/defaults_extractor/signature_extractor_spec.rb +93 -0
- metadata +11 -3
data/README.md
CHANGED
@@ -0,0 +1,43 @@
|
|
1
|
+
module NamedParameter
|
2
|
+
module DefaultsExtractor
|
3
|
+
class DefaultParametersExtractor
|
4
|
+
def defaults_of(method)
|
5
|
+
filepath = method.source_location[0]
|
6
|
+
linenumber = method.source_location[1]
|
7
|
+
|
8
|
+
if filepath.downcase == "(irb)"
|
9
|
+
msg = NamedParameter::Errors::OnIrbException::MESSAGE
|
10
|
+
raise NamedParameter::Errors::OnIrbException, msg
|
11
|
+
end
|
12
|
+
|
13
|
+
rb_content = File.read(filepath)
|
14
|
+
|
15
|
+
extractor = SignatureExtractor.new
|
16
|
+
signature = extractor.signature_of(rb_content, linenumber);
|
17
|
+
|
18
|
+
created_method = method_with_signature(signature, method)
|
19
|
+
|
20
|
+
defaults = call_with_required_params(created_method)
|
21
|
+
|
22
|
+
defaults
|
23
|
+
end
|
24
|
+
|
25
|
+
def method_with_signature(signature, original)
|
26
|
+
argnames = original.parameters.map{|(_, name)| name}
|
27
|
+
method_code = "#{signature}; return [#{argnames.join(",")}];end"
|
28
|
+
eval(method_code)
|
29
|
+
|
30
|
+
method(original.name)
|
31
|
+
end
|
32
|
+
|
33
|
+
def call_with_required_params(method)
|
34
|
+
required = method.parameters
|
35
|
+
.find_all{|(tag, _)| tag == :req}
|
36
|
+
.map{|(_, name)| name}
|
37
|
+
|
38
|
+
args = [nil] * required.size
|
39
|
+
method.call(*args)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module NamedParameter
|
2
|
+
module DefaultsExtractor
|
3
|
+
class SignatureExtractor
|
4
|
+
module EndLineValidators
|
5
|
+
WITH_PARENTESIS = lambda{|line| line.strip.end_with?(")")}
|
6
|
+
WITHOUT_PARENTESIS = lambda{|line| !line.strip.end_with?(",")}
|
7
|
+
end
|
8
|
+
|
9
|
+
def signature_of(content, linenumber)
|
10
|
+
content = extract_until_signature_starts(content, linenumber)
|
11
|
+
|
12
|
+
signature_end = find_signature_end content
|
13
|
+
|
14
|
+
signature = content[0..signature_end].split("\n").map{|line| line.strip}.join
|
15
|
+
|
16
|
+
signature
|
17
|
+
end
|
18
|
+
|
19
|
+
def extract_until_signature_starts(content, linenumber)
|
20
|
+
content
|
21
|
+
.split("\n")[(linenumber-1)..-1]
|
22
|
+
.join("\n")
|
23
|
+
.gsub(";", "\n")
|
24
|
+
.strip
|
25
|
+
.reverse.gsub(/ fed.*$/, " fed").reverse
|
26
|
+
end
|
27
|
+
|
28
|
+
def find_signature_end(signature)
|
29
|
+
if signature =~ /^def +[A-Za-z_\?]+ *\(/
|
30
|
+
sign_end_validator = EndLineValidators::WITH_PARENTESIS
|
31
|
+
else
|
32
|
+
sign_end_validator = EndLineValidators::WITHOUT_PARENTESIS
|
33
|
+
end
|
34
|
+
|
35
|
+
lines = signature.split("\n")
|
36
|
+
sign_end = 0
|
37
|
+
i = 0
|
38
|
+
until sign_end_validator.call(lines[i])
|
39
|
+
sign_end += lines[i].size + 1
|
40
|
+
i += 1
|
41
|
+
end
|
42
|
+
sign_end += lines[i].size
|
43
|
+
|
44
|
+
sign_end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -33,54 +33,16 @@ module NamedParameter
|
|
33
33
|
end
|
34
34
|
|
35
35
|
if arguments.any?{|arg| arg.equal? NO_ARGUMENT}
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
if filepath.downcase == "(irb)"
|
40
|
-
msg = NamedParameter::Errors::OnIrbException::MESSAGE
|
41
|
-
raise NamedParameter::Errors::OnIrbException, msg
|
42
|
-
end
|
43
|
-
|
44
|
-
content = File.read(filepath).split("\n")[(linenumber-1)..-1].join.strip
|
36
|
+
extractor = DefaultsExtractor::DefaultParametersExtractor.new
|
37
|
+
defaults = extractor.defaults_of(@method)
|
45
38
|
|
46
|
-
default_values = extract_default_values content
|
47
|
-
|
48
39
|
arguments.map!.with_index do |arg, i|
|
49
40
|
no_argument = arg.equal? NO_ARGUMENT
|
50
|
-
no_argument ?
|
41
|
+
no_argument ? defaults[i] : arg
|
51
42
|
end
|
52
43
|
end
|
53
44
|
|
54
45
|
return arguments
|
55
46
|
end
|
56
|
-
|
57
|
-
def extract_default_values(file_fragment)
|
58
|
-
match = file_fragment.match /^[^\(]*\(([^\)]*)\)/
|
59
|
-
args_fragment = match[1]
|
60
|
-
args_fragments = extract_args_fragments args_fragment
|
61
|
-
default_values = args_fragments.map{|frag| eval(frag.gsub(/^[^=]*(=|$)/, ""))}
|
62
|
-
return default_values
|
63
|
-
end
|
64
|
-
|
65
|
-
def extract_args_fragments(fragment)
|
66
|
-
base_frags = fragment.split ","
|
67
|
-
frags = []
|
68
|
-
i = 0
|
69
|
-
while i < base_frags.size
|
70
|
-
frag = base_frags[i]
|
71
|
-
while inconsistent_fragment?(frag)
|
72
|
-
i += 1
|
73
|
-
frag += ",#{base_frags[i]}"
|
74
|
-
end
|
75
|
-
frags << frag
|
76
|
-
i += 1;
|
77
|
-
end
|
78
|
-
return frags
|
79
|
-
end
|
80
|
-
|
81
|
-
def inconsistent_fragment?(fragment)
|
82
|
-
return fragment.count("([{") != fragment.count(")]}") ||
|
83
|
-
fragment.count(%q{"'/}).odd?
|
84
|
-
end
|
85
47
|
end
|
86
48
|
end
|
data/lib/named_parameter.rb
CHANGED
@@ -87,7 +87,7 @@ describe "BugFix: should suport any kind use combination of optional parameters"
|
|
87
87
|
end
|
88
88
|
|
89
89
|
|
90
|
-
context %q{named def whatever(a="z",
|
90
|
+
context %q{named def whatever(a,b,c="z", d="z",e="z",f="z")} do
|
91
91
|
specify "inline" do
|
92
92
|
class SomeClass
|
93
93
|
extend NamedParameter
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module NamedParameter
|
4
|
+
module DefaultsExtractor
|
5
|
+
describe DefaultParametersExtractor do
|
6
|
+
it 'extract for parameters with one required at right' do
|
7
|
+
def sample(a="a", b="b", c)
|
8
|
+
end
|
9
|
+
|
10
|
+
extractor = DefaultParametersExtractor.new
|
11
|
+
defaults = extractor.defaults_of(method(:sample))
|
12
|
+
|
13
|
+
defaults.should == ["a", "b", nil]
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'extract for parameters with ;' do
|
17
|
+
def sample(a="a", b="b", c, d);end
|
18
|
+
|
19
|
+
extractor = DefaultParametersExtractor.new
|
20
|
+
defaults = extractor.defaults_of(method(:sample))
|
21
|
+
|
22
|
+
defaults.should == ["a", "b", nil, nil]
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'extract from 2 required at left' do
|
26
|
+
def sample(a,b,c="c", d="d",e="e",f="f");end
|
27
|
+
|
28
|
+
extractor = DefaultParametersExtractor.new
|
29
|
+
defaults = extractor.defaults_of(method(:sample))
|
30
|
+
|
31
|
+
defaults.should == [nil, nil, "c", "d", "e", "f"]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module NamedParameter
|
4
|
+
module DefaultsExtractor
|
5
|
+
describe SignatureExtractor do
|
6
|
+
it 'extract for parameters with simple strings as defaults' do
|
7
|
+
code =
|
8
|
+
%q{class Teste
|
9
|
+
def sample(a="a", b="b", c)
|
10
|
+
end
|
11
|
+
end}
|
12
|
+
|
13
|
+
extractor = SignatureExtractor.new
|
14
|
+
signature = extractor.signature_of(code, 2)
|
15
|
+
|
16
|
+
signature.should == %q{def sample(a="a", b="b", c)}
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'extract from no parentesis methods' do
|
20
|
+
code =
|
21
|
+
%q{class Teste
|
22
|
+
def sample a="a", b="b", c
|
23
|
+
end
|
24
|
+
end}
|
25
|
+
|
26
|
+
extractor = SignatureExtractor.new
|
27
|
+
signature = extractor.signature_of(code, 2)
|
28
|
+
|
29
|
+
signature.should == %q{def sample a="a", b="b", c}
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'extract from methods with more than one line' do
|
33
|
+
code =
|
34
|
+
%q{class Teste
|
35
|
+
def sample a="a",
|
36
|
+
b="b",
|
37
|
+
c
|
38
|
+
end
|
39
|
+
end}
|
40
|
+
|
41
|
+
extractor = SignatureExtractor.new
|
42
|
+
signature = extractor.signature_of(code, 2)
|
43
|
+
|
44
|
+
signature.should == %q{def sample a="a",b="b",c}
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'extract from methods with more than one line and parentesis' do
|
48
|
+
code =
|
49
|
+
%q{class Teste
|
50
|
+
def sample(a="a",
|
51
|
+
b="b",
|
52
|
+
c
|
53
|
+
)
|
54
|
+
end
|
55
|
+
end}
|
56
|
+
|
57
|
+
extractor = SignatureExtractor.new
|
58
|
+
signature = extractor.signature_of(code, 2)
|
59
|
+
|
60
|
+
signature.should == %q{def sample(a="a",b="b",c)}
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'extract from methods with something before def' do
|
64
|
+
code =
|
65
|
+
%q{class Teste
|
66
|
+
named def sample(a="a",
|
67
|
+
b="b",
|
68
|
+
c
|
69
|
+
)
|
70
|
+
end
|
71
|
+
end}
|
72
|
+
|
73
|
+
extractor = SignatureExtractor.new
|
74
|
+
signature = extractor.signature_of(code, 2)
|
75
|
+
|
76
|
+
signature.should == %q{def sample(a="a",b="b",c)}
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
it 'can have an "def" in the middle of the signature' do
|
81
|
+
code =
|
82
|
+
%q{class Teste
|
83
|
+
def sample(arg="default");end
|
84
|
+
end}
|
85
|
+
|
86
|
+
extractor = SignatureExtractor.new
|
87
|
+
signature = extractor.signature_of(code, 2)
|
88
|
+
|
89
|
+
signature.should == %q{def sample(arg="default")}
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: named_parameter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-10-20 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Allows named parameter in ruby
|
15
15
|
email:
|
@@ -23,6 +23,9 @@ files:
|
|
23
23
|
- README.md
|
24
24
|
- Rakefile
|
25
25
|
- lib/named_parameter.rb
|
26
|
+
- lib/named_parameter/defaults_extractor.rb
|
27
|
+
- lib/named_parameter/defaults_extractor/default_parameters_extractor.rb
|
28
|
+
- lib/named_parameter/defaults_extractor/signature_extractor.rb
|
26
29
|
- lib/named_parameter/error.rb
|
27
30
|
- lib/named_parameter/errors.rb
|
28
31
|
- lib/named_parameter/errors/not_hash.rb
|
@@ -46,6 +49,8 @@ files:
|
|
46
49
|
- spec/features/named_inline/individual_named_method_feature_spec.rb
|
47
50
|
- spec/features/named_inline/singleton_named_method_feature_spec.rb
|
48
51
|
- spec/spec_helper.rb
|
52
|
+
- spec/unit/defaults_extractor/default_parameters_extractor_spec.rb
|
53
|
+
- spec/unit/defaults_extractor/signature_extractor_spec.rb
|
49
54
|
- spec/unit/error_spec.rb
|
50
55
|
- spec/unit/errors/not_hash_spec.rb
|
51
56
|
- spec/unit/errors/required_parameters_spec.rb
|
@@ -76,7 +81,7 @@ rubyforge_project:
|
|
76
81
|
rubygems_version: 1.8.15
|
77
82
|
signing_key:
|
78
83
|
specification_version: 3
|
79
|
-
summary: named_parameter-1.0.
|
84
|
+
summary: named_parameter-1.0.2
|
80
85
|
test_files:
|
81
86
|
- spec/bug_fixing/parameters_order_spec.rb
|
82
87
|
- spec/features/named_above/class_self_named_method_feature_spec.rb
|
@@ -86,6 +91,8 @@ test_files:
|
|
86
91
|
- spec/features/named_inline/individual_named_method_feature_spec.rb
|
87
92
|
- spec/features/named_inline/singleton_named_method_feature_spec.rb
|
88
93
|
- spec/spec_helper.rb
|
94
|
+
- spec/unit/defaults_extractor/default_parameters_extractor_spec.rb
|
95
|
+
- spec/unit/defaults_extractor/signature_extractor_spec.rb
|
89
96
|
- spec/unit/error_spec.rb
|
90
97
|
- spec/unit/errors/not_hash_spec.rb
|
91
98
|
- spec/unit/errors/required_parameters_spec.rb
|
@@ -93,3 +100,4 @@ test_files:
|
|
93
100
|
- spec/unit/named_method_spec.rb
|
94
101
|
- spec/unit/named_method_transmuter_spec.rb
|
95
102
|
- spec/unit/parameters_adapter_spec.rb
|
103
|
+
has_rdoc:
|