method-args 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -22,19 +22,19 @@ Pretend you have a file `your_ruby_file.rb`:
22
22
  To look at the arguments to something, do the following.
23
23
 
24
24
  MethodArgs.load('your_ruby_file') # <-- this also requires the file
25
- MyClass.arg_list(:something)
25
+ MyClass.instance_method(:something).args
26
26
 
27
27
  This will return an `ArgList` object. You can then look at the names & types.
28
28
 
29
- MyClass.arg_list(:something).names
29
+ MyClass.instance_method(:something).args.names
30
30
  # => [:one, :two, :three]
31
31
 
32
- MyClass.arg_list(:something).types
32
+ MyClass.instance_method(:something).args.types
33
33
  # => [:required, :optional, :splat]
34
34
 
35
35
  If you want to get the value of a default argument, you'll need a context in which to evaluate it, namely,
36
36
  an instance of the class from which the method derives.
37
37
 
38
38
  obj = MyClass.new
39
- MyClass.arg_list(:something).last.default_value(obj)
39
+ MyClass.instance_method(:something).args.last.default_value(obj)
40
40
  # => 'hello'
@@ -2,6 +2,14 @@ require 'ruby2ruby'
2
2
  require 'ruby_parser'
3
3
  require 'sexp_processor'
4
4
 
5
+ class Method
6
+ attr_accessor :args
7
+ end
8
+
9
+ class UnboundMethod
10
+ attr_accessor :args
11
+ end
12
+
5
13
  module MethodArgs
6
14
 
7
15
  class ArgList < Array
@@ -151,11 +159,28 @@ module MethodArgs
151
159
  def add_methods
152
160
  unless current_class.const_defined?(:ArgList)
153
161
  current_class.send(:const_set, :ArgList, @method_maps[current_classname])
154
- current_class.module_eval "
155
- def self.arg_list(method_name)
156
- ArgList[method_name]
162
+ current_class.module_eval(<<-HERE_DOC, __FILE__, __LINE__)
163
+ class << self
164
+ alias_method :__instance_method__, :instance_method unless method_defined?(:__instance_method__)
157
165
  end
158
- "
166
+ def self.instance_method(name)
167
+ m = __instance_method__(name)
168
+ m.args = instance_arg_list(name)
169
+ m
170
+ end
171
+
172
+ def self.instance_arg_list(method_name)
173
+ method = __instance_method__(method_name)
174
+ if method.owner == self
175
+ ArgList[method_name]
176
+ elsif method.owner.respond_to?(:instance_arg_list)
177
+ method.owner.instance_arg_list(method_name)
178
+ else
179
+ raise \"\#{method.owner} has not been loaded with method_args\"
180
+ end
181
+ end
182
+
183
+ HERE_DOC
159
184
  end
160
185
  end
161
186
 
@@ -1,3 +1,3 @@
1
1
  module MethodArgs
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
12
12
  s.summary = "Get back more detailed information about the arguments for a method"
13
13
  s.description = "Get back more detailed information about the arguments for a method"
14
14
 
15
- s.rubyforge_project = "method_args2"
15
+ s.rubyforge_project = "method_args"
16
16
 
17
17
  s.files = `git ls-files`.split("\n")
18
18
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -0,0 +1,15 @@
1
+ class Two
2
+ def one(hi, there)
3
+ end
4
+ end
5
+
6
+ module TwoModule
7
+ def mod(from, mod)
8
+ end
9
+ end
10
+
11
+ class TwoSubclass < Two
12
+ include TwoModule
13
+ def two(one, more)
14
+ end
15
+ end
@@ -0,0 +1,37 @@
1
+ class TestSimple < MiniTest::Unit::TestCase
2
+ def setup
3
+ MethodArgs.load(~'fixtures/1')
4
+ end
5
+
6
+ def test_arg_counts
7
+ assert_equal 0, One.instance_method(:no_args).args.count
8
+ assert_equal 1, One.instance_method(:one_args).args.count
9
+ assert_equal 2, One.instance_method(:two_args).args.count
10
+ assert_equal 3, One.instance_method(:splat_args).args.count
11
+ assert_equal 2, One.instance_method(:splat_args).args.required_count
12
+ assert_equal 2, One.instance_method(:default_args).args.count
13
+ assert_equal 1, One.instance_method(:default_args).args.required_count
14
+ assert_equal 2, One.instance_method(:default_args_with_dependant_value).args.count
15
+ assert_equal 1, One.instance_method(:default_args_with_dependant_value).args.required_count
16
+ end
17
+
18
+ def test_arg_types
19
+ assert_equal [], One.instance_method(:no_args).args.types
20
+ assert_equal [:required], One.instance_method(:one_args).args.types
21
+ assert_equal [:required, :required], One.instance_method(:two_args).args.types
22
+ assert_equal [:required, :required, :splat], One.instance_method(:splat_args).args.types
23
+ assert_equal [:required, :optional], One.instance_method(:default_args).args.types
24
+ assert_equal [:required, :optional], One.instance_method(:default_args_with_dependant_value).args.types
25
+ end
26
+
27
+ def test_arg_method
28
+ assert_equal One.instance_method(:no_args), One.instance_arg_list(:no_args).to_method
29
+ end
30
+
31
+ def test_default_args
32
+ one = One.new
33
+ one.two_method = 'happy times'
34
+ assert_equal 'happy times', One.instance_method(:default_args_with_dependant_value).args.last.default_value(one)
35
+ end
36
+
37
+ end
@@ -0,0 +1,17 @@
1
+ class TestSubclass < MiniTest::Unit::TestCase
2
+ def setup
3
+ MethodArgs.load(~'fixtures/2')
4
+ end
5
+
6
+ def test_normal_visibility
7
+ assert_equal [:one, :more], TwoSubclass.instance_method(:two).args.names
8
+ end
9
+
10
+ def test_superclass_visibility
11
+ assert_equal [:hi, :there], TwoSubclass.instance_method(:one).args.names
12
+ end
13
+
14
+ def test_module_visibility
15
+ assert_equal [:from, :mod], TwoSubclass.instance_method(:mod).args.names
16
+ end
17
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: method-args
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joshua Hull
@@ -158,8 +158,10 @@ files:
158
158
  - lib/method_args/version.rb
159
159
  - method_args.gemspec
160
160
  - test/fixtures/1.rb
161
+ - test/fixtures/2.rb
161
162
  - test/helper.rb
162
- - test/test_method_args.rb
163
+ - test/test_simple.rb
164
+ - test/test_subclass.rb
163
165
  has_rdoc: true
164
166
  homepage: https://github.com/joshbuddy/method_args
165
167
  licenses: []
@@ -189,12 +191,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
189
191
  version: "0"
190
192
  requirements: []
191
193
 
192
- rubyforge_project: method_args2
194
+ rubyforge_project: method_args
193
195
  rubygems_version: 1.3.7
194
196
  signing_key:
195
197
  specification_version: 3
196
198
  summary: Get back more detailed information about the arguments for a method
197
199
  test_files:
198
200
  - test/fixtures/1.rb
201
+ - test/fixtures/2.rb
199
202
  - test/helper.rb
200
- - test/test_method_args.rb
203
+ - test/test_simple.rb
204
+ - test/test_subclass.rb
@@ -1,37 +0,0 @@
1
- class TestMethodArgs < MiniTest::Unit::TestCase
2
- def setup
3
- MethodArgs.load(~'fixtures/1')
4
- end
5
-
6
- def test_arg_counts
7
- assert_equal 0, One.arg_list(:no_args).count
8
- assert_equal 1, One.arg_list(:one_args).count
9
- assert_equal 2, One.arg_list(:two_args).count
10
- assert_equal 3, One.arg_list(:splat_args).count
11
- assert_equal 2, One.arg_list(:splat_args).required_count
12
- assert_equal 2, One.arg_list(:default_args).count
13
- assert_equal 1, One.arg_list(:default_args).required_count
14
- assert_equal 2, One.arg_list(:default_args_with_dependant_value).count
15
- assert_equal 1, One.arg_list(:default_args_with_dependant_value).required_count
16
- end
17
-
18
- def test_arg_types
19
- assert_equal [], One.arg_list(:no_args).types
20
- assert_equal [:required], One.arg_list(:one_args).types
21
- assert_equal [:required, :required], One.arg_list(:two_args).types
22
- assert_equal [:required, :required, :splat], One.arg_list(:splat_args).types
23
- assert_equal [:required, :optional], One.arg_list(:default_args).types
24
- assert_equal [:required, :optional], One.arg_list(:default_args_with_dependant_value).types
25
- end
26
-
27
- def test_arg_method
28
- assert_equal One.instance_method(:no_args), One.arg_list(:no_args).to_method
29
- end
30
-
31
- def test_default_args
32
- one = One.new
33
- one.two_method = 'happy times'
34
- assert_equal 'happy times', One.arg_list(:default_args_with_dependant_value).last.default_value(one)
35
- end
36
-
37
- end