method-args 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -36,5 +36,5 @@ If you want to get the value of a default argument, you'll need a context in whi
36
36
  an instance of the class from which the method derives.
37
37
 
38
38
  obj = MyClass.new
39
- MyClass.instance_method(:something).args.last.default_value(obj)
39
+ obj.method(:something).args.last.default_value
40
40
  # => 'hello'
@@ -2,26 +2,29 @@ require 'ruby2ruby'
2
2
  require 'ruby_parser'
3
3
  require 'sexp_processor'
4
4
 
5
- class Method
6
- attr_accessor :args
7
- end
5
+ module MethodMixin
6
+ attr_reader :args
8
7
 
9
- class UnboundMethod
10
- attr_accessor :args
8
+ def args=(a)
9
+ @args = a.clone
10
+ @args.owning_method = self
11
+ @args
12
+ end
11
13
  end
12
14
 
15
+ Method.send(:include, MethodMixin)
16
+ UnboundMethod.send(:include, MethodMixin)
17
+
13
18
  module MethodArgs
14
19
 
15
20
  class ArgList < Array
16
21
  class Arg
17
22
 
23
+ attr_accessor :arg_list
18
24
  attr_reader :name, :type
19
25
 
20
- def initialize(cls, arg)
21
- @cls = cls
22
- @name = arg[0]
23
- @type = arg[1]
24
- @default = arg[2]
26
+ def initialize(name, type, default = nil)
27
+ @name, @type, @default = name, type, default
25
28
  end
26
29
 
27
30
  def required?
@@ -36,13 +39,28 @@ module MethodArgs
36
39
  @type == :splat
37
40
  end
38
41
 
39
- def default_value(inst)
42
+ def default_value(receiver = nil)
40
43
  return nil if @default.nil?
41
- raise "You must evaluate defaults in the context of a matching class. #{inst.class.name} is not a #{@cls.name}." unless inst.is_a?(@cls)
42
- inst.instance_eval(@default)
44
+ receiver ||= arg_list.owning_method.receiver if arg_list.owning_method.respond_to?(:receiver)
45
+ raise "You must specify a receiver for the defaul value" if receiver.nil?
46
+ raise "You must evaluate defaults in the context of a matching class. #{receiver.class.name} is not a #{@cls.name}." unless receiver.is_a?(arg_list.cls)
47
+ receiver.instance_eval(@default)
43
48
  end
44
49
  end
45
-
50
+
51
+ attr_accessor :owning_method
52
+ attr_reader :cls
53
+
54
+ def initialize(cls)
55
+ @cls = cls
56
+ end
57
+
58
+ def clone
59
+ o = super
60
+ o.each {|arg| arg.arg_list = o}
61
+ o
62
+ end
63
+
46
64
  def required_size
47
65
  inject(0) {|count, arg| count += arg.required? ? 1 : 0}
48
66
  end
@@ -55,17 +73,8 @@ module MethodArgs
55
73
  def types
56
74
  map(&:type)
57
75
  end
58
-
59
- def initialize(cls, method)
60
- @cls, @method = cls, method
61
- end
62
-
63
- def to_method
64
- @cls.instance_method(@method)
65
- end
66
76
  end
67
77
 
68
-
69
78
  def self.load(file)
70
79
  file = File.expand_path(file)
71
80
  require file
@@ -131,15 +140,15 @@ module MethodArgs
131
140
 
132
141
  def process_args(exp)
133
142
  exp.shift
134
- arg_list = ArgList.new(current_class, @current_method)
143
+ arg_list = ArgList.new(current_class)
135
144
  while !exp.empty?
136
145
  t = exp.shift
137
146
  case t
138
147
  when Symbol
139
148
  arg_list << if t.to_s[0] == ?*
140
- ArgList::Arg.new(current_class, [t.to_s[1, t.to_s.size].to_sym, :splat])
149
+ ArgList::Arg.new(t.to_s[1, t.to_s.size].to_sym, :splat)
141
150
  else
142
- ArgList::Arg.new(current_class, [t, :required])
151
+ ArgList::Arg.new(t, :required)
143
152
  end
144
153
  when Sexp
145
154
  case t.shift
@@ -147,7 +156,7 @@ module MethodArgs
147
156
  lasgn = t.shift
148
157
  lasgn.shift
149
158
  name = lasgn.shift
150
- new_arg = ArgList::Arg.new(current_class, [name, :optional, @ruby2ruby.process(lasgn.last)])
159
+ new_arg = ArgList::Arg.new(name, :optional, @ruby2ruby.process(lasgn.last))
151
160
  arg_list.each_with_index{|arg, idx| arg_list[idx] = new_arg if arg.name == name}
152
161
  end
153
162
  end
@@ -160,6 +169,14 @@ module MethodArgs
160
169
  unless current_class.const_defined?(:ArgList)
161
170
  current_class.send(:const_set, :ArgList, @method_maps[current_classname])
162
171
  current_class.module_eval(<<-HERE_DOC, __FILE__, __LINE__)
172
+ alias_method :__method__, :method
173
+
174
+ def method(name)
175
+ m = __method__(name)
176
+ m.args = self.class.instance_arg_list(name)
177
+ m
178
+ end
179
+
163
180
  class << self
164
181
  alias_method :__instance_method__, :instance_method unless method_defined?(:__instance_method__)
165
182
  end
@@ -175,6 +192,10 @@ module MethodArgs
175
192
  ArgList[method_name]
176
193
  elsif method.owner.respond_to?(:instance_arg_list)
177
194
  method.owner.instance_arg_list(method_name)
195
+ # elsif method.respond_to?(:source_location)
196
+ # source, line = method.source_location
197
+ # MethodArgs.load(source)
198
+ # method.owner.instance_arg_list(method_name)
178
199
  else
179
200
  raise \"\#{method.owner} has not been loaded with method_args\"
180
201
  end
@@ -1,3 +1,3 @@
1
1
  module MethodArgs
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -0,0 +1,15 @@
1
+ class TestBoundMethod < MiniTest::Unit::TestCase
2
+ def setup
3
+ MethodArgs.load(~'fixtures/1')
4
+ end
5
+
6
+ def test_default_args
7
+ one = One.new
8
+ one.two_method = 'happy times'
9
+ two = One.new
10
+ two.two_method = 'more happy times'
11
+ assert_equal 'happy times', one.method(:default_args_with_dependant_value).args.last.default_value
12
+ assert_equal 'more happy times', two.method(:default_args_with_dependant_value).args.last.default_value
13
+ end
14
+
15
+ end
@@ -24,10 +24,6 @@ class TestSimple < MiniTest::Unit::TestCase
24
24
  assert_equal [:required, :optional], One.instance_method(:default_args_with_dependant_value).args.types
25
25
  end
26
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
27
  def test_default_args
32
28
  one = One.new
33
29
  one.two_method = 'happy times'
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: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joshua Hull
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-02 00:00:00 -08:00
18
+ date: 2011-01-03 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -160,6 +160,7 @@ files:
160
160
  - test/fixtures/1.rb
161
161
  - test/fixtures/2.rb
162
162
  - test/helper.rb
163
+ - test/test_bound_method.rb
163
164
  - test/test_simple.rb
164
165
  - test/test_subclass.rb
165
166
  has_rdoc: true
@@ -200,5 +201,6 @@ test_files:
200
201
  - test/fixtures/1.rb
201
202
  - test/fixtures/2.rb
202
203
  - test/helper.rb
204
+ - test/test_bound_method.rb
203
205
  - test/test_simple.rb
204
206
  - test/test_subclass.rb