merb-action-args 1.0.15 → 1.1.0.pre

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,75 +1,66 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), "..", "rake_helpers"))
2
-
3
- ##############################################################################
4
- # Package && release
5
- ##############################################################################
6
- RUBY_FORGE_PROJECT = "merb"
7
- PROJECT_URL = "http://merbivore.com"
8
- PROJECT_SUMMARY = "Merb plugin that provides support for ActionArgs"
9
- PROJECT_DESCRIPTION = PROJECT_SUMMARY
10
-
11
- AUTHOR = "Yehuda Katz"
12
- EMAIL = "ykatz@engineyard.com"
13
-
14
- GEM_NAME = "merb-action-args"
15
- PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
16
- GEM_VERSION = Merb::VERSION + PKG_BUILD
17
-
18
- RELEASE_NAME = "REL #{GEM_VERSION}"
19
-
20
- require "extlib/tasks/release"
21
-
22
- spec = Gem::Specification.new do |s|
23
- s.rubyforge_project = RUBY_FORGE_PROJECT
24
- s.name = GEM_NAME
25
- s.version = GEM_VERSION
26
- s.platform = Gem::Platform::RUBY
27
- s.has_rdoc = true
28
- s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
29
- s.summary = PROJECT_SUMMARY
30
- s.description = PROJECT_DESCRIPTION
31
- s.author = AUTHOR
32
- s.email = EMAIL
33
- s.homepage = PROJECT_URL
34
- s.add_dependency('merb-core', "~> #{Merb::VERSION}")
35
- s.add_dependency('ruby2ruby', '>= 1.1.9')
36
- s.add_dependency('ParseTree', '>= 2.1.1')
37
- s.require_path = 'lib'
38
- s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,spec}/**/*")
39
- end
1
+ require 'rubygems'
2
+ require 'rake'
40
3
 
41
- Rake::GemPackageTask.new(spec) do |pkg|
42
- pkg.gem_spec = spec
43
- end
4
+ # Assume a typical dev checkout to fetch the current merb-core version
5
+ require File.expand_path('../../merb-core/lib/merb-core/version', __FILE__)
44
6
 
45
- desc "Install the gem"
46
- task :install do
47
- Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
48
- end
7
+ # Load this library's version information
8
+ require File.expand_path('../lib/merb-action-args/version', __FILE__)
49
9
 
50
- desc "Uninstall the gem"
51
- task :uninstall do
52
- Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
53
- end
10
+ begin
11
+
12
+ gem 'jeweler', '~> 1.4'
13
+ require 'jeweler'
14
+
15
+ Jeweler::Tasks.new do |gemspec|
16
+
17
+ gemspec.version = Merb::ActionArgs::VERSION
18
+
19
+ gemspec.name = "merb-action-args"
20
+ gemspec.description = "Merb plugin that supports controller action arguments"
21
+ gemspec.summary = "Merb plugin that provides support for named parameters in your controller actions"
22
+
23
+ gemspec.authors = [ "Yehuda Katz" ]
24
+ gemspec.email = "ykatz@engineyard.com"
25
+ gemspec.homepage = "http://merbivore.com/"
26
+
27
+ gemspec.files = %w(LICENSE Rakefile README TODO) + Dir['{lib,spec}/**/*']
28
+
29
+ # Runtime dependencies
30
+ gemspec.add_dependency 'merb-core', "~> #{Merb::VERSION}"
31
+ gemspec.add_dependency 'ruby2ruby', '>= 1.1.9'
32
+ gemspec.add_dependency 'ParseTree', '>= 2.1.1'
33
+
34
+ # Development dependencies
35
+ gemspec.add_development_dependency 'rspec', '>= 1.2.9'
54
36
 
55
- desc "Create a gemspec file"
56
- task :gemspec do
57
- File.open("#{GEM_NAME}.gemspec", "w") do |file|
58
- file.puts spec.to_ruby
59
37
  end
38
+
39
+ Jeweler::GemcutterTasks.new
40
+
41
+ rescue LoadError
42
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
60
43
  end
61
44
 
62
- desc "Run all examples (or a specific spec with TASK=xxxx)"
63
- Spec::Rake::SpecTask.new('spec') do |t|
64
- t.spec_opts = ["-cfs"]
65
- t.spec_files = begin
66
- if ENV["TASK"]
67
- ENV["TASK"].split(',').map { |task| "spec/**/#{task}_spec.rb" }
68
- else
69
- FileList['spec/**/*_spec.rb']
70
- end
71
- end
45
+ require 'spec/rake/spectask'
46
+ Spec::Rake::SpecTask.new(:spec) do |spec|
47
+ spec.spec_opts << '--options' << 'spec/spec.opts' if File.exists?('spec/spec.opts')
48
+ spec.libs << 'lib' << 'spec'
49
+ spec.spec_files = FileList['spec/**/*_spec.rb']
50
+ end
51
+
52
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
53
+ spec.libs << 'lib' << 'spec'
54
+ spec.pattern = 'spec/**/*_spec.rb'
55
+ spec.rcov = true
72
56
  end
73
57
 
74
- desc 'Default: run spec examples'
75
- task :default => 'spec'
58
+ task :default => :spec
59
+
60
+ require 'rake/rdoctask'
61
+ Rake::RDocTask.new do |rdoc|
62
+ rdoc.rdoc_dir = 'rdoc'
63
+ rdoc.title = "test_gem #{Merb::ActionArgs::VERSION}"
64
+ rdoc.rdoc_files.include('README*')
65
+ rdoc.rdoc_files.include('lib/**/*.rb')
66
+ end
@@ -41,4 +41,4 @@ class Merb::AbstractController
41
41
  end
42
42
  __send__(action, *args)
43
43
  end
44
- end
44
+ end
@@ -1,94 +1,9 @@
1
- unless RUBY_PLATFORM == "java"
2
- require 'parse_tree'
3
- require 'ruby2ruby'
4
-
5
- class ParseTreeArray < Array
6
- R2R = Object.const_defined?(:Ruby2Ruby) ? Ruby2Ruby : RubyToRuby
7
-
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
17
- end
18
- sexp = Unifier.new.process(sexp)
19
- self.new(sexp)
20
- end
21
-
22
- def deep_array_node(type = nil)
23
- each do |node|
24
- return ParseTreeArray.new(node) if node.is_a?(Array) && (!type || node[0] == type)
25
- next unless node.is_a?(Array)
26
- return ParseTreeArray.new(node).deep_array_node(type)
27
- end
28
- nil
29
- end
30
-
31
- def arg_nodes
32
- self[1..-1].inject([]) do |sum,item|
33
- sum << [item] unless item.is_a?(Array)
34
- sum
35
- end
36
- end
37
-
38
- def get_args
39
- if arg_node = deep_array_node(:args)
40
- # method defined with def keyword
41
- args = arg_node.arg_nodes
42
- default_node = arg_node.deep_array_node(:block)
43
- return [args, []] unless default_node
44
- else
45
- # assuming method defined with Module#define_method
46
- return [[],[]]
47
- end
48
-
49
- # if it was defined with def, and we found the default_node,
50
- # that should bring us back to regularly scheduled programming..
51
- lasgns = default_node[1..-1]
52
- lasgns.each do |asgn|
53
- args.assoc(asgn[1]) << eval(R2R.new.process(asgn[2]))
54
- end
55
- [args, (default_node[1..-1].map { |asgn| asgn[1] })]
56
- end
57
-
58
- end
59
-
60
- # Used in mapping controller arguments to the params hash.
61
- # NOTE: You must have the 'ruby2ruby' gem installed for this to work.
62
- #
63
- # ==== Examples
64
- # # In PostsController
65
- # def show(id) #=> id is the same as params[:id]
66
- module GetArgs
67
-
68
- # ==== Returns
69
- # Array:: Method arguments and their default values.
70
- #
71
- # ==== Examples
72
- # class Example
73
- # def hello(one,two="two",three)
74
- # end
75
- #
76
- # def goodbye
77
- # end
78
- # end
79
- #
80
- # Example.instance_method(:hello).get_args
81
- # #=> [[:one], [:two, "two"], [:three, "three"]]
82
- # Example.instance_method(:goodbye).get_args #=> nil
83
- def get_args
84
- klass, meth = self.to_s.split(/ /).to_a[1][0..-2].split("#")
85
- # Remove stupidity for #<Method: Class(Object)#foo>
86
- klass = $` if klass =~ /\(/
87
- ParseTreeArray.translate(Object.full_const_get(klass), meth).get_args
88
- end
89
- end
90
- else
1
+ if RUBY_PLATFORM == "java"
91
2
  require File.dirname(__FILE__) / "jruby_args"
3
+ elsif RUBY_VERSION < "1.9"
4
+ require File.dirname(__FILE__) / "mri_args"
5
+ else
6
+ require File.dirname(__FILE__) / "vm_args"
92
7
  end
93
8
 
94
9
  class UnboundMethod
@@ -97,4 +12,4 @@ end
97
12
 
98
13
  class Method
99
14
  include GetArgs
100
- end
15
+ end
@@ -29,12 +29,12 @@ module GetArgs
29
29
 
30
30
  # required args
31
31
  if (args_node.args && args_node.args.size > 0)
32
- required << args_node.args.child_nodes.map { |arg| [arg.name.to_s.intern] }
32
+ required = args_node.args.child_nodes.map { |arg| [arg.name.to_s.intern] }
33
33
  end
34
34
 
35
35
  # optional args
36
36
  if (args_node.opt_args && args_node.opt_args.size > 0)
37
- optional << args_node.opt_args.child_nodes.map do |arg|
37
+ optional = args_node.opt_args.child_nodes.map do |arg|
38
38
  name = arg.name.to_s.intern
39
39
  value_node = arg.value_node
40
40
  case value_node
@@ -51,17 +51,7 @@ module GetArgs
51
51
  end
52
52
  end
53
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
54
+ args = required + optional
55
+ return [args, optional.map{|name,| name}]
66
56
  end
67
- end
57
+ end
@@ -0,0 +1,88 @@
1
+ require 'parse_tree'
2
+ require 'ruby2ruby'
3
+
4
+ class ParseTreeArray < Array
5
+ R2R = Object.const_defined?(:Ruby2Ruby) ? Ruby2Ruby : RubyToRuby
6
+
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)
14
+ end
15
+ sexp = ParseTree.translate(klass, args.last) if klass
16
+ end
17
+ sexp = Unifier.new.process(sexp)
18
+ self.new(sexp)
19
+ end
20
+
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
28
+ end
29
+
30
+ def arg_nodes
31
+ self[1..-1].inject([]) do |sum,item|
32
+ sum << [item] unless item.is_a?(Array)
33
+ sum
34
+ end
35
+ end
36
+
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
47
+
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] })]
55
+ end
56
+
57
+ end
58
+
59
+ # Used in mapping controller arguments to the params hash.
60
+ # NOTE: You must have the 'ruby2ruby' gem installed for this to work.
61
+ #
62
+ # ==== Examples
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.full_const_get(klass), meth).get_args
87
+ end
88
+ end
@@ -0,0 +1,5 @@
1
+ module Merb
2
+ module ActionArgs
3
+ VERSION = '1.1.0.pre'.freeze
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ begin
2
+ require "methopara"
3
+ rescue
4
+ puts "Make sure you have methopara http://github.com/genki/methopara installed if you want to use action args on Ruby 1.9"
5
+ end
6
+
7
+ module GetArgs
8
+ def get_args
9
+ unless respond_to?(:parameters)
10
+ raise NotImplementedError, "Ruby #{RUBY_VERSION} doesn't support #{self.class}#parameters"
11
+ end
12
+
13
+ required = []
14
+ optional = []
15
+
16
+ parameters.each do |(type, name)|
17
+ if type == :opt
18
+ required << [name, nil]
19
+ optional << name
20
+ else
21
+ required << [name]
22
+ end
23
+ end
24
+
25
+ return [required, optional]
26
+ end
27
+ end
@@ -1,11 +1,23 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
1
+ require 'spec_helper'
2
2
 
3
3
  describe "ActionArgs" do
4
4
 
5
5
  it "should not accidently introduce any methods as controller actions" do
6
6
  Merb::Controller.callable_actions.should be_empty
7
7
  end
8
-
8
+
9
+ it "should extract explicit one arg" do
10
+ ActionArgs.instance_method(:index).get_args.should == [[[:foo]], []]
11
+ end
12
+
13
+ it "should extract explicit multi args" do
14
+ ActionArgs.instance_method(:multi).get_args.should == [[[:foo], [:bar]], []]
15
+ end
16
+
17
+ it "should extract optional args" do
18
+ ActionArgs.instance_method(:with_default_nil).get_args.should == [[[:foo], [:bar, nil]], [:bar]]
19
+ end
20
+
9
21
  end
10
22
 
11
23
  describe Merb::AbstractController do
@@ -64,4 +76,4 @@ describe Merb::AbstractController do
64
76
  Merb::ControllerExceptions::BadRequest, /were missing bar/)
65
77
  end
66
78
 
67
- end
79
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --format specdoc
2
+ --colour
data/spec/spec_helper.rb CHANGED
@@ -1,10 +1,18 @@
1
- $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
-
3
1
  require "rubygems"
4
- require "merb-core"
2
+
3
+ # Use current merb-core sources if running from a typical dev checkout.
4
+ lib = File.expand_path('../../../merb-core/lib', __FILE__)
5
+ $LOAD_PATH.unshift(lib) if File.directory?(lib)
6
+ require 'merb-core'
7
+
8
+ # The lib under test
5
9
  require "merb-action-args"
6
- require File.dirname(__FILE__) / "controllers" / "action-args"
7
- require "spec"
10
+
11
+ # Satisfies Autotest and anyone else not using the Rake tasks
12
+ require 'spec'
13
+
14
+ # Additional files required for specs
15
+ require "controllers/action-args"
8
16
 
9
17
  Merb.start :environment => 'test'
10
18
 
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: 1.0.15
4
+ version: 1.1.0.pre
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: 2009-11-04 00:00:00 +00:00
12
+ date: 2010-02-20 00:00:00 +00: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: 1.0.15
23
+ version: 1.1.0.pre
24
24
  version:
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: ruby2ruby
@@ -42,35 +42,49 @@ dependencies:
42
42
  - !ruby/object:Gem::Version
43
43
  version: 2.1.1
44
44
  version:
45
- description: Merb plugin that provides support for ActionArgs
45
+ - !ruby/object:Gem::Dependency
46
+ name: rspec
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 1.2.9
54
+ version:
55
+ description: Merb plugin that supports controller action arguments
46
56
  email: ykatz@engineyard.com
47
57
  executables: []
48
58
 
49
59
  extensions: []
50
60
 
51
61
  extra_rdoc_files:
52
- - README
53
62
  - LICENSE
63
+ - README
54
64
  - TODO
55
65
  files:
56
66
  - LICENSE
57
67
  - README
58
68
  - Rakefile
59
69
  - TODO
70
+ - lib/merb-action-args.rb
60
71
  - lib/merb-action-args/abstract_controller.rb
61
72
  - lib/merb-action-args/get_args.rb
62
73
  - lib/merb-action-args/jruby_args.rb
63
- - lib/merb-action-args.rb
74
+ - lib/merb-action-args/mri_args.rb
75
+ - lib/merb-action-args/version.rb
76
+ - lib/merb-action-args/vm_args.rb
64
77
  - spec/action_args_spec.rb
65
78
  - spec/controllers/action-args.rb
79
+ - spec/spec.opts
66
80
  - spec/spec_helper.rb
67
81
  has_rdoc: true
68
- homepage: http://merbivore.com
82
+ homepage: http://merbivore.com/
69
83
  licenses: []
70
84
 
71
85
  post_install_message:
72
- rdoc_options: []
73
-
86
+ rdoc_options:
87
+ - --charset=UTF-8
74
88
  require_paths:
75
89
  - lib
76
90
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -81,16 +95,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
81
95
  version:
82
96
  required_rubygems_version: !ruby/object:Gem::Requirement
83
97
  requirements:
84
- - - ">="
98
+ - - ">"
85
99
  - !ruby/object:Gem::Version
86
- version: "0"
100
+ version: 1.3.1
87
101
  version:
88
102
  requirements: []
89
103
 
90
- rubyforge_project: merb
104
+ rubyforge_project:
91
105
  rubygems_version: 1.3.5
92
106
  signing_key:
93
107
  specification_version: 3
94
- summary: Merb plugin that provides support for ActionArgs
108
+ summary: Merb plugin that provides support for named parameters in your controller actions
95
109
  test_files: []
96
110