Skip to content

Instantly share code, notes, and snippets.

@sandro
Created July 31, 2010 19:01
Show Gist options
  • Save sandro/502499 to your computer and use it in GitHub Desktop.
Save sandro/502499 to your computer and use it in GitHub Desktop.
Fabricate.attributes_for
Fabricate.class_eval do
def self.attributes_for(name, options={})
fetch_schematic(name).attributes.inject({}) do |hash, attribute|
value = attribute.value.respond_to?(:call) ? attribute.value.call : attribute.value
hash.merge(attribute.name => value)
end.merge(options)
end
private
def self.fetch_schematic(name)
if fabricator = Fabrication.fabricators[name]
fabricator.schematic
else
# force finding definitions after setting @@fabricators to an empty array
Fabrication.send(:class_variable_set, :@@fabricators, nil) if Fabrication.fabricators.empty?
build(name)
fetch_schematic(name)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment