rogerdpack-arguments 0.4.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,13 @@
1
+ == 0.4 2009-06-30
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
5
+ * Works with Ruby 1.8.6 and 1.9.1
6
+
7
+ == 0.4.2 2009-07-03
8
+
9
+ * Fixed a Bug where not passing arguments would rise try to call #key? on nil
10
+
11
+ == 0.4.3 2009-07-07
12
+
13
+ * Fixed a serious Bug where default values could not be overriden by passing arguments without keyword
data/Manifest.txt ADDED
@@ -0,0 +1,10 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ lib/arguments.rb
6
+ lib/arguments/class.rb
7
+ lib/arguments/mri.rb
8
+ lib/arguments/vm.rb
9
+ spec/arguments_spec.rb
10
+ spec/klass.rb
data/README.rdoc ADDED
@@ -0,0 +1,85 @@
1
+ = arguments
2
+
3
+ Keyword arguments support now!
4
+
5
+ == DESCRIPTION:
6
+
7
+ You don't have to wait until Ruby 2.0 to get (named|keyword) arguments support.
8
+ Arguments has been tested with Ruby 1.8.6 and ruby 1.9.1 and eventually will work with JRuby (if someone is interested in contributing, I guess is possible since merb-action-args works with JRuby)
9
+
10
+ == SYNOPSIS:
11
+
12
+ require 'arguments'
13
+
14
+ class Example
15
+ def meth(a = :a, b = :b, c = :c)
16
+ [a,b,c]
17
+ end
18
+
19
+ named_args_for :meth
20
+
21
+ class << self
22
+ def class_method(a = :a, b = :b, c = :c)
23
+ [a,b,c]
24
+ end
25
+ named_args_for :class_method
26
+ end
27
+ end
28
+
29
+ nu = Example.new
30
+ nu.meth #=> [:a,:b,:c]
31
+ nu.meth(1, :c => Class) #=> [1,:b,Class]
32
+ nu.meth(:b => nil, :a => 'something') #=> ['something', nil, :c]
33
+
34
+ Example.class_method(:b => nil, :a => 'something') #=> ['something', nil, :c]
35
+
36
+
37
+ Note: Keyword argument take precedence over argument order:
38
+
39
+ nu.meth(10, :a => -10) #=> [-10, :b, :c]
40
+
41
+ == LIMITATIONS
42
+
43
+ * Performance penalty occurs only in 1.8.6 due to ParseTree use and only while calling Class#named_args\_for, penalty while calling the actuall method is neglectable.
44
+ * With Ruby 1.8.6 it can patch methods declared with eval, with 1.9.1 only methods declared in a source file.
45
+ * If last argument is a Hash is taken as an options Hash and not assigned to the argument varible, if you want to pass a has as a last argument use keywords.
46
+
47
+ == TODO
48
+
49
+ Don't use last argument (Hash) as options if passed total number of args.
50
+
51
+ == REQUIREMENTS:
52
+
53
+ Ruby 1.8.6:
54
+
55
+ - ParseTree >= 3.0.3
56
+ - Ruby2Ruby 1.1.9
57
+
58
+ == INSTALL:
59
+
60
+ sudo gem install maca-arguments –source http://gems.github.com
61
+
62
+ == LICENSE:
63
+
64
+ (The MIT License)
65
+
66
+ Copyright (c) 2009 Macario Ortega
67
+
68
+ Permission is hereby granted, free of charge, to any person obtaining
69
+ a copy of this software and associated documentation files (the
70
+ 'Software'), to deal in the Software without restriction, including
71
+ without limitation the rights to use, copy, modify, merge, publish,
72
+ distribute, sublicense, and/or sell copies of the Software, and to
73
+ permit persons to whom the Software is furnished to do so, subject to
74
+ the following conditions:
75
+
76
+ The above copyright notice and this permission notice shall be
77
+ included in all copies or substantial portions of the Software.
78
+
79
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
80
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
81
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
82
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
83
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
84
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
85
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen hoe].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/arguments'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.new('arguments', Arguments::VERSION) do |p|
7
+ p.developer('Macario Ortega', 'macarui@gmail.com')
8
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
+ # p.rubyforge_name = p.name # TODO this is default value
10
+
11
+ p.extra_deps = [
12
+ ['ParseTree','>= 3.0.3'],
13
+ ['ruby2ruby', '= 1.1.9']
14
+ ]
15
+
16
+ p.extra_dev_deps = [
17
+ ['newgem', ">= #{::Newgem::VERSION}"]
18
+ ]
19
+
20
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
21
+ # path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
22
+ # p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
23
+ p.rsync_args = '-av --delete --ignore-errors'
24
+ end
25
+
26
+ require 'newgem/tasks' # load /tasks/*.rake
27
+ Dir['tasks/**/*.rake'].each { |t| load t }
28
+
29
+ # TODO - want other tests/tasks run by default? Add them to the list
30
+ # task :default => [:spec, :features]
data/lib/arguments.rb ADDED
@@ -0,0 +1,8 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
2
+
3
+ require 'arguments/class'
4
+ RUBY_VERSION.to_f >= 1.9 ? require( 'arguments/vm' ) : require( 'arguments/mri' )
5
+
6
+ module Arguments
7
+ VERSION = '0.4.3'
8
+ end
@@ -0,0 +1,50 @@
1
+ # used to throw if you try to call it on a binary method
2
+ class BinaryMethodError < Exception; end
3
+
4
+ class Class
5
+
6
+ def named_arguments_for *methods
7
+ methods.each do |method|
8
+ names = Arguments.names self, method
9
+ raise BinaryMethodError.new("tried to call it on a Binary Method" + method.to_s) unless names
10
+ assigns = []
11
+
12
+ names.each_with_index do |name, index|
13
+ unless name.size == 1
14
+ # optionals
15
+ assigns << "#{ name.first } = opts.key?(:#{ name.first }) ? opts[:#{ name.first }] : args.fetch(#{ index },#{ name.last })"
16
+ assigns << <<-RUBY_EVAL
17
+ #{ name.first } =
18
+ if opts.key?(:#{ name.first })
19
+ opts[:#{ name.first }]
20
+ else
21
+ args.size >= #{ index + 1 } ? args[#{ index }] : #{ name.last }
22
+ end
23
+ RUBY_EVAL
24
+ else
25
+ # requireds
26
+ assigns << <<-RUBY_EVAL
27
+ begin
28
+ #{ name.first } = opts.key?(:#{ name.first }) ? opts[:#{ name.first }] : args.fetch(#{ index })
29
+ rescue
30
+ raise ArgumentError.new('passing `#{ name.first }` is required')
31
+ end
32
+ RUBY_EVAL
33
+ end
34
+ end
35
+
36
+ self.module_eval <<-RUBY_EVAL, __FILE__, __LINE__
37
+ def __new_#{ method } *args, &block
38
+ opts = args.last.kind_of?( Hash ) ? args.pop : {}
39
+ #{ assigns.join("\n") }
40
+ __original_#{ method } #{ names.collect{ |n| n.first }.join(', ') }, &block
41
+ end
42
+
43
+ alias __original_#{ method } #{ method }
44
+ alias #{ method } __new_#{ method }
45
+ RUBY_EVAL
46
+ end
47
+ end
48
+ alias :named_args_for :named_arguments_for
49
+
50
+ end
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ gem 'ParseTree', '>= 3.0.3'
3
+ require 'parse_tree'
4
+
5
+ gem 'ruby2ruby', '= 1.1.9'
6
+ require 'ruby2ruby'
7
+
8
+ module Arguments
9
+ def self.names klass, method
10
+ begin
11
+ args = ParseTree.translate( klass, method ).assoc(:scope).assoc(:block).assoc(:args)[1..-1]
12
+ rescue NoMethodError # binary method will fail on one of those assoc's
13
+ return nil
14
+ end
15
+ if args.last.instance_of? Array
16
+ vals = args.pop[1..-1]
17
+ else
18
+ # if it has no optionals
19
+ vals = []
20
+ end
21
+
22
+ args.collect do |arg|
23
+ if val = vals.find{ |v| v[1] == arg }
24
+ [arg, Ruby2Ruby.new.process(val.last)]
25
+ else
26
+ [arg]
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,8 @@
1
+
2
+ module Arguments
3
+ def self.names klass, method
4
+ source, line = klass.instance_method( method ).source_location
5
+ IO.readlines(source)[ line - 1 ].match(/def\s*\w+\s*\(?\s*([^)\n]*)/)[1] #This could look better
6
+ .scan(/(\w+)(?:\s*=\s*([^,]+))|(\w+)/).map{ |e| e.compact }
7
+ end
8
+ end
@@ -0,0 +1,92 @@
1
+ require "#{ File.dirname __FILE__ }/../lib/arguments"
2
+ require 'benchmark'
3
+
4
+ describe Arguments do
5
+
6
+ before do
7
+ Object.send(:remove_const, 'Klass') rescue nil
8
+ load "#{ File.dirname __FILE__ }/klass.rb"
9
+ @instance = Klass.new
10
+ end
11
+
12
+ it "should respond to named_arguments" do
13
+ Klass.send( :named_arguments_for, :three )
14
+ end
15
+
16
+ it "should not respond to named_arguments" do
17
+ lambda { Klass.new.send( :named_arguments_for ) }.should raise_error( NoMethodError )
18
+ end
19
+
20
+ it "should get argument names with literals" do
21
+ Arguments.names( Klass, :two ).collect{ |a, b| [a.to_sym, b].compact }.
22
+ should == [[:one], [:two, "2"], [:three, "Klass.new"]]
23
+ end
24
+
25
+ it "shouldn't break defaults" do
26
+ @instance.two(1).should == [1, 2, Klass.new]
27
+ end
28
+
29
+ it "should allow passing named argument" do
30
+ Klass.send( :named_arguments_for, :two )
31
+ @instance.two(1, :three => 3).should == [1, 2, 3]
32
+ end
33
+
34
+ it "should raise ArgumentError if not passing required params" do
35
+ Klass.send( :named_arguments_for, :two )
36
+ lambda { @instance.two( :three => 3 ) }.should raise_error(ArgumentError)
37
+ end
38
+
39
+ it "should override passed value with hash" do
40
+ Klass.send( :named_arguments_for, :two )
41
+ @instance.two( :one => nil ).should == [nil, 2, Klass.new]
42
+ end
43
+
44
+ it "should allow overriding with nil" do
45
+ Klass.send( :named_arguments_for, :two )
46
+ @instance.two( 1, :three => nil ).should == [1, 2, nil]
47
+ end
48
+
49
+ it "should pass block" do
50
+ Klass.send( :named_arguments_for, :with_block )
51
+ @instance.with_block( 1, :three => nil, :two => 'something' ){ :block }.should == [1, 'something', nil, :block]
52
+ end
53
+
54
+ it "should override defaults on standard passing" do
55
+ Klass.send( :named_arguments_for, :asr )
56
+ @instance.asr(0, 1, 2, :curve => 3).should == [0,1,2,3]
57
+ end
58
+
59
+ it "should work with methods that have no optionals" do
60
+ Klass.send( :named_arguments_for, :go5 )
61
+ @instance.go5(1, 2).should == [1,2]
62
+ @instance.go5(1, :b => 3).should == [1,3]
63
+ @instance.go5(:a => 4, :b => 5).should == [4, 5]
64
+ end
65
+
66
+ it "should work with class methods" do
67
+ Klass.asr(0, 1, 2, :curve => 3).should == [0,1,2,3]
68
+ end
69
+
70
+ it "should benchmark without hack" do
71
+ puts Benchmark.measure {
72
+ 1_000.times do
73
+ @instance.with_block( 1, :three => nil ){ :block }
74
+ end
75
+ }
76
+ end
77
+
78
+ it "should raise if you try to call it on a c (binary) method" do
79
+ lambda { class String; named_arguments_for(:strip); end }.should raise_error( BinaryMethodError )
80
+ end
81
+
82
+ it "should benchmark with hack" do
83
+ puts Benchmark.measure {
84
+ Klass.send( :named_arguments_for, :with_block )
85
+ }
86
+ puts Benchmark.measure {
87
+ 1_000.times do
88
+ @instance.with_block( 1, :three => nil ){ :block }
89
+ end
90
+ }
91
+ end
92
+ end
data/spec/klass.rb ADDED
@@ -0,0 +1,33 @@
1
+ class Klass
2
+ def three one = 1, two = 2, three = 3
3
+ [one, two, three]
4
+ end
5
+
6
+ def two one, two = 2, three = Klass.new
7
+ [one, two, three]
8
+ end
9
+
10
+ def with_block one, two = 2, three = 3
11
+ [one, two, three, yield]
12
+ end
13
+
14
+
15
+ def go5(a, b)
16
+ [a, b]
17
+ end
18
+
19
+ def asr attackTime = 3, sustainLevel = 2, releaseTime = 1, curve = 0
20
+ [attackTime, sustainLevel, releaseTime, curve]
21
+ end
22
+
23
+ class << self
24
+ def asr attackTime = 3, sustainLevel = 2, releaseTime = 1, curve = 0
25
+ [attackTime, sustainLevel, releaseTime, curve]
26
+ end
27
+ named_arguments_for :asr
28
+ end
29
+
30
+ def == other
31
+ self.class == other.class
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rogerdpack-arguments
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.3.2
5
+ platform: ruby
6
+ authors:
7
+ - Macario Ortega
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-03 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: ParseTree
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 3.0.3
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: ruby2ruby
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.9
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: newgem
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.4.1
44
+ version:
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.7
54
+ version:
55
+ - !ruby/object:Gem::Dependency
56
+ name: hoe
57
+ type: :development
58
+ version_requirement:
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 1.8.0
64
+ version:
65
+ description: You don't have to wait until Ruby 2.0 to get (named|keyword) arguments support. Arguments has been tested with Ruby 1.8.6 and ruby 1.9.1 and eventually will work with JRuby
66
+ email:
67
+ - macarui@gmail.com
68
+ executables: []
69
+
70
+ extensions: []
71
+
72
+ extra_rdoc_files:
73
+ - History.txt
74
+ - Manifest.txt
75
+ - README.rdoc
76
+ files:
77
+ - History.txt
78
+ - Manifest.txt
79
+ - README.rdoc
80
+ - Rakefile
81
+ - lib/arguments.rb
82
+ - lib/arguments/class.rb
83
+ - lib/arguments/mri.rb
84
+ - lib/arguments/vm.rb
85
+ - spec/arguments_spec.rb
86
+ - spec/klass.rb
87
+ has_rdoc: true
88
+ homepage:
89
+ post_install_message:
90
+ rdoc_options:
91
+ - --main
92
+ - README.rdoc
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: "0"
100
+ version:
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: "0"
106
+ version:
107
+ requirements: []
108
+
109
+ rubyforge_project: arguments
110
+ rubygems_version: 1.2.0
111
+ signing_key:
112
+ specification_version: 2
113
+ summary: You don't have to wait until Ruby 2.0 to get (named|keyword) arguments support
114
+ test_files: []
115
+