lib/mongo-fixture.rb in mongo-fixture-0.0.4 vs lib/mongo-fixture.rb in mongo-fixture-0.0.5

- old
+ new

@@ -2,10 +2,11 @@ require "fast/fast" require "symbolmatrix" require "mongo" require "mongo-fixture/version" +require "mongo-fixture/inserter" module Mongo # Fixture managing class for MongoDB class Fixture @@ -23,11 +24,11 @@ # and a Mongo::DB connection def initialize fixture = nil, connection = nil, option_push = true load fixture if fixture @connection = connection if connection - @inserted = [] + @inserter = Inserter.new self push if fixture && connection && option_push end # Loads the fixture files into this instance def load fixture @@ -60,10 +61,13 @@ return super end # Returns the current database connection attr_reader :connection + + # Returns the inserter for this fixture + attr_reader :inserter # Sets the connection. Raises an ChangingConnectionIllegal exception if this fixture has already been checked def connection= the_connection raise ChangingConnectionIllegal, "A check has already been performed, changing the connection now is illegal" if @checked @connection = the_connection @@ -87,69 +91,28 @@ # Inserts the fixture data into the corresponding collections def push check - @data.each do |collection, matrix| - unless data_was_inserted_in? collection - matrix.each do |element, values| - begin - @connection[collection].insert simplify values.to_hash - rescue MissingProcessedValueError => m - rollback - raise MissingProcessedValueError, "In record '#{element}' to be inserted into '#{collection}', the processed value of field '#{m.field}' is missing, aborting" - end - end - @inserted << collection - end + data.each_key do |collection| + @inserter.insert_data_for collection end +# @data.each do |collection, matrix| +# unless @inserter.data_was_inserted_in? collection +# matrix.each do |element, values| +# begin +# @connection[collection].insert @inserter.simplify values.to_hash +# rescue MissingProcessedValueError => m +# rollback +# raise MissingProcessedValueError, "In record '#{element}' to be inserted into '#{collection}', the processed value of field '#{m.field}' is missing, aborting" +# end +# end +# @inserted << collection +# end +# end end - - # Simplifies the hash in order to insert it into the database - # Resolves external references and flattens the values that provide alternatives - # @param [Hash] the hash to be processed - def simplify the_hash - the_returned_hash = {} - the_hash.each do |key, value| - if value.is_a? Hash - - # If no alternative matches the name of a collection, look for a :processed value - if (value.keys & @data.keys).empty? - unless value.has_key? :processed - raise MissingProcessedValueError.new "The processed value to insert into the db is missing from the field '#{key}', aborting", key - end - the_returned_hash[key] = value[:processed] - else - - # Does any of the options hold a record named after the value of the option? - options = value.keys & @data.keys - actual_option = options.each do |option| - break option if @data[option].has_key? value[option].to_sym - end - - unless data_was_inserted_in? actual_option - insert_data_for actual_option - end - current_collection = @connection[actual_option] - current_data = simplify @data[actual_option][value[actual_option].to_sym] - the_returned_hash[key] = current_collection.find(current_data).first["_id"] - end - else - the_returned_hash[key] = value - end - end - return the_returned_hash - end - - # Inserts the collection data into the database - def insert_data_for collection - @data[collection].each do |key, record| - @connection[collection].insert simplify record - end - @inserted << collection - end - + # Empties the collections, only if they were empty to begin with def rollback begin check @@ -165,19 +128,17 @@ class CollectionsNotEmptyError < StandardError; end class MissingFixtureError < StandardError; end class MissingConnectionError < StandardError; end class ChangingConnectionIllegal < StandardError; end class RollbackIllegalError < StandardError; end + class ReferencedRecordNotFoundError < StandardError; end class MissingProcessedValueError < StandardError attr_accessor :field def initialize message, field = nil @field = field super message end end - private - def data_was_inserted_in? collection - @inserted.include? collection - end + end end