# HG changeset patch # User Michael Granger # Date 1648147747 25200 # Thu Mar 24 11:49:07 2022 -0700 # Node ID 3dded3cc26c09d27831abde0f35bd25dcd428400 # Parent 4142346af0dd77d7e175e1a790e3781c95f79464 # Parent 142f70aec0b42cd1665578846ef737df7fafd2bc Merge pull request #1 from rgalanakis/master Fix keyword argument decorators in Ruby 3.0 diff --git a/.ruby-version b/.ruby-version --- a/.ruby-version +++ b/.ruby-version @@ -1,1 +1,1 @@ -2.7 +3.1 diff --git a/fluent_fixtures.gemspec b/fluent_fixtures.gemspec --- a/fluent_fixtures.gemspec +++ b/fluent_fixtures.gemspec @@ -5,6 +5,7 @@ s.name = "fluent_fixtures".freeze s.version = "0.9.0.pre.20201102181742" + s.authors = [ "Michael Granger" ] s.required_rubygems_version = Gem::Requirement.new("> 1.3.1".freeze) if s.respond_to? :required_rubygems_version= s.metadata = { "changelog_uri" => "http://deveiate.org/code/fluent_fixtures/History_md.html", "documentation_uri" => "http://deveiate.org/code/fluent_fixtures", "homepage_uri" => "http://bitbucket.org/ged/fluent_fixtures" } if s.respond_to? :metadata= s.require_paths = ["lib".freeze] diff --git a/lib/fluent_fixtures/factory.rb b/lib/fluent_fixtures/factory.rb --- a/lib/fluent_fixtures/factory.rb +++ b/lib/fluent_fixtures/factory.rb @@ -190,7 +190,13 @@ self.apply_prelude( instance, decorator_options[:prelude] ) if decorator_options[:prelude] instance = self.try_to_save( instance ) if decorator_options[:presave] - instance.instance_exec( *args, &decorator_block ) + if args[-1].is_a?(Hash) + kwargs = args[-1] + args = args[0..-2] + else + kwargs = {} + end + instance.instance_exec( *args, **kwargs, &decorator_block ) return instance end diff --git a/spec/fluent_fixtures/factory_spec.rb b/spec/fluent_fixtures/factory_spec.rb --- a/spec/fluent_fixtures/factory_spec.rb +++ b/spec/fluent_fixtures/factory_spec.rb @@ -336,6 +336,38 @@ end + it "handles keyword-only decorators" do + fixture_module.decorator( :set_fields ) do |name: nil, email: nil| + self.name = name + self.email = email + end + + object = factory.set_fields( name: 'x', email: 'a@b.c' ).instance + + expect( object ).to be_a( fixtured_class ) + expect( object.name ).to eq( 'x' ) + expect( object.email ).to eq( 'a@b.c' ) + expect( object ).to_not be_saved + end + + + it "handles mixed position and keyword decorators" do + fixture_module.decorator( :set_fields ) do |arg1, arg2=2, kwarg: nil| + self.name = arg1 + self.email = arg2 + self.login = kwarg + end + + object = factory.set_fields( 'x', 'a@b.c', kwarg: 'mylogin' ).instance + + expect( object ).to be_a( fixtured_class ) + expect( object.name ).to eq( 'x' ) + expect( object.email ).to eq( 'a@b.c' ) + expect( object.login ).to eq( 'mylogin' ) + expect( object ).to_not be_saved + end + + describe "enumerator/generator" do it "is Enumerable" do