Class: CucumberFactory::BuildStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber_factory/build_strategy.rb

Overview

wraps machinist / factory_bot / ruby object logic

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, &block) ⇒ BuildStrategy

Returns a new instance of BuildStrategy.



97
98
99
100
# File 'lib/cucumber_factory/build_strategy.rb', line 97

def initialize(model_class, &block)
  @model_class = model_class
  @create_proc = block
end

Instance Attribute Details

#model_classObject (readonly)

Returns the value of attribute model_class.



95
96
97
# File 'lib/cucumber_factory/build_strategy.rb', line 95

def model_class
  @model_class
end

Class Method Details

.from_prose(model_prose, variant_prose) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cucumber_factory/build_strategy.rb', line 9

def from_prose(model_prose, variant_prose)
  # don't use \w which depends on the system locale
  underscored_model_name = model_prose.gsub(/[^A-Za-z0-9_\/]+/, "_")
  if variant_prose.present?
    variants = /\((.*?)\)/.match(variant_prose)[1].split(/\s*,\s*/)
    variants = variants.collect { |variant| variant.downcase.gsub(" ", "_") }
  else
    variants = []
  end

  if factory_bot_strategy = factory_bot_strategy(underscored_model_name, variants)
    factory_bot_strategy
  else
    model_class = underscored_model_name.camelize.constantize
    machinist_strategy(model_class, variants) ||
      active_record_strategy(model_class) ||
      ruby_object_strategy(model_class)
  end
end

Instance Method Details

#create_record(attributes) ⇒ Object



102
103
104
# File 'lib/cucumber_factory/build_strategy.rb', line 102

def create_record(attributes)
  @create_proc.call(attributes)
end