Skip to content

Instantly share code, notes, and snippets.

@c-vetter
Created April 10, 2011 17:11
Show Gist options
  • Save c-vetter/912531 to your computer and use it in GitHub Desktop.
Save c-vetter/912531 to your computer and use it in GitHub Desktop.
Many models, one set of cucumber steps: BOOYAH!! also: machinist ftw
# make sure there are none
Given /^no (.+) exists$/ do |model_name|
model_class(model_name).destroy_all
end
# single model, one attribute of interest
Given /^a (?:(.+) )?(.+) with (.+) "(.+)" exists$/ do |blueprint_name, model_name, key, value|
model_class(model_name).make blueprint_name, key => value
end
# single model, attributes as rows
Given /^the following (?:(.+) )?(.+) exists:$/ do |blueprint_name, model_name, data|
model_class(model_name).make blueprint_name, data.rows_hash
end
# several models, attributes as columns
Given /^the following (?:(.+) )?(.+) exist:$/ do |blueprint_name, model_name, data|
model = model_class(model_name)
data.hashes.each do |datum|
model.make blueprint_name, datum
end
end
# given number of models, attribute values irrelevant
Given /^(\d+) (?:(.+) )?(.+) exist$/ do |count, blueprint_name, model_name|
model = model_class(model_name)
count.to_i.times { model.make blueprint_name }
end
# DRY
def model_class(model_name)
model_name.titleize.gsub(/\s/, '').singularize.constantize
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment