Sha256: 5c9982dca236fbd5086d4cae9d54e81bddc8f7aa1deda5c1830d92585f66c7c6
Contents?: true
Size: 1.92 KB
Versions: 1
Compression:
Stored size: 1.92 KB
Contents
require 'factory_girl' module Summon (class << self; self; end).class_eval do attr_accessor :noisy end self.noisy = false class Conjure attr :parent def initialize(parent) @parent = parent end def log(string) if parent.nil? && Summon.noisy printf string; $stdout.flush end end def method_missing(name, *args, &block) process_options = lambda do |o| o.inject({}) do |hash, pair| key, value = pair value = case value when Range: value.to_a[rand(value.to_a.size)] when Array: value[rand(value.size)] when Proc: value.call else value end hash[key] = value hash end end quantity = args.first || 1 quantity = quantity.to_a[rand(quantity.to_a.size)] if quantity.is_a?(Range) options = args.extract_options! log "** Summoning #{quantity} #{name.to_s.pluralize} " quantity.times do log '.' attributes = process_options.call(options) if parent.nil? child = Factory(name, attributes) else association = @parent.class.reflect_on_association(name.to_sym) raise "Association #{name} not found on #{@parent.class.to_s}" unless association child = case association.macro when :has_one: parent.send("#{name}=", Factory(name.to_s.singularize, attributes)) when :has_many: parent.send(name).create(Factory.attributes_for(name.to_s.singularize, attributes)) else raise "#{association.macro} macros are not supported" end end yield Conjure.new(child) if block_given? end log "\n" end end end def Summon(factory, quantity, options = {}, &block) Summon::Conjure.new(nil).send(factory, quantity, options, &block) end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
jim-summon-0.1.0 | lib/summon.rb |