Sha256: b6d57a8522d5e5b70d613eaf92ed8f3a2e9bfa3e8bff5c8803ccf10770b9cb0e
Contents?: true
Size: 1.89 KB
Versions: 2
Compression:
Stored size: 1.89 KB
Contents
require 'active_support' require 'active_record' module Machinist def self.with_save_nerfed begin @@nerfed = true yield ensure @@nerfed = false end end @@nerfed = false def self.nerfed? @@nerfed end module ActiveRecordExtensions def self.included(base) base.extend(ClassMethods) end module ClassMethods def blueprint(&blueprint) @blueprint = blueprint end def make(attributes = {}) raise "No blueprint for class #{self}" if @blueprint.nil? lathe = Lathe.new(self.new, attributes) lathe.instance_eval(&@blueprint) unless Machinist.nerfed? lathe.object.save! lathe.object.reload end returning(lathe.object) do |object| yield object if block_given? end end def make_unsaved(attributes = {}) returning(Machinist.with_save_nerfed { make(attributes) }) do |object| yield object if block_given? end end end end class Lathe def initialize(object, attributes) @object = object @assigned_attributes = [] attributes.each do |key, value| @object.send("#{key}=", value) @assigned_attributes << key end end attr_reader :object def method_missing(symbol, *args, &block) if @assigned_attributes.include?(symbol) @object.send(symbol) else value = if block block.call elsif args.first.is_a?(Hash) || args.empty? symbol.to_s.camelize.constantize.make(args.first || {}) else args.first end @object.send("#{symbol}=", value) @assigned_attributes << symbol end end end end if Module.const_defined?("RAILS_ENV") && RAILS_ENV == 'test' require 'sham' class ActiveRecord::Base include Machinist::ActiveRecordExtensions end end
Version data entries
2 entries across 2 versions & 2 rubygems
Version | Path |
---|---|
elight-machinist-1.0.1 | lib/machinist.rb |
notahat-machinist-0.1.1 | lib/machinist.rb |