Class: Mongo::Fixture
- Inherits:
-
Object
- Object
- Mongo::Fixture
- Defined in:
- lib/mongo-fixture.rb,
lib/mongo-fixture/version.rb,
lib/mongo-fixture/inserter.rb
Overview
Fixture managing class for MongoDB
Defined Under Namespace
Classes: ChangingConnectionIllegal, CollectionsNotEmptyError, Inserter, LoadingFixtureIllegal, MissingConnectionError, MissingFixtureError, MissingProcessedValueError, ReferencedRecordNotFoundError, RollbackIllegalError
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the current database connection.
-
#data ⇒ Object
readonly
Returns the current data collection.
-
#inserter ⇒ Object
readonly
Returns the inserter for this fixture.
Class Method Summary collapse
-
.path ⇒ String
Returns the current path to the fixtures folder.
-
.path=(the_path) ⇒ Object
Sets the current path to the fixtures folder.
Instance Method Summary collapse
-
#[](reference) ⇒ Object
Returns the SymbolMatrix with the data referring to that table.
-
#check ⇒ Object
Assures that the collections are empty before proceeding.
-
#fixtures_path ⇒ Object
Returns the current fixtures path where Sequel::Fixtures looks for fixture folders.
-
#force_checked! ⇒ Object
Forces the check to pass.
-
#initialize(fixture = nil, connection = nil, option_push = true) ⇒ Fixture
constructor
Initializes the fixture handler Accepts optionally a symbol as a reference to the fixture and a Mongo::DB connection.
-
#load(fixture) ⇒ Object
Loads the fixture files into this instance.
-
#method_missing(s, *args) ⇒ Object
Method missing, for enabling discovery of tables.
-
#push ⇒ Object
Inserts the fixture data into the corresponding collections.
-
#rollback ⇒ Object
Empties the collections, only if they were empty to begin with.
Constructor Details
#initialize(fixture = nil, connection = nil, option_push = true) ⇒ Fixture
Initializes the fixture handler Accepts optionally a symbol as a reference to the fixture and a Mongo::DB connection
30 31 32 33 34 35 36 |
# File 'lib/mongo-fixture.rb', line 30 def initialize fixture = nil, connection = nil, option_push = true load fixture if fixture @connection = connection if connection @inserter = Inserter.new self push if fixture && connection && option_push end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(s, *args) ⇒ Object
Method missing, for enabling discovery of tables
64 65 66 67 |
# File 'lib/mongo-fixture.rb', line 64 def method_missing s, *args return @data[s] if @data && @data.has_key?(s) return super end |
Instance Attribute Details
#connection ⇒ Object
Returns the current database connection
70 71 72 |
# File 'lib/mongo-fixture.rb', line 70 def connection @connection end |
#data ⇒ Object (readonly)
Returns the current data collection
82 83 84 |
# File 'lib/mongo-fixture.rb', line 82 def data @data end |
#inserter ⇒ Object (readonly)
Returns the inserter for this fixture
73 74 75 |
# File 'lib/mongo-fixture.rb', line 73 def inserter @inserter end |
Class Method Details
.path ⇒ String
Returns the current path to the fixtures folder
16 17 18 |
# File 'lib/mongo-fixture.rb', line 16 def self.path @@path ||= "test/fixtures" end |
.path=(the_path) ⇒ Object
Sets the current path to the fixtures folder
21 22 23 |
# File 'lib/mongo-fixture.rb', line 21 def self.path= the_path @@path = the_path end |
Instance Method Details
#[](reference) ⇒ Object
Returns the SymbolMatrix with the data referring to that table
59 60 61 |
# File 'lib/mongo-fixture.rb', line 59 def [] reference @data[reference] end |
#check ⇒ Object
Assures that the collections are empty before proceeding
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/mongo-fixture.rb', line 85 def check return @checked if @checked # If already checked, it's alright raise MissingFixtureError, "No fixture has been loaded, nothing to check" unless @data raise MissingConnectionError, "No connection has been provided, impossible to check" unless @connection @data.each_key do |collection| raise CollectionsNotEmptyError, "The collection '#{collection}' is not empty, all collections should be empty prior to testing" if @connection[collection].count != 0 end return @checked = true end |
#fixtures_path ⇒ Object
Returns the current fixtures path where Sequel::Fixtures looks for fixture folders
49 50 51 |
# File 'lib/mongo-fixture.rb', line 49 def fixtures_path Mongo::Fixture.path end |
#force_checked! ⇒ Object
Forces the check to pass. Dangerous!
54 55 56 |
# File 'lib/mongo-fixture.rb', line 54 def force_checked! @checked = true end |
#load(fixture) ⇒ Object
Loads the fixture files into this instance
39 40 41 42 43 44 45 46 |
# File 'lib/mongo-fixture.rb', line 39 def load fixture raise LoadingFixtureIllegal, "A check has already been made, loading a different fixture is illegal" if @checked Fast.dir("#{fixtures_path}/#{fixture}").files.to.symbols.each do |file| @data ||= {} @data[file] = SymbolMatrix.new "#{fixtures_path}/#{fixture}/#{file}.yaml" end end |
#push ⇒ Object
Inserts the fixture data into the corresponding collections
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/mongo-fixture.rb', line 98 def push check 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 |
#rollback ⇒ Object
Empties the collections, only if they were empty to begin with
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/mongo-fixture.rb', line 120 def rollback begin check @data.each_key do |collection| @connection[collection].drop end rescue CollectionsNotEmptyError => e raise RollbackIllegalError, "The collections weren't empty to begin with, rollback aborted." end end |