Class: Squixtures::Fixture
- Inherits:
-
Object
- Object
- Squixtures::Fixture
- Defined in:
- lib/squixtures/fixture.rb
Overview
This class represents a single fixtures and provides the functionality to load it’s contents into the database.
Instance Method Summary collapse
-
#file_path ⇒ Object
This method fetches the path and name of the file that the fixture will load it’s data from.
-
#initialize(name, configuration) ⇒ Fixture
constructor
Constructor for the Fixture class.
-
#load(connection) ⇒ Object
This method attempts to locate a fixture file, load it’s contents and then insert the details held with the fixture file into the database.
-
#table_name ⇒ Object
This method returns the name of the database table that the fixture will load data into.
Constructor Details
#initialize(name, configuration) ⇒ Fixture
Constructor for the Fixture class.
Parameters
- name
-
The name of the fixture. This will be used to work out the fixture file and table names.
- configuration
-
The configuration to be used by the Fixture.
20 21 22 23 |
# File 'lib/squixtures/fixture.rb', line 20 def initialize(name, configuration) @name = name @configuration = configuration end |
Instance Method Details
#file_path ⇒ Object
This method fetches the path and name of the file that the fixture will load it’s data from. This will be nil if a file cannot be located for the fixture.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/squixtures/fixture.rb', line 66 def file_path path = nil full_path = "#{@configuration[:fixtures_dir]}/#{@name}.yml" Fixture.log.debug "Checking for the existence of #{full_path}." if !File.exist?(full_path) @configuration[:search_paths].find do |entry| full_path = "#{entry}/#{@name}.yml" Fixture.log.debug "Checking for the existence of #{full_path}." path = full_path if File.exist?(full_path) !path.nil? end else path = full_path end path.nil? ? path : File.(path) end |
#load(connection) ⇒ Object
This method attempts to locate a fixture file, load it’s contents and then insert the details held with the fixture file into the database.
Parameters
- connection
-
The database connection to be used to insert the fixture details into the database.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/squixtures/fixture.rb', line 31 def load(connection) Fixture.log.debug "Loading the #{@name} fixture." fixture_path = file_path if fixture_path.nil? raise SquixtureError.new("Unable to locate a data file for the #{@name} fixture.") end begin Fixture.log.debug "Loading the data for the #{@name} fixture from #{fixture_path}." content = File.open(fixture_path, "r") {|file| file.readlines.join("")} #Fixture.log.debug "Read:\n#{content}" entries = YAML.load(ERB.new(content).result) if entries && entries.size > 0 data_set = connection[@name.intern] data_set.delete if @configuration[:clear_tables] entries.each do |key, values| data_set.insert(convert_values(values)) end end rescue => error = "Load of the #{@name} fixture failed." Fixture.log.error "#{}\nCause: #{error}\n" + error.backtrace.join("\n") raise SquixtureError.new(, error) end end |
#table_name ⇒ Object
This method returns the name of the database table that the fixture will load data into.
59 60 61 |
# File 'lib/squixtures/fixture.rb', line 59 def table_name @name.to_s end |