Class: Sequel::Fixture
- Inherits:
-
Object
- Object
- Sequel::Fixture
- Defined in:
- lib/sequel-fixture.rb,
lib/sequel-fixture/version.rb
Overview
Fixture managing class for Sequel
Defined Under Namespace
Classes: ChangingConnectionIllegal, LoadingFixtureIllegal, MissingConnectionError, MissingFixtureError, MissingProcessedValueError, RollbackIllegalError, TablesNotEmptyError
Constant Summary collapse
- VERSION =
"0.0.3"
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
-
.path ⇒ Object
Returns 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 tables 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 Sequel::Database 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 tables.
-
#rollback ⇒ Object
Empties the tables, only if they were empty to begin with.
-
#simplify(the_hash) ⇒ Object
Simplifies the hash in order to insert it into the database (Note: I’m well aware that this functionality belongs in a dependency).
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 Sequel::Database connection
24 25 26 27 28 29 |
# File 'lib/sequel-fixture.rb', line 24 def initialize fixture = nil, connection = nil, option_push = true load fixture if fixture @connection = connection if connection 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
52 53 54 55 |
# File 'lib/sequel-fixture.rb', line 52 def method_missing s, *args return @data[s] if @data && @data.has_key?(s) return super end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
104 105 106 |
# File 'lib/sequel-fixture.rb', line 104 def connection @connection end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
112 113 114 |
# File 'lib/sequel-fixture.rb', line 112 def data @data end |
Class Method Details
.path ⇒ Object
Returns the current path to the fixtures folder
14 15 16 |
# File 'lib/sequel-fixture.rb', line 14 def self.path @@path ||= "test/fixtures" end |
Instance Method Details
#[](reference) ⇒ Object
Returns the SymbolMatrix with the data referring to that table
47 48 49 |
# File 'lib/sequel-fixture.rb', line 47 def [] reference @data[reference] end |
#check ⇒ Object
Assures that the tables are empty before proceeding
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/sequel-fixture.rb', line 63 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 |table| raise TablesNotEmptyError, "The table '#{table}' is not empty, all tables should be empty prior to testing" if @connection[table].count != 0 end return @checked = true end |
#fixtures_path ⇒ Object
Returns the current fixtures path where Sequel::Fixtures looks for fixture folders
42 43 44 |
# File 'lib/sequel-fixture.rb', line 42 def fixtures_path Sequel::Fixture.path end |
#force_checked! ⇒ Object
Forces the check to pass. Dangerous!
58 59 60 |
# File 'lib/sequel-fixture.rb', line 58 def force_checked! @checked = true end |
#load(fixture) ⇒ Object
Loads the fixture files into this instance
32 33 34 35 36 37 38 39 |
# File 'lib/sequel-fixture.rb', line 32 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 tables
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/sequel-fixture.rb', line 76 def push check @data.each do |table, matrix| matrix.each do |element, values| begin @connection[table].insert simplify values.to_hash rescue MissingProcessedValueError => m rollback raise MissingProcessedValueError, "In record '#{element}' to be inserted into '#{table}', the processed value of field '#{m.field}' is missing, aborting" end end end end |
#rollback ⇒ Object
Empties the tables, only if they were empty to begin with
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/sequel-fixture.rb', line 92 def rollback begin check @data.each_key do |table| @connection[table].truncate end rescue TablesNotEmptyError => e raise RollbackIllegalError, "The tables weren't empty to begin with, rollback aborted." end end |
#simplify(the_hash) ⇒ Object
Simplifies the hash in order to insert it into the database (Note: I’m well aware that this functionality belongs in a dependency)
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/sequel-fixture.rb', line 116 def simplify the_hash the_returned_hash = {} the_hash.each do |key, value| if value.is_a? Hash 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 the_returned_hash[key] = value end end return the_returned_hash end |