Created
July 31, 2010 19:01
-
-
Save sandro/502499 to your computer and use it in GitHub Desktop.
Fabricate.attributes_for
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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