1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00
thoughtbot--factory_bot/lib/factory_bot/evaluator.rb
Adam Hess 5c071d42fd Fix ruby 2.7 kwargs warning
Ruby 2.7 deprecated passing kwargs when the method expects a hash or passing a hash when the method expects kwargs. In factory_bot, this creates the warning:

```
/Users/hparker/code/factory_bot/lib/factory_bot/decorator/new_constructor.rb:9: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/Users/hparker/code/factory_bot/spec/acceptance/initialize_with_spec.rb:220: warning: The called method `initialize' is defined here
```

We can fix this warning by updating the syntax. We need to include `**kwargs` in the `method_missing` calls when we are on ruby 2.7 or later.
In decorator.rb, adding `**kwargs` alone doesn't work since adding `**kwargs` can change what arguments remain in the `args`.

In this case we have to class eval the method if we are running ruby 2.7. This way the syntax is valid in previous versions and we can use the `...` operator which allows us to avoid changing the arguments passed on in method missing.

Co-authored-by: Lee Quarella <leequarella@gmail.com>
2020-06-24 18:27:38 -04:00

90 lines
2.7 KiB
Ruby

require "active_support/core_ext/hash/except"
require "active_support/core_ext/class/attribute"
module FactoryBot
# @api private
class Evaluator
class_attribute :attribute_lists
private_instance_methods.each do |method|
undef_method(method) unless method.match?(/^__|initialize/)
end
def initialize(build_strategy, overrides = {})
@build_strategy = build_strategy
@overrides = overrides
@cached_attributes = overrides
@instance = nil
@overrides.each do |name, value|
singleton_class.define_attribute(name) { value }
end
end
def association(factory_name, *traits_and_overrides)
overrides = traits_and_overrides.extract_options!
strategy_override = overrides.fetch(:strategy) {
FactoryBot.use_parent_strategy ? @build_strategy.class : :create
}
traits_and_overrides += [overrides.except(:strategy)]
runner = FactoryRunner.new(factory_name, strategy_override, traits_and_overrides)
@build_strategy.association(runner)
end
attr_writer :instance
if ::Gem::Version.new(::RUBY_VERSION) >= ::Gem::Version.new("2.7")
def method_missing(method_name, *args, **kwargs, &block) # rubocop:disable Style/MethodMissingSuper, Style/MissingRespondToMissing
if @instance.respond_to?(method_name)
@instance.send(method_name, *args, **kwargs, &block)
else
SyntaxRunner.new.send(method_name, *args, **kwargs, &block)
end
end
else
def method_missing(method_name, *args, &block) # rubocop:disable Style/MethodMissingSuper, Style/MissingRespondToMissing
if @instance.respond_to?(method_name)
@instance.send(method_name, *args, &block)
else
SyntaxRunner.new.send(method_name, *args, &block)
end
end
end
def respond_to_missing?(method_name, _include_private = false)
@instance.respond_to?(method_name) || SyntaxRunner.new.respond_to?(method_name)
end
def __override_names__
@overrides.keys
end
def increment_sequence(sequence)
sequence.next(self)
end
def self.attribute_list
AttributeList.new.tap do |list|
attribute_lists.each do |attribute_list|
list.apply_attributes attribute_list.to_a
end
end
end
def self.define_attribute(name, &block)
if instance_methods(false).include?(name) || private_instance_methods(false).include?(name)
undef_method(name)
end
define_method(name) do
if @cached_attributes.key?(name)
@cached_attributes[name]
else
@cached_attributes[name] = instance_exec(&block)
end
end
end
end
end