Permalink
Browse files

moved named arguments into a gem

  • Loading branch information...
1 parent 2d5071d commit 9f3e942385f91d509420bcfcdd5b92628f525149 Macario committed Oct 27, 2008
Showing with 1 addition and 306 deletions.
  1. +1 −0 .gitignore
  2. +0 −72 lib/scruby/named_args.rb
  3. +0 −234 spec/named_args_specs.rb
View
@@ -0,0 +1 @@
+.DS_Store
@@ -1,72 +0,0 @@
-require 'rubygems'
-require 'ruby2ruby'
-
-
-module NamedArgs
-
- module ExtendMethods
- def named_args_for( *methods )
- methods.each do | m |
- unbound = instance_method( m )
- defaults = NamedArgs.assign_defaults_string( unbound )
-
- args = unbound.arg_names.join(',')
-
- hacked_method = "def #{m}_hacked( *args );" +
- unless args.empty?
- "opts = args.pop if args.last.instance_of?(Hash);" + args + "=args;" + "eval(NamedArgs.assign_options_string(opts));" + defaults
- else
- ''
- end +
- "#{m}_original(" + args + ");" +
- "end"
-
- self.class_eval hacked_method
-
- alias_method "#{m}_original", m
- alias_method m, "#{m}_hacked"
- end
- end
- end
-
- class << self
- def included( klass )
- klass.send( :extend, ExtendMethods )
- end
-
- def assign_options_string( hash )
- assign_opts = [:block] + hash.to_a.collect{ |e| [:lasgn, e.first, [:lit, e.last]] }
- Ruby2Ruby.new.process assign_opts
- end
-
- def assign_defaults_string( method )
- args, assigns = method.args.partition{ |t| t.instance_of?(Symbol) }
- assigns = *assigns
- return '' unless assigns
- assign_block = [:block] + assigns.select{ |e| e.instance_of?(Array) }.collect{ |e| [:if, [:lvar, e[1]], nil, e] }
- Ruby2Ruby.new.process assign_block
- end
- end
-
-
-end
-
-class UnboundMethod
- def to_sexp
- name = ProcStoreTmp.new_name
- ProcStoreTmp.send(:define_method, name, self)
- ProcStoreTmp.new.method(name).to_sexp
- end
-
- def args
- self.to_sexp.assoc(:dmethod).assoc(:scope).assoc(:block).assoc(:args)[1..-1]
- end
-
- def arg_names
- self.args.select{ |i| i.instance_of?(Symbol) }
- end
-end
-
-class Object
- include NamedArgs
-end
@@ -1,234 +0,0 @@
-require File.join( File.expand_path(File.dirname(__FILE__)),"helper")
-require "#{LIB_DIR}/named_args"
-require 'benchmark'
-
-
-class A
- def test(uno = 1, dos = 2, tres = 3, cuatro = 4, cinco = 5, seis = 6, siete = 7, ocho = 8, nueve = 9, diez = 10, once = 11, doce = 12, trece = 13, catorce = 14, quince = 15, dieciseis = 16)
- end
-end
-
-class B
- def test(uno = 1, dos = 2, tres = 3, cuatro = 4, cinco = 5, seis = 6, siete = 7, ocho = 8, nueve = 9, diez = 10, once = 11, doce = 12, trece = 13, catorce = 14, quince = 15, dieciseis = 16)
- end
- named_args_for :test
-end
-
-describe NamedArgs do
-
- before :all do
- Klass = nil
- end
-
- before :each do
- Object.send(:remove_const, 'Klass')
- class Klass;
- def zero
- end
-
- def three( uno = 1, dos = 2, tres = 3 )
- end
-
- def six( uno = 1, dos = 2, tres = 3, cuatro = 4, cinco = 5, seis = 6)
- end
-
- class << self
- def dos( uno = 1, dos = 2)
- end
- named_args_for :dos
- end
- end
- @unbound = mock('unbound', :args => [], :arg_names => [] )
- end
-
- describe 'inclusion' do
-
- it 'should include Named args' do
- # Klass.send( :include, NamedArgs )
- Klass.included_modules.should include(NamedArgs)
- end
-
- it "should extend ExtendMethods" do
- Klass.should_receive(:extend).with(NamedArgs::ExtendMethods)
- Klass.send( :include, NamedArgs )
- end
-
- it do
- Klass.send( :include, NamedArgs )
- Klass.should respond_to(:named_args_for)
- end
-
- it 'should receive #instance_method with :three' do
- Klass.should_receive( :instance_method ).with( :three ).and_return( @unbound )
- Klass.send( :include, NamedArgs )
- Klass.send( :named_args_for, :three )
- end
-
- it 'should receive #instance_method with :zero and :three' do
- Klass.should_receive( :instance_method ).with( :zero ).and_return( @unbound )
- Klass.should_receive( :instance_method ).with( :three ).and_return( @unbound )
- Klass.send( :include, NamedArgs )
- Klass.send( :named_args_for, :zero, :three )
- end
-
- it do
- NamedArgs.should respond_to(:assign_options_string)
- NamedArgs.should respond_to(:assign_defaults_string)
- end
-
- it do
- Klass.should_receive( :instance_method ).with( :three ).and_return( @unbound )
- Klass.should_receive( :instance_method ).with( :zero ).and_return( @unbound )
- @unbound.should_receive(:args).twice.and_return([])
- @unbound.should_receive(:arg_names).twice.and_return([])
- Klass.send( :include, NamedArgs )
- Klass.send( :named_args_for, :zero, :three )
- end
- end
-
- describe '#named_args_for' do
-
- before do
- Klass.send( :include, NamedArgs )
- end
-
- it do
- Klass.send( :named_args_for, :three )
- Klass.new.should respond_to( :three_original )
- end
-
- it do
- Klass.send( :named_args_for, :three )
- Klass.new.should respond_to( :three_hacked )
- end
-
- end
-
- describe '#hacked_method' do
-
- before do
- Klass.send( :include, NamedArgs )
- Klass.send( :named_args_for, :zero, :three, :six )
- @instance = Klass.new
- end
-
- it do
- @instance.should_receive(:three_original).with(1,2,3)
- @instance.three(1,2,3)
- end
-
- it do
- @instance.should_receive(:three_original).with(1,2,3)
- @instance.three
- end
-
- it do
- @instance.should_receive(:three_original).with(1,2,2)
- @instance.three(1,2,2)
- end
-
- it do
- @instance.should_receive(:three_original).with(:a,:b,:c)
- @instance.three(:uno => :a, :dos => :b, :tres => :c)
- end
-
- it do
- @instance.should_receive(:three_original).with(:a,:b,:c)
- @instance.three(1, 2, 3, :uno => :a, :dos => :b, :tres => :c)
- end
-
- it do
- @instance.should_receive(:three_original).with(:a,:b,:c)
- @instance.three(1, 2, 3, :uno => :a, :dos => :b, :tres => :c)
- end
-
- it do
- @instance.should_receive(:six_original).with(1,:a,3,:b,5,:c)
- @instance.six(:dos => :a, :cuatro => :b, :seis => :c)
- end
-
- it do
- @instance.should_receive(:zero_original).with()
- @instance.zero
- end
-
- end
-
- describe NamedArgs do
-
- it "should get assign_defaults_string" do
- NamedArgs.assign_defaults_string( Klass.instance_method(:zero) ).should == ""
- NamedArgs.assign_defaults_string( Klass.instance_method(:three) ).should == "uno = 1 unless uno\ndos = 2 unless dos\ntres = 3 unless tres\n"
- end
-
- it "should assign from hash" do
- NamedArgs.assign_options_string( {:uno => 1, :dos => 2, :tres => 3} ).should =~ /uno = 1/
- NamedArgs.assign_options_string( {:uno => 1, :dos => 2, :tres => 3} ).should =~ /dos = 2/
- NamedArgs.assign_options_string( {:uno => 1, :dos => 2, :tres => 3} ).should =~ /tres = 3/
- end
-
- end
-
- describe 'Class methods' do
-
- it do
- Klass.should_receive(:dos_original)
- Klass.dos
- end
-
- end
-end
-
-
-describe 'benchmark' do
- before :all do
- @a = A.new
- @b = B.new
- end
-
- it do
- @b.should_receive(:test_original).with(1,2,3,4,5,6,7,8,9,10,11,12,13,14,:a,:b)
- @b.test(:quince => :a, :dieciseis => :b)
- end
-
- it do
- Benchmark.bm do |x|
- x.report do
- 10000.times { @a.test(1,2,3) }
- end
- x.report do
- 10000.times { @a.test(:uno => 1, :dos => 2, :tres => 3) }
- end
- end
- end
-
- it do
- Benchmark.bm do |x|
- x.report do
- class C
- def test(uno = 1, dos = 2, tres = 3, cuatro = 4, cinco = 5, seis = 6, siete = 7, ocho = 8, nueve = 9, diez = 10, once = 11, doce = 12, trece = 13, catorce = 14, quince = 15, dieciseis = 16)
- end
- end
- end
- x.report do
- class D
- def test(uno = 1, dos = 2, tres = 3, cuatro = 4, cinco = 5, seis = 6, siete = 7, ocho = 8, nueve = 9, diez = 10, once = 11, doce = 12, trece = 13, catorce = 14, quince = 15, dieciseis = 16)
- end
- named_args_for :test
- end
- end
- end
- end
-
-
-end
-
-
-
-
-
-
-
-
-
-

0 comments on commit 9f3e942

Please sign in to comment.