Skip to content

Instantly share code, notes, and snippets.

@sshaw
Last active December 21, 2015 05:49
Show Gist options
  • Save sshaw/6259749 to your computer and use it in GitHub Desktop.
Save sshaw/6259749 to your computer and use it in GitHub Desktop.
Rake task to list the class and names of fabricators defined via the Fabrication gem. Use CLASS and FABRICATOR to filter. Now part of fabrication gem: https://github.com/paulelliott/fabrication/issues/187
# https://gist.github.com/sshaw/6259749
#
# As of 2.9.4, this is part of the fabrication gem (less env vars)
# https://github.com/paulelliott/fabrication/blob/master/lib/tasks/defined_fabricators.rake
desc "List fabricators, use CLASS and FABRICATOR to filter"
task :fabricators => :environment do
Fabrication::Support.find_definitions if Fabrication.manager.empty?
if Fabrication.manager.schematics.none?
puts "No fabricators found"
next
end
groups = Fabrication.manager.schematics.group_by { |name, fabdef| fabdef.klass.name }
if ENV["CLASS"]
re = Regexp.new(ENV["CLASS"])
groups.reject! { |klass, _| klass !~ re }
end
if ENV["FABRICATOR"]
re = Regexp.new(ENV["FABRICATOR"])
groups.each do |_, set|
set.reject! { |fabdef| fabdef.first.to_s !~ re }
end
groups.reject! { |_, set| set.empty? }
end
fabricators = {}
groups.sort_by { |klass, _| klass }.each do |klass, groups|
fabricators[klass] = groups.map(&:first).sort.join(", ")
end
class_width = fabricators.keys.max_by { |v| v.size }.size + 3 # padding
names_width = fabricators.values.max_by { |v| v.size }.size
say = lambda do |f1, f2|
printf "%-#{class_width}s%-#{names_width}s\n", f1, f2
end
say["Class", "Fabricator"]
puts "-" * (names_width + class_width)
fabricators.each { |klass, names| say[klass,names] }
end
@sshaw
Copy link
Author

sshaw commented Aug 18, 2013

Output looks like this:

Class    Fabricator   
----------------------
Animal   animal, beast
Person   person  

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment