Class: Mongo::Fixture::Inserter

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo-fixture/inserter.rb

Overview

Handles the actual insertion into the database.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fixture) ⇒ Inserter

Receives a fixture as argument



7
8
9
10
# File 'lib/mongo-fixture/inserter.rb', line 7

def initialize fixture
  @fixture = fixture 
  @inserted = []
end

Instance Attribute Details

#fixtureObject (readonly)

Returns the fixture



113
114
115
# File 'lib/mongo-fixture/inserter.rb', line 113

def fixture
  @fixture
end

Instance Method Details

#data_was_inserted_in?(collection) ⇒ Boolean

Returns true if the collection was already inserted

Returns:

  • (Boolean)


104
105
106
# File 'lib/mongo-fixture/inserter.rb', line 104

def data_was_inserted_in? collection
  @inserted.include? collection
end

#insert_data_for(collection) ⇒ Object

Inserts the collection data into the database



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mongo-fixture/inserter.rb', line 78

def insert_data_for collection
#      @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        
  return if data_was_inserted_in? collection
  @fixture.data[collection].each do |key, record|
    begin
      @fixture.connection[collection].insert simplify record
    rescue MissingProcessedValueError => m
      @fixture.rollback
      raise MissingProcessedValueError, "In record '#{key}' to be inserted into '#{collection}', the processed value of field '#{m.field}' is missing, aborting"
    end
  end
  @inserted << collection
end

#insertedObject



108
109
110
# File 'lib/mongo-fixture/inserter.rb', line 108

def inserted
  @inserted
end

#resolve_field_hash(value) ⇒ Object

Returns the correct data for this field resolving the hash

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mongo-fixture/inserter.rb', line 30

def resolve_field_hash value
  raise ArgumentError, "Hash expected" unless value.is_a? Hash
  return value[:processed] if value.has_key? :processed

  intersection = value.keys & @fixture.data.keys
  if intersection.empty?
    raise Mongo::Fixture::ReferencedRecordNotFoundError,
      "This fixture does not include data for the collections [#{value.keys.join(',')}]"
  else
    intersection.each do |collection|
      insert_data_for collection unless data_was_inserted_in? collection
      if value[collection].is_a? Array
        ids = []
        value[collection].each do |element|
          ids.push do_the_resolving collection, element.to_sym
        end
        return ids
      else
        return do_the_resolving collection, value[collection].to_sym
      end
    end
  end
end

#simplify(the_record) ⇒ Object

Simplifies the hash in order to insert it into the database Resolves external references and flattens the values that provide alternatives

Parameters:

  • the (Hash)

    hash to be processed



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mongo-fixture/inserter.rb', line 15

def simplify the_record
  the_returned_hash = {}
  the_record.each do |field, value|
    begin
      value = resolve_field_hash value if value.is_a? Hash
    rescue Mongo::Fixture::ReferencedRecordNotFoundError => e
      @fixture.rollback
      raise e
    end
    the_returned_hash[field] = value
  end
  return the_returned_hash
end