Class: Sequel::Fixture
- Inherits:
-
Object
- Object
- Sequel::Fixture
- Defined in:
- lib/sequel-fixture.rb,
lib/sequel-fixture/util.rb,
lib/sequel-fixture/table.rb,
lib/sequel-fixture/version.rb,
lib/sequel-fixture/exceptions.rb
Defined Under Namespace
Classes: ChangingConnectionIllegal, IllegalFixtureFormat, LoadingFixtureIllegal, MissingConnectionError, MissingFixtureError, MissingProcessedValueError, RollbackIllegalError, Row, Table, TablesNotEmptyError
Constant Summary collapse
- VERSION =
"2.0.1"
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Class Method Summary collapse
-
.path ⇒ Object
Description Returns the current path to the fixtures folder.
-
.path=(path) ⇒ Object
Description Set the current path of the fixtures folder.
Instance Method Summary collapse
-
#[](reference) ⇒ Object
Description Returns the SymbolMatrix with the data referring to that table.
-
#check ⇒ Object
Description Assures that the tables are empty before proceeding.
-
#fixtures_path ⇒ Object
Description Returns the current fixtures path where Sequel::Fixture looks for fixture folders.
-
#force_checked! ⇒ Object
Description Forces the check to pass.
-
#initialize(fixture = nil, connection = nil, option_push = true) ⇒ Fixture
constructor
Description Initializes the fixture handler Accepts optionally a symbol as a reference to the fixture and a Sequel::Database connection.
-
#load(fixture) ⇒ Object
Description Loads the fixture files into this instance.
-
#method_missing(key, *args) ⇒ Object
Description For enabling discovery of tables.
-
#push ⇒ Object
Description Initializes fixture schema and Inserts the fixture data into the corresponding tables.
-
#push_schema(table, values) ⇒ Object
Description Create the schema in our DB connection based on the schema values.
-
#rollback ⇒ Object
Description Empties the tables, only if they were empty to begin with.
-
#simplify(the_hash) ⇒ Object
Description 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
Description
Initializes the fixture handler Accepts optionally a symbol as a reference to the fixture and a Sequel::Database connection
33 34 35 36 37 38 39 40 41 |
# File 'lib/sequel-fixture.rb', line 33 def initialize(fixture = nil, connection = nil, option_push = true) @schema = {} @data = {} 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(key, *args) ⇒ Object
Description
For enabling discovery of tables
75 76 77 78 |
# File 'lib/sequel-fixture.rb', line 75 def method_missing(key, *args) return @data[key] if @data && @data.has_key?(key) return super end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
189 190 191 |
# File 'lib/sequel-fixture.rb', line 189 def connection @connection end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
190 191 192 |
# File 'lib/sequel-fixture.rb', line 190 def data @data end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
191 192 193 |
# File 'lib/sequel-fixture.rb', line 191 def schema @schema end |
Class Method Details
.path ⇒ Object
Description
Returns the current path to the fixtures folder
17 18 19 |
# File 'lib/sequel-fixture.rb', line 17 def self.path @@path ||= "test/fixtures" end |
.path=(path) ⇒ Object
Description
Set the current path of the fixtures folder
25 26 27 |
# File 'lib/sequel-fixture.rb', line 25 def self.path=(path) @@path = path end |
Instance Method Details
#[](reference) ⇒ Object
Description
Returns the SymbolMatrix with the data referring to that table
83 84 85 |
# File 'lib/sequel-fixture.rb', line 83 def [](reference) @data[reference] end |
#check ⇒ Object
Description
Assures that the tables are empty before proceeding
99 100 101 102 103 104 105 106 |
# File 'lib/sequel-fixture.rb', line 99 def check return @checked if @checked # If already checked, it's alright raise MissingFixtureError, "No fixture has been loaded, nothing to check" unless @data.length > 0 raise MissingConnectionError, "No connection has been provided, impossible to check" unless @connection return @checked = true end |
#fixtures_path ⇒ Object
Description
Returns the current fixtures path where Sequel::Fixture looks for fixture folders
67 68 69 |
# File 'lib/sequel-fixture.rb', line 67 def fixtures_path Sequel::Fixture.path end |
#force_checked! ⇒ Object
Description
Forces the check to pass. Dangerous!
91 92 93 |
# File 'lib/sequel-fixture.rb', line 91 def force_checked! @checked = true end |
#load(fixture) ⇒ Object
Description
Loads the fixture files into this instance
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/sequel-fixture.rb', line 47 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 ||= {} @schema ||= {} file_data = SymbolMatrix.new "#{fixtures_path}/#{fixture}/#{file}.yaml" if file_data @data[file] = Table.new(file_data[:data]) if file_data.key?(:data) @schema[file] = file_data[:schema] if file_data.key?(:schema) end end end |
#push ⇒ Object
Description
Initializes fixture schema and Inserts the fixture data into the corresponding tables
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/sequel-fixture.rb', line 113 def push check @schema.each do |table, matrix| push_schema(table, matrix) end @data.each do |table_name, table_data| table_data.rows.each do |values| begin @connection[table_name].insert(simplify(values.to_h)) rescue MissingProcessedValueError => m rollback raise MissingProcessedValueError, "In record '#{values.to_h}' to be inserted into '#{table_name}', the processed value of field '#{m.field}' is missing, aborting." rescue NoMethodError => e raise IllegalFixtureFormat, "In record '#{values}', data must be formatted as arrays of hashes. Check 'data' section in '#{table_name}.yaml'" end end end end |
#push_schema(table, values) ⇒ Object
Description
Create the schema in our DB connection based on the schema values
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/sequel-fixture.rb', line 138 def push_schema(table, values) ## Lets passively ignore the schema if the table already exists return if @connection.table_exists?(table.to_sym) ## Find the primary key pkey_data = nil values.each do |column_def| pkey_data = column_def if column_def["primary_key"] end ## Create the table with the primary key @connection.create_table(table) do column(pkey_data["name"].to_sym, pkey_data["type"].to_sym) end ## Add the rest of the columns values.each do |column_def| unless column_def["primary_key"] @connection.alter_table(table) { add_column(column_def["name"].to_sym, column_def["type"].to_sym) } end end end |
#rollback ⇒ Object
Description
Empties the tables, only if they were empty to begin with
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/sequel-fixture.rb', line 165 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
Description
Simplifies the hash in order to insert it into the database (Note: I’m well aware that this functionality belongs in a dependency)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/sequel-fixture/util.rb', line 7 def simplify(the_hash) the_returned_hash = {} the_hash.each do |key, value| if value.is_a? Hash unless value.has_key?("processed") || 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"] || value[:processed] else the_returned_hash[key] = value end end return the_returned_hash end |