Skip to content

Instantly share code, notes, and snippets.

@deevis
Last active August 15, 2019 20:30
Show Gist options
  • Save deevis/1a45ab7f55ea514d2a2c8fa3a27ec271 to your computer and use it in GitHub Desktop.
Save deevis/1a45ab7f55ea514d2a2c8fa3a27ec271 to your computer and use it in GitHub Desktop.
Show create counts by FactoryBot
#
# lib/factory_bot/strategy/create.rb
#
# coloring from: http://graysoftinc.com/terminal-tricks/random-access-terminal
#
module FactoryBot
module Strategy
class Create
@@__counts = Hash.new(0)
CSI = "\e["
def association(runner)
runner.run
end
def result(evaluation)
class_name = evaluation.instance_variable_get(:@attribute_assigner).instance_variable_get(:@build_class).to_s
@@__counts[class_name] += 1
# print "#{CSI}s" # save cursor position
print "#{CSI}2J" # clear screen
print "#{CSI}1;1H" # move to line 1, character 4
puts "FactoryBot Counts"
puts "-" * 50
sum = 0
@@__counts.each do |name, count|
sum += count
puts "#{name.rjust(20,' ')}: #{count.to_s.ljust(6, ' ')}"
end
puts "-" * 50
puts " Total: #{sum}"
# print "#{CSI}u" # restore cursor position
evaluation.object.tap do |instance|
evaluation.notify(:after_build, instance)
evaluation.notify(:before_create, instance)
evaluation.create(instance)
evaluation.notify(:after_create, instance)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment