class Fabrication::Cucumber::StepFabricator
Attributes
Public Class Methods
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
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
Source
# 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
rubocop:disable Naming/PredicateName
Source
# File lib/fabrication/cucumber/step_fabricator.rb, line 45 def klass Fabricate.schematic(@fabricator).send(:klass) end
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
Source
# File lib/fabrication/cucumber/step_fabricator.rb, line 39 def parent return unless @parent_name Fabrications[dehumanize(@parent_name)] end
rubocop:enable Naming/PredicateName
Private Instance Methods
Source
# File lib/fabrication/cucumber/step_fabricator.rb, line 63 def dehumanize(string) string.gsub(/\W+/, '_').downcase end
Source
# File lib/fabrication/cucumber/step_fabricator.rb, line 71 def make(attrs = {}) Fabricate(@fabricator, attrs.merge(parentship)) end
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
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
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
Source
# File lib/fabrication/cucumber/step_fabricator.rb, line 59 def singular? @model == Fabrication::Support.singularize(@model) end