class Fabrication::Cucumber::StepFabricator

Attributes

model[R]

Public Class Methods

new(model_name, opts = {}) click to toggle source
# 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

Public Instance Methods

from_table(table, extra = {}) click to toggle source
# 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) click to toggle source

rubocop:disable Naming/PredicateName

# 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
klass() click to toggle source
# File lib/fabrication/cucumber/step_fabricator.rb, line 45
def klass
  Fabricate.schematic(@fabricator).send(:klass)
end
n(count, attrs = {}) click to toggle source
# File lib/fabrication/cucumber/step_fabricator.rb, line 24
def n(count, attrs = {})
  Array.new(count).map { make(attrs) }.tap { |o| remember(o) }
end
parent() click to toggle source

rubocop:enable Naming/PredicateName

# File lib/fabrication/cucumber/step_fabricator.rb, line 39
def parent
  return unless @parent_name

  Fabrications[dehumanize(@parent_name)]
end

Private Instance Methods

dehumanize(string) click to toggle source
# File lib/fabrication/cucumber/step_fabricator.rb, line 63
def dehumanize(string)
  string.gsub(/\W+/, '_').downcase
end
make(attrs = {}) click to toggle source
# File lib/fabrication/cucumber/step_fabricator.rb, line 71
def make(attrs = {})
  Fabricate(@fabricator, attrs.merge(parentship))
end
parameterize_hash(hash) click to toggle source
# File lib/fabrication/cucumber/step_fabricator.rb, line 67
def parameterize_hash(hash)
  hash.inject({}) { |h, (k, v)| h.update(dehumanize(k).to_sym => v) }
end
parentship() click to toggle source
# File lib/fabrication/cucumber/step_fabricator.rb, line 75
def parentship
  return {} unless parent

  parent_class_name = parent.class.to_s.underscore

  parent_instance = parent
  unless klass.new.respond_to?("#{parent_class_name}=")
    parent_class_name = parent_class_name.pluralize
    parent_instance = [parent]
  end

  { parent_class_name => parent_instance }
end
remember(objects) click to toggle source
# File lib/fabrication/cucumber/step_fabricator.rb, line 51
def remember(objects)
  if singular?
    Fabrications[@fabricator] = objects.last
  else
    Fabrications[@model.to_sym] = objects
  end
end
singular?() click to toggle source
# File lib/fabrication/cucumber/step_fabricator.rb, line 59
def singular?
  @model == Fabrication::Support.singularize(@model)
end