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.



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

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.



4
5
6
# File 'lib/fabrication/cucumber/step_fabricator.rb', line 4

def model
  @model
end

Instance Method Details

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



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

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



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

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



43
44
45
# File 'lib/fabrication/cucumber/step_fabricator.rb', line 43

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

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



22
23
24
# File 'lib/fabrication/cucumber/step_fabricator.rb', line 22

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

#parentObject

rubocop:enable Naming/PredicateName



37
38
39
40
41
# File 'lib/fabrication/cucumber/step_fabricator.rb', line 37

def parent
  return unless @parent_name

  Fabrications[dehumanize(@parent_name)]
end