ferb 0.6.3 → 0.6.4

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.
Files changed (3) hide show
  1. data/lib/ferb.rb +17 -5
  2. data/lib/method_args.rb +37 -98
  3. metadata +1 -1
data/lib/ferb.rb CHANGED
@@ -123,14 +123,25 @@ EOS
123
123
  end
124
124
 
125
125
  def self.construct_function_sig(function, sig)
126
- sig = sig.strip
127
- if !sig.empty?
128
- ["#{function.to_s.strip}(#{sig})", "#{function.to_s.strip}(#{MethodArgsHelper.get_args(sig).join(',')})"]
126
+ if sig and !sig.strip.empty?
127
+ sig = sig.strip
128
+ ma = MethodArgs.new
129
+ ["#{function.to_s.strip}(#{sig})", "#{function.to_s.strip}(#{ma.output_arg_info(sig).join(',')})"]
129
130
  else
130
- [function.to_s.strip, nil]
131
+ [function.to_s.strip, function.to_s.strip]
131
132
  end
132
133
  end
133
134
 
135
+ def self.extract_signature(function)
136
+ function = function.to_s.strip
137
+ result = [function, nil]
138
+ if function =~ /^([a-z0-9_?!=]*)\((.*)\)$/i
139
+ result = [$1, $2]
140
+ result = [$1, nil] if result[1].strip.empty?
141
+ end
142
+ result
143
+ end
144
+
134
145
  def self.construct_function_def(function, template, args = nil)
135
146
  parts = get_template_parts(template)
136
147
  funsig = nil
@@ -138,7 +149,8 @@ EOS
138
149
  if parts.is_a?(Array)
139
150
  funsig, clean_sig = construct_function_sig(function,parts[0])
140
151
  else
141
- funsig = function
152
+ sig = extract_signature(function)
153
+ funsig, clean_sig = construct_function_sig(*sig)
142
154
  end
143
155
  build_template(funsig, clean_sig, args, function)
144
156
  end
data/lib/method_args.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  # Code to extract the formal parameter names and types from a function
2
2
  # given a standard Ruby function argument signature. Original code based
3
3
  # on the MethodArgs module written by Mauricio Fernandez <mfp@acm.org>
4
- # (http://eigenclass.org). Updated to work with function signatures added at
5
- # runtime, also, peroperly handles block arguments.
4
+ # (http://eigenclass.org). Rewritten for the specific case of extracting named
5
+ # argument variables from an argument signature.
6
6
  #
7
7
  # Author:: Jim Powers (mailto:jim@corruptmemory.com)
8
8
  # Copyright:: Copyright (c) 2008-2009 Jim Powers
@@ -12,125 +12,64 @@
12
12
  # == Example:
13
13
  #
14
14
  # require 'method_args'
15
- # MethodArgsHelper.get_args("a, b = {}, *c, &d")
16
- # => ["a", "b", "*c", "&d"]
15
+ # ma = MethodArgs.new
16
+ # ma.output_arg_info("a=nil,b=2,c={},*d,&e")
17
+ # => ["a", "b", "c", "*d", "&e"]
17
18
  #
18
19
  # Very useful for building proxy functions that call an underlying function
19
20
  #
20
21
  class MethodArgs
21
22
  MAX_ARGS = 20
22
- attr_reader :params
23
+ attr_reader :params, :last_args
23
24
 
24
- def test_method(object,meth,*args)
25
- m = object.method(meth)
26
- m.call(*args)
25
+ def defined_test_method(sig)
26
+ instance_eval <<EOS
27
+ def tester(#{sig})
28
+ @values = {}
29
+ @params = []
30
+ local_variables.each {|x| @values[x] = eval(x) }
31
+ local_variables.each do |x|
32
+ val = @values[x]
33
+ if val.is_a?(Array)
34
+ @params << "*#{'#{x}'}"
35
+ elsif val.is_a?(Proc)
36
+ @params << "&#{'#{x}'}"
37
+ else
38
+ @params << x
39
+ end
40
+ end
41
+ throw :done
42
+ end
43
+ EOS
27
44
  end
28
45
 
29
- def output_method_info(klass, object, meth, is_singleton = false)
46
+
47
+ def test_method(*args)
48
+ tester(*args) { 1 }
49
+ end
50
+
51
+ def output_arg_info(sig)
52
+ defined_test_method(sig)
30
53
  @params = nil
31
54
  @values = nil
32
- @arg_count = nil
33
- @arity = nil
34
- num_args = 0
35
- unless %w[initialize].include?(meth.to_s)
36
- if is_singleton
37
- return if class << klass; private_instance_methods(true) end.include?(meth.to_s)
38
- else
39
- return if class << object; private_instance_methods(true) end.include?(meth.to_s)
40
- end
41
- end
42
- arity = is_singleton ? object.method(meth).arity : klass.instance_method(meth).arity
43
- set_trace_func lambda{|event, file, line, id, binding, classname|
44
- begin
45
- if event[/call/] && classname == MethodArgsHelper && id == meth
46
- @params = eval("local_variables", binding)
47
- @values = eval("local_variables.map{|x| eval(x)}", binding)
48
- if (@arg_count >= @arity) and (@params.length > @arity)
49
- if @values[-1].nil?
50
- @params[-1] = "&#{@params[-1]}"
51
- end
52
- end
53
- throw :done
54
- end
55
- rescue Exception
56
- end
57
- }
55
+ meth = method(:tester)
56
+ arity = meth.arity
58
57
  if arity >= 0
59
- num_args = arity
60
58
  catch(:done) do
61
- @arg_count = arity
62
- @arity = arity
63
- test_method(object,meth,*(0...arity))
59
+ test_method(*(0...arity))
64
60
  end
65
61
  else
66
62
  # determine number of args (including splat & block)
67
63
  MAX_ARGS.downto(arity.abs - 1) do |i|
68
64
  catch(:done) do
69
65
  begin
70
- @arg_count = i
71
- @arity = arity.abs - 1
72
- test_method(object,meth,*(0...i))
66
+ test_method(*(0...i))
73
67
  rescue Exception
74
68
  end
75
69
  end
76
- next if !@values || @values.compact.empty?
77
- k = nil
78
- @values.each_with_index{|x,j| break (k = j) if Array === x}
79
- if k
80
- num_args = k+1
81
- else
82
- num_args = i
83
- end
84
70
  end
85
- args = (0...arity.abs-1).to_a
86
- catch(:done) do
87
- if args.empty?
88
- @arg_count = 0
89
- @arity = 0
90
- test_method(object,meth)
91
- else
92
- @arg_count = args.length
93
- @arity = args.length
94
- test_method(object,meth,*args)
95
- end
96
- end
97
- end
98
- set_trace_func(nil)
99
- fmt_params = lambda do |arr, arity|
100
- arr.inject([[], 0]) do |(a, i), x|
101
- if Array === @values[i]
102
- [a << "*#{x}", i+1]
103
- else
104
- [a << x, i+1]
105
- end
106
- end.first
107
71
  end
108
- original_params = @params
109
- @params = fmt_params.call(@params,arity)
110
- set_trace_func(nil)
111
- end
112
- end
113
-
114
- class MethodArgsHelper
115
- @@last_args = nil
116
-
117
- def self.method_added(meth)
118
- begin
119
- o = self.allocate
120
- rescue Exception
121
- p $!
122
- end
123
- ma = MethodArgs.new
124
- ma.output_method_info(self, o, meth, false)
125
- @@last_args = ma.params
126
- end
127
-
128
- def self.get_args(sig)
129
- class_eval <<EOS
130
- def tester(#{sig})
131
- end
132
- EOS
133
- @@last_args
72
+ @params
134
73
  end
135
74
  end
136
75
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ferb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Powers