Sha256: af116ad95280fc5ae28bef955c27c3d883d618b7002fae9e4da072f48149ecac
Contents?: true
Size: 1.63 KB
Versions: 2
Compression:
Stored size: 1.63 KB
Contents
module Makers class Maker attr_reader :assignments, :associations, :sequences, :options attr_accessor :disabled_association def initialize(assignments, associations, sequences, options) @assignments = assignments @associations = associations @sequences = sequences @options = options end %w(build create).each do |name| define_method name do |*args| options = args.extract_options! action = :"#{name}_one" if args.any? collection = [] args.first.times do collection << send(action, options) end collection else send action, options end end end def attributes all = assignments if options.has_key?(:parent) all.reverse_merge! Makers.definitions.find(options[:parent]).assignments end if disabled_association associations.each do |name, class_name| if disabled_association == class_name all[name] = -> { nil } end end end object = Object.new all.each do |name, block| object.define_singleton_method name, &block end hash = {} all.keys.each do |name| hash[name] = object.send(name) end hash end private def build_one(overrides={}) instance = options[:class_name].constantize.new attributes.merge(overrides).each do |name, value| instance.send "#{name}=", value end instance end def create_one(overrides={}) instance = build_one(overrides) instance.save instance end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
makers-4.0.0.2 | lib/makers/maker.rb |
makers-4.0.0.1 | lib/makers/maker.rb |