get_args 1.0.0 → 1.1.0

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/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
data/get_args.gemspec ADDED
@@ -0,0 +1,62 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{get_args}
8
+ s.version = "1.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["maiha", "Yehuda Katz"]
12
+ s.date = %q{2009-12-11}
13
+ s.description = %q{Extracted from Merb, get_args allows you to query a method for its argument names and defaults.
14
+
15
+ This gem exists so you can get this functionality without having to include all of Merb.}
16
+ s.email = %q{crnixon@gmail.com}
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".gitignore",
24
+ "LICENSE",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "get_args.gemspec",
29
+ "lib/argument_list.rb",
30
+ "lib/get_args.rb",
31
+ "lib/jruby_args.rb",
32
+ "lib/mri_args.rb",
33
+ "lib/vm_args.rb",
34
+ "test/helper.rb",
35
+ "test/test_argument_list.rb",
36
+ "test/test_get_args.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/crnixon/get_args}
39
+ s.rdoc_options = ["--charset=UTF-8"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.3.5}
42
+ s.summary = %q{Allows one to introspect on the argument names and defaults for a method.}
43
+ s.test_files = [
44
+ "test/helper.rb",
45
+ "test/test_argument_list.rb",
46
+ "test/test_get_args.rb"
47
+ ]
48
+
49
+ if s.respond_to? :specification_version then
50
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
54
+ s.add_runtime_dependency(%q<extlib>, [">= 0.9.13"])
55
+ else
56
+ s.add_dependency(%q<extlib>, [">= 0.9.13"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<extlib>, [">= 0.9.13"])
60
+ end
61
+ end
62
+
@@ -0,0 +1,28 @@
1
+ module GetArgs
2
+ class ArgumentList
3
+ def initialize(args_array)
4
+ @arguments = args_array.map { |arg| Argument.new(arg) }
5
+ end
6
+
7
+ def method_missing(*args)
8
+ @arguments.send(*args)
9
+ end
10
+ end
11
+
12
+ class Argument
13
+ attr_reader :name, :default
14
+
15
+ def initialize(arg_array)
16
+ @optional = arg_array.length > 1
17
+ @name, @default = *arg_array
18
+ end
19
+
20
+ def optional?
21
+ @optional
22
+ end
23
+
24
+ def multiple?
25
+ name.to_s.slice(0,1) == '*'
26
+ end
27
+ end
28
+ end
data/lib/get_args.rb CHANGED
@@ -9,6 +9,13 @@ elsif RUBY_VERSION < "1.9"
9
9
  else
10
10
  require 'vm_args'
11
11
  end
12
+ require 'argument_list'
13
+
14
+ module GetArgs
15
+ def get_argument_list
16
+ ArgumentList.new(get_args.first)
17
+ end
18
+ end
12
19
 
13
20
  class UnboundMethod
14
21
  include GetArgs
@@ -0,0 +1,39 @@
1
+ require 'helper'
2
+
3
+ class Dummy
4
+ def one_arg(foo); end
5
+ def multiple_args(foo, bar, baz); end
6
+ def optional_args(foo, bar = 1, baz = nil); end
7
+ def star_args(*foo); end
8
+ end
9
+
10
+ class TestArgumentList < Test::Unit::TestCase
11
+ def test_one_arg
12
+ list = Dummy.instance_method(:one_arg).get_argument_list
13
+ assert_equal 1, list.length
14
+ assert_equal :foo, list.first.name
15
+ assert !list.first.optional?
16
+ end
17
+
18
+ def test_multiple_args
19
+ list = Dummy.instance_method(:multiple_args).get_argument_list
20
+ assert_equal 3, list.length
21
+ end
22
+
23
+ def test_optional_args
24
+ list = Dummy.instance_method(:optional_args).get_argument_list
25
+
26
+ assert !list[0].optional?
27
+ assert list[1].optional?
28
+ assert_equal 1, list[1].default
29
+ assert list[2].optional?
30
+ assert_equal nil, list[2].default
31
+ end
32
+
33
+ def test_star_args
34
+ list = Dummy.instance_method(:star_args).get_argument_list
35
+
36
+ assert list.first.multiple?
37
+ end
38
+
39
+ end
@@ -3,7 +3,7 @@ require 'helper'
3
3
  class Dummy
4
4
  def one_arg(foo); end
5
5
  def multiple_args(foo, bar, baz); end
6
- def optional_args(foo, bar = 1); end
6
+ def optional_args(foo, bar = 1, baz = nil); end
7
7
  def star_args(*foo); end
8
8
  end
9
9
 
@@ -11,7 +11,7 @@ class TestGetArgs < Test::Unit::TestCase
11
11
  def test_get_args
12
12
  assert_equal [[[:foo]], []], Dummy.instance_method(:one_arg).get_args
13
13
  assert_equal [[[:foo], [:bar], [:baz]], []], Dummy.instance_method(:multiple_args).get_args
14
- assert_equal [[[:foo], [:bar, 1]], [:bar]], Dummy.instance_method(:optional_args).get_args
14
+ assert_equal [[[:foo], [:bar, 1], [:baz, nil]], [:bar, :baz]], Dummy.instance_method(:optional_args).get_args
15
15
  assert_equal [[[%s[*foo]]], []], Dummy.instance_method(:star_args).get_args
16
16
  end
17
17
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: get_args
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - maiha
@@ -42,11 +42,14 @@ files:
42
42
  - README.rdoc
43
43
  - Rakefile
44
44
  - VERSION
45
+ - get_args.gemspec
46
+ - lib/argument_list.rb
45
47
  - lib/get_args.rb
46
48
  - lib/jruby_args.rb
47
49
  - lib/mri_args.rb
48
50
  - lib/vm_args.rb
49
51
  - test/helper.rb
52
+ - test/test_argument_list.rb
50
53
  - test/test_get_args.rb
51
54
  has_rdoc: true
52
55
  homepage: http://github.com/crnixon/get_args
@@ -78,4 +81,5 @@ specification_version: 3
78
81
  summary: Allows one to introspect on the argument names and defaults for a method.
79
82
  test_files:
80
83
  - test/helper.rb
84
+ - test/test_argument_list.rb
81
85
  - test/test_get_args.rb