Class: ActiveCucumber::Creator

Inherits:
Object
  • Object
show all
Includes:
FactoryBot::Syntax::Methods
Defined in:
lib/active_cucumber/creator.rb

Overview

Creates ActiveRecord entries with data from given Cucumber tables.

Instance Method Summary collapse

Constructor Details

#initialize(attributes, context) ⇒ Creator

Returns a new instance of Creator.



10
11
12
13
14
15
# File 'lib/active_cucumber/creator.rb', line 10

def initialize(attributes, context)
  @attributes = attributes
  context.each do |key, value|
    instance_variable_set "@#{key}", value
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object (private)



46
47
48
49
50
51
52
53
54
# File 'lib/active_cucumber/creator.rb', line 46

def method_missing(method_name, *, &)
  # This is necessary so that a Creator subclass can access
  # methods of @attributes as if they were its own.
  if @attributes.respond_to?(method_name, true)
    @attributes.send(method_name, *, &)
  else
    super
  end
end

Instance Method Details

#factorybot_attributesObject

Returns the FactoryBot version of this Creator’s attributes. rubocop:disable Metrics/MethodLength



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_cucumber/creator.rb', line 19

def factorybot_attributes
  symbolize_attributes!
  # Capture original keys and values before any transformations
  # to ensure each value_for_* method receives the original value
  original_attributes = @attributes.dup
  keys_to_process = original_attributes.keys
  keys_to_process.each do |key|
    method = method_name(key)
    next unless respond_to?(method)

    value = original_attributes[key]
    result = send(method, value)

    # Keep the transformed value if it's truthy or if the original was nil
    # Check if key still exists (value_for_* method may have deleted it)
    if result || value.nil?
      @attributes[key] = result if @attributes.key?(key)
    else
      @attributes.delete(key)
    end
  end
  @attributes
end