merb-action-args 0.9.12 → 0.9.13

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/Rakefile CHANGED
@@ -1,9 +1,4 @@
1
- require 'rubygems'
2
- require "merb-core"
3
- require 'rake/gempackagetask'
4
- require "extlib"
5
- require File.join(File.dirname(__FILE__), '../merb-core/lib/merb-core/tasks/merb_rake_helper')
6
- require "spec/rake/spectask"
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "rake_helpers"))
7
2
 
8
3
  ##############################################################################
9
4
  # Package && release
@@ -10,8 +10,12 @@ class Merb::AbstractController
10
10
  # klass<Class>::
11
11
  # The controller that is being inherited from Merb::AbstractController.
12
12
  def inherited(klass)
13
- klass.action_argument_list = Hash.new do |h,k|
14
- h[k] = ParseTreeArray.translate(klass, k.to_sym).get_args
13
+ klass.action_argument_list = Hash.new do |h,k|
14
+ args = klass.instance_method(k).get_args
15
+ arguments = args[0]
16
+ defaults = []
17
+ arguments.each {|a| defaults << a[0] if a.size == 2} if arguments
18
+ h[k] = [arguments || [], defaults]
15
19
  end
16
20
  old_inherited(klass)
17
21
  end
@@ -26,6 +30,7 @@ class Merb::AbstractController
26
30
  # BadRequest:: The params hash doesn't have a required parameter.
27
31
  def _call_action(action)
28
32
  arguments, defaults = self.class.action_argument_list[action]
33
+
29
34
  args = arguments.map do |arg, default|
30
35
  arg = arg
31
36
  p = params.key?(arg.to_sym)
@@ -1,89 +1,93 @@
1
- require 'parse_tree'
2
- require 'ruby2ruby'
1
+ unless RUBY_PLATFORM == "java"
2
+ require 'parse_tree'
3
+ require 'ruby2ruby'
3
4
 
4
- class ParseTreeArray < Array
5
- R2R = Object.const_defined?(:Ruby2Ruby) ? Ruby2Ruby : RubyToRuby
5
+ class ParseTreeArray < Array
6
+ R2R = Object.const_defined?(:Ruby2Ruby) ? Ruby2Ruby : RubyToRuby
6
7
 
7
- def self.translate(*args)
8
- sexp = ParseTree.translate(*args)
9
- # ParseTree.translate returns [nil] if called on an inherited method, so walk
10
- # up the inheritance chain to find the class that the method was defined in
11
- unless sexp.first
12
- klass = args.first.ancestors.detect do |klass|
13
- klass.public_instance_methods(false).include?(args.last.to_s)
8
+ def self.translate(*args)
9
+ sexp = ParseTree.translate(*args)
10
+ # ParseTree.translate returns [nil] if called on an inherited method, so walk
11
+ # up the inheritance chain to find the class that the method was defined in
12
+ unless sexp.first
13
+ klass = args.first.ancestors.detect do |klass|
14
+ klass.public_instance_methods(false).include?(args.last.to_s)
15
+ end
16
+ sexp = ParseTree.translate(klass, args.last) if klass
14
17
  end
15
- sexp = ParseTree.translate(klass, args.last) if klass
18
+ self.new(sexp)
16
19
  end
17
- self.new(sexp)
18
- end
19
20
 
20
- def deep_array_node(type = nil)
21
- each do |node|
22
- return ParseTreeArray.new(node) if node.is_a?(Array) && (!type || node[0] == type)
23
- next unless node.is_a?(Array)
24
- return ParseTreeArray.new(node).deep_array_node(type)
21
+ def deep_array_node(type = nil)
22
+ each do |node|
23
+ return ParseTreeArray.new(node) if node.is_a?(Array) && (!type || node[0] == type)
24
+ next unless node.is_a?(Array)
25
+ return ParseTreeArray.new(node).deep_array_node(type)
26
+ end
27
+ nil
25
28
  end
26
- nil
27
- end
28
29
 
29
- def arg_nodes
30
- self[1..-1].inject([]) do |sum,item|
31
- sum << [item] unless item.is_a?(Array)
32
- sum
30
+ def arg_nodes
31
+ self[1..-1].inject([]) do |sum,item|
32
+ sum << [item] unless item.is_a?(Array)
33
+ sum
34
+ end
33
35
  end
34
- end
35
36
 
36
- def get_args
37
- if arg_node = deep_array_node(:args)
38
- # method defined with def keyword
39
- args = arg_node.arg_nodes
40
- default_node = arg_node.deep_array_node(:block)
41
- return [args, []] unless default_node
42
- else
43
- # assuming method defined with Module#define_method
44
- return [[],[]]
45
- end
37
+ def get_args
38
+ if arg_node = deep_array_node(:args)
39
+ # method defined with def keyword
40
+ args = arg_node.arg_nodes
41
+ default_node = arg_node.deep_array_node(:block)
42
+ return [args, []] unless default_node
43
+ else
44
+ # assuming method defined with Module#define_method
45
+ return [[],[]]
46
+ end
46
47
 
47
- # if it was defined with def, and we found the default_node,
48
- # that should bring us back to regularly scheduled programming..
49
- lasgns = default_node[1..-1]
50
- lasgns.each do |asgn|
51
- args.assoc(asgn[1]) << eval(R2R.new.process(asgn[2]))
48
+ # if it was defined with def, and we found the default_node,
49
+ # that should bring us back to regularly scheduled programming..
50
+ lasgns = default_node[1..-1]
51
+ lasgns.each do |asgn|
52
+ args.assoc(asgn[1]) << eval(R2R.new.process(asgn[2]))
53
+ end
54
+ [args, (default_node[1..-1].map { |asgn| asgn[1] })]
52
55
  end
53
- [args, (default_node[1..-1].map { |asgn| asgn[1] })]
54
- end
55
56
 
56
- end
57
+ end
57
58
 
58
- # Used in mapping controller arguments to the params hash.
59
- # NOTE: You must have the 'ruby2ruby' gem installed for this to work.
60
- #
61
- # ==== Examples
62
- # # In PostsController
63
- # def show(id) #=> id is the same as params[:id]
64
- module GetArgs
65
-
66
- # ==== Returns
67
- # Array:: Method arguments and their default values.
59
+ # Used in mapping controller arguments to the params hash.
60
+ # NOTE: You must have the 'ruby2ruby' gem installed for this to work.
68
61
  #
69
62
  # ==== Examples
70
- # class Example
71
- # def hello(one,two="two",three)
72
- # end
73
- #
74
- # def goodbye
75
- # end
76
- # end
77
- #
78
- # Example.instance_method(:hello).get_args
79
- # #=> [[:one], [:two, "two"], [:three, "three"]]
80
- # Example.instance_method(:goodbye).get_args #=> nil
81
- def get_args
82
- klass, meth = self.to_s.split(/ /).to_a[1][0..-2].split("#")
83
- # Remove stupidity for #<Method: Class(Object)#foo>
84
- klass = $` if klass =~ /\(/
85
- ParseTreeArray.translate(Object.const_get(klass), meth).get_args
63
+ # # In PostsController
64
+ # def show(id) #=> id is the same as params[:id]
65
+ module GetArgs
66
+
67
+ # ==== Returns
68
+ # Array:: Method arguments and their default values.
69
+ #
70
+ # ==== Examples
71
+ # class Example
72
+ # def hello(one,two="two",three)
73
+ # end
74
+ #
75
+ # def goodbye
76
+ # end
77
+ # end
78
+ #
79
+ # Example.instance_method(:hello).get_args
80
+ # #=> [[:one], [:two, "two"], [:three, "three"]]
81
+ # Example.instance_method(:goodbye).get_args #=> nil
82
+ def get_args
83
+ klass, meth = self.to_s.split(/ /).to_a[1][0..-2].split("#")
84
+ # Remove stupidity for #<Method: Class(Object)#foo>
85
+ klass = $` if klass =~ /\(/
86
+ ParseTreeArray.translate(Object.const_get(klass), meth).get_args
87
+ end
86
88
  end
89
+ else
90
+ require File.dirname(__FILE__) / "jruby_args"
87
91
  end
88
92
 
89
93
  class UnboundMethod
@@ -0,0 +1,67 @@
1
+ require 'java'
2
+ require 'jruby'
3
+
4
+ module GetArgs
5
+ Methods = org.jruby.internal.runtime.methods
6
+
7
+ def get_args
8
+ real_method = JRuby.reference(self)
9
+
10
+ # hack to expose a protected field; could be improved in 1.1.5
11
+ method_field = org.jruby.RubyMethod.java_class.declared_field(:method)
12
+
13
+ method_field.accessible = true
14
+
15
+ dyn_method = method_field.value(real_method)
16
+
17
+ case dyn_method
18
+ when Methods.MethodArgs
19
+ return build_args(dyn_method.args_node)
20
+ else
21
+ raise "Can't get args from method: #{self}"
22
+ end
23
+ end
24
+
25
+ def build_args(args_node)
26
+ args = []
27
+ required = []
28
+ optional = []
29
+
30
+ # required args
31
+ if (args_node.args && args_node.args.size > 0)
32
+ required << args_node.args.child_nodes.map { |arg| [arg.name.to_s.intern] }
33
+ end
34
+
35
+ # optional args
36
+ if (args_node.opt_args && args_node.opt_args.size > 0)
37
+ optional << args_node.opt_args.child_nodes.map do |arg|
38
+ name = arg.name.to_s.intern
39
+ value_node = arg.value_node
40
+ case value_node
41
+ when org.jruby.ast::FixnumNode
42
+ value = value_node.value
43
+ when org.jruby.ast::SymbolNode
44
+ value = value_node.get_symbol(JRuby.runtime)
45
+ when org.jruby.ast::StrNode
46
+ value = value_node.value
47
+ else
48
+ value = nil
49
+ end
50
+ [name, value]
51
+ end
52
+ end
53
+
54
+ first_args = required.first
55
+ optional.first.each {|arg| first_args << arg} if optional.first
56
+
57
+ args = [first_args]
58
+
59
+ rest = args_node.rest_arg_node
60
+ args << (rest ? rest.name.to_s.intern : nil)
61
+
62
+ block = args_node.block_arg_node
63
+ args << (block ? block.name.to_s.intern : nil)
64
+
65
+ args
66
+ end
67
+ end
@@ -10,6 +10,10 @@ end
10
10
 
11
11
  describe Merb::AbstractController do
12
12
 
13
+ it "should be able to handle no arguments" do
14
+ dispatch_to(ActionArgs, :nada).body.should == "NADA"
15
+ end
16
+
13
17
  it "should be able to accept Action Arguments" do
14
18
  dispatch_to(ActionArgs, :index, :foo => "bar").body.should == "bar"
15
19
  end
@@ -11,6 +11,10 @@ end
11
11
 
12
12
  class ActionArgs < Merb::Controller
13
13
  include ExtraActions
14
+
15
+ def nada
16
+ "NADA"
17
+ end
14
18
 
15
19
  def index(foo)
16
20
  foo
@@ -31,7 +35,7 @@ class ActionArgs < Merb::Controller
31
35
  define_method :dynamic_define_method do
32
36
  "mos def"
33
37
  end
34
-
38
+
35
39
  def with_default_nil(foo, bar = nil)
36
40
  "#{foo} #{bar}"
37
41
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: merb-action-args
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.12
4
+ version: 0.9.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yehuda Katz
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-10-29 00:00:00 -07:00
12
+ date: 2008-11-03 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.9.12
23
+ version: 0.9.13
24
24
  version:
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: ruby2ruby
@@ -60,6 +60,7 @@ files:
60
60
  - lib/merb-action-args
61
61
  - lib/merb-action-args/abstract_controller.rb
62
62
  - lib/merb-action-args/get_args.rb
63
+ - lib/merb-action-args/jruby_args.rb
63
64
  - lib/merb-action-args.rb
64
65
  - spec/action_args_spec.rb
65
66
  - spec/controllers
@@ -87,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
88
  requirements: []
88
89
 
89
90
  rubyforge_project: merb
90
- rubygems_version: 1.3.0
91
+ rubygems_version: 1.3.1
91
92
  signing_key:
92
93
  specification_version: 2
93
94
  summary: Merb plugin that provides support for ActionArgs