Class: Fabrication::Cucumber::StepFabricator

Inherits:
Object
  • Object
show all
Defined in:
lib/fabrication/cucumber/step_fabricator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_name, opts = {}) ⇒ StepFabricator

Returns a new instance of StepFabricator.



8
9
10
11
12
# File 'lib/fabrication/cucumber/step_fabricator.rb', line 8

def initialize(model_name, opts = {})
  @model = dehumanize(model_name)
  @fabricator = Fabrication::Support.singularize(@model).to_sym
  @parent_name = opts.delete(:parent)
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



6
7
8
# File 'lib/fabrication/cucumber/step_fabricator.rb', line 6

def model
  @model
end

Instance Method Details

#from_table(table, extra = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/fabrication/cucumber/step_fabricator.rb', line 14

def from_table(table, extra = {})
  hashes = singular? ? [table.rows_hash] : table.hashes
  transformed_hashes = hashes.map do |hash|
    transformed_hash = Fabrication::Transform.apply_to(@model, parameterize_hash(hash))
    make(transformed_hash.merge(extra))
  end
  remember(transformed_hashes)
  transformed_hashes
end

#has_many(children) ⇒ Object

rubocop:disable Naming/PredicateName



29
30
31
32
33
34
35
36
# File 'lib/fabrication/cucumber/step_fabricator.rb', line 29

def has_many(children)
  instance = Fabrications[@fabricator]
  children = dehumanize(children)
  [Fabrications[children]].flatten.each do |child|
    child.send("#{klass.to_s.underscore.downcase}=", instance)
    child.respond_to?(:save!) && child.save!
  end
end

#klassObject



45
46
47
# File 'lib/fabrication/cucumber/step_fabricator.rb', line 45

def klass
  Fabricate.schematic(@fabricator).send(:klass)
end

#n(count, attrs = {}) ⇒ Object



24
25
26
# File 'lib/fabrication/cucumber/step_fabricator.rb', line 24

def n(count, attrs = {})
  Array.new(count).map { make(attrs) }.tap { |o| remember(o) }
end

#parentObject

rubocop:enable Naming/PredicateName



39
40
41
42
43
# File 'lib/fabrication/cucumber/step_fabricator.rb', line 39

def parent
  return unless @parent_name

  Fabrications[dehumanize(@parent_name)]
end