merb-action-args 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README +49 -0
- data/Rakefile +44 -0
- data/TODO +5 -0
- data/lib/merb-action-args.rb +2 -0
- data/lib/merb-action-args/abstract_controller.rb +36 -0
- data/lib/merb-action-args/get_args.rb +84 -0
- metadata +70 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 YOUR NAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
merb-action-args
|
2
|
+
================
|
3
|
+
|
4
|
+
A plugin for the Merb framework that provides support for arguments to actions that
|
5
|
+
come in from the query.
|
6
|
+
|
7
|
+
==== Basics
|
8
|
+
|
9
|
+
{{[
|
10
|
+
class Foo < Merb::Controller
|
11
|
+
def bar(baz)
|
12
|
+
bar
|
13
|
+
end
|
14
|
+
end
|
15
|
+
]}}
|
16
|
+
|
17
|
+
Hitting "/foo/bar?baz=bat" will call foo("bat").
|
18
|
+
|
19
|
+
Hitting "/foo/bar" will raise a BadRequest (Status 400) error.
|
20
|
+
|
21
|
+
==== Defaults
|
22
|
+
|
23
|
+
{{[
|
24
|
+
class Foo < Merb::Controller
|
25
|
+
def bar(baz, bat = "hola")
|
26
|
+
"#{baz} #{bat}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
]}}
|
30
|
+
|
31
|
+
Hitting "/foo/bar?baz=bat" will call foo("bat", "hola")
|
32
|
+
|
33
|
+
Hitting "/foo/bar?baz=bat&bat=whaa" will call foo("bat", "whaa")
|
34
|
+
|
35
|
+
Hitting "/foo/bar" will still raise a BadRequest.
|
36
|
+
|
37
|
+
==== Out of order defaults
|
38
|
+
|
39
|
+
{{[
|
40
|
+
class Foo < Merb::Controller
|
41
|
+
def bar(one, two = "dos", three = "tres")
|
42
|
+
"#{one} #{two} #{three}"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
]}}
|
46
|
+
|
47
|
+
The interesting thing here is that hitting "/foo/bar?one=uno&three=three" will call
|
48
|
+
foo("uno", "dos", "three"). In other words, the defaults can be in any order, and
|
49
|
+
merb-action-args will figure out where to fill in the holes.
|
data/Rakefile
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
|
4
|
+
PLUGIN = "merb-action-args"
|
5
|
+
NAME = "merb-action-args"
|
6
|
+
VERSION = "0.9.2"
|
7
|
+
AUTHOR = "Yehuda Katz"
|
8
|
+
EMAIL = "ykatz@engineyard.com"
|
9
|
+
HOMEPAGE = "http://merb-plugins.rubyforge.org/merb-haml/"
|
10
|
+
SUMMARY = "Merb plugin that provides support for ActionArgs"
|
11
|
+
|
12
|
+
spec = Gem::Specification.new do |s|
|
13
|
+
s.name = NAME
|
14
|
+
s.version = VERSION
|
15
|
+
s.platform = Gem::Platform::RUBY
|
16
|
+
s.has_rdoc = true
|
17
|
+
s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
|
18
|
+
s.summary = SUMMARY
|
19
|
+
s.description = s.summary
|
20
|
+
s.author = AUTHOR
|
21
|
+
s.email = EMAIL
|
22
|
+
s.homepage = HOMEPAGE
|
23
|
+
s.add_dependency('merb-core', '>= 0.9.2')
|
24
|
+
s.require_path = 'lib'
|
25
|
+
s.autorequire = PLUGIN
|
26
|
+
s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs}/**/*")
|
27
|
+
end
|
28
|
+
|
29
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
30
|
+
pkg.gem_spec = spec
|
31
|
+
end
|
32
|
+
|
33
|
+
task :install => [:package] do
|
34
|
+
sh %{sudo gem install pkg/#{NAME}-#{VERSION} --no-update-sources}
|
35
|
+
end
|
36
|
+
|
37
|
+
namespace :jruby do
|
38
|
+
|
39
|
+
desc "Run :package and install the resulting .gem with jruby"
|
40
|
+
task :install => :package do
|
41
|
+
sh %{#{SUDO} jruby -S gem install pkg/#{NAME}-#{Merb::VERSION}.gem --no-rdoc --no-ri}
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
data/TODO
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
class Merb::AbstractController
|
2
|
+
|
3
|
+
class << self
|
4
|
+
attr_accessor :action_argument_list
|
5
|
+
alias_method :old_inherited, :inherited
|
6
|
+
|
7
|
+
# Stores the argument lists for all methods for this class.
|
8
|
+
#
|
9
|
+
# ==== Parameters
|
10
|
+
# klass<Class>::
|
11
|
+
# The controller that is being inherited from Merb::AbstractController.
|
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
|
15
|
+
end
|
16
|
+
old_inherited(klass)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Calls an action and maps the params hash to the action parameters.
|
21
|
+
#
|
22
|
+
# ==== Parameters
|
23
|
+
# action<Symbol>:: The action to call
|
24
|
+
#
|
25
|
+
# ==== Raises
|
26
|
+
# BadRequest:: The params hash doesn't have a required parameter.
|
27
|
+
def _call_action(action)
|
28
|
+
args = self.class.action_argument_list[action].map do |arg, default|
|
29
|
+
arg = arg
|
30
|
+
p = params.key?(arg.to_sym)
|
31
|
+
raise BadRequest unless p || default
|
32
|
+
p ? params[arg.to_sym] : default
|
33
|
+
end
|
34
|
+
__send__(action, *args)
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'ruby2ruby'
|
2
|
+
|
3
|
+
class ParseTreeArray < Array #:nodoc:
|
4
|
+
def self.translate(*args)
|
5
|
+
self.new(ParseTree.translate(*args))
|
6
|
+
end
|
7
|
+
|
8
|
+
def deep_array_node(type = nil)
|
9
|
+
each do |node|
|
10
|
+
return ParseTreeArray.new(node) if node.is_a?(Array) && (!type || node[0] == type)
|
11
|
+
next unless node.is_a?(Array)
|
12
|
+
return ParseTreeArray.new(node).deep_array_node(type)
|
13
|
+
end
|
14
|
+
nil
|
15
|
+
end
|
16
|
+
|
17
|
+
def arg_nodes
|
18
|
+
self[1..-1].inject([]) do |sum,item|
|
19
|
+
sum << [item] unless item.is_a?(Array)
|
20
|
+
sum
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_args
|
25
|
+
if arg_node = deep_array_node(:args)
|
26
|
+
# method defined with def keyword
|
27
|
+
args = arg_node.arg_nodes
|
28
|
+
default_node = arg_node.deep_array_node(:block)
|
29
|
+
return args unless default_node
|
30
|
+
else
|
31
|
+
# assuming method defined with Module#define_method
|
32
|
+
return []
|
33
|
+
end
|
34
|
+
|
35
|
+
# if it was defined with def, and we found the default_node,
|
36
|
+
# that should bring us back to regularly scheduled programming..
|
37
|
+
|
38
|
+
lasgns = default_node[1..-1]
|
39
|
+
lasgns.each do |asgn|
|
40
|
+
args.assoc(asgn[1]) << eval(RubyToRuby.new.process(asgn[2]))
|
41
|
+
end
|
42
|
+
args
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
# Used in mapping controller arguments to the params hash.
|
48
|
+
# NOTE: You must have the 'ruby2ruby' gem installed for this to work.
|
49
|
+
#
|
50
|
+
# ==== Examples
|
51
|
+
# # In PostsController
|
52
|
+
# def show(id) #=> id is the same as params[:id]
|
53
|
+
module GetArgs
|
54
|
+
|
55
|
+
# ==== Returns
|
56
|
+
# Array:: Method arguments and their default values.
|
57
|
+
#
|
58
|
+
# ==== Examples
|
59
|
+
# class Example
|
60
|
+
# def hello(one,two="two",three)
|
61
|
+
# end
|
62
|
+
#
|
63
|
+
# def goodbye
|
64
|
+
# end
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# Example.instance_method(:hello).get_args
|
68
|
+
# #=> [[:one], [:two, "two"], [:three, "three"]]
|
69
|
+
# Example.instance_method(:goodbye).get_args #=> nil
|
70
|
+
def get_args
|
71
|
+
klass, meth = self.to_s.split(/ /).to_a[1][0..-2].split("#")
|
72
|
+
# Remove stupidity for #<Method: Class(Object)#foo>
|
73
|
+
klass = $` if klass =~ /\(/
|
74
|
+
ParseTreeArray.translate(Object.const_get(klass), meth).get_args
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
class UnboundMethod #:nodoc:
|
79
|
+
include GetArgs
|
80
|
+
end
|
81
|
+
|
82
|
+
class Method #:nodoc:
|
83
|
+
include GetArgs
|
84
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: merb-action-args
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yehuda Katz
|
8
|
+
autorequire: merb-action-args
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-03-24 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: merb-core
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.9.2
|
23
|
+
version:
|
24
|
+
description: Merb plugin that provides support for ActionArgs
|
25
|
+
email: ykatz@engineyard.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- README
|
32
|
+
- LICENSE
|
33
|
+
- TODO
|
34
|
+
files:
|
35
|
+
- LICENSE
|
36
|
+
- README
|
37
|
+
- Rakefile
|
38
|
+
- TODO
|
39
|
+
- lib/merb-action-args
|
40
|
+
- lib/merb-action-args/abstract_controller.rb
|
41
|
+
- lib/merb-action-args/get_args.rb
|
42
|
+
- lib/merb-action-args.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://merb-plugins.rubyforge.org/merb-haml/
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.0.1
|
66
|
+
signing_key:
|
67
|
+
specification_version: 2
|
68
|
+
summary: Merb plugin that provides support for ActionArgs
|
69
|
+
test_files: []
|
70
|
+
|