Class: Gamera::Builders::SequelFixtureBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/gamera/builders/sequel_fixture_builder.rb

Overview

A builder for loading YAML fixtures using a Sequel DB connection.

Usage:

Gamera::Builders::SequelFixtureBuilder.new(
  database_config:          "/path/to/database.yml",
  fixture_directory:        "/path/to/fixtures/",
  database_cleaner_options: { skip: false, tables: ['users', 'messages'] }
).build

Or:

Gamera::Builders::SequelFixtureBuilder.new.
  with_database_config("/path/to/database.yml").
  with_fixture_directory("/path/to/fixtures/").
  with_database_cleaner_options({ skip: false, tables: ['users', 'messages'] }).
  build

Defaults:

database_config:    ./config/database.yml
fixture_directory:  ./spec/fixtures/ or ./test/fixtures/
database_cleaner_options:
  skip:   false
  tables: (all tables in the database)

So if you follow these defaults, you can simply do:

Gamera::Builders::SequelFixtureBuilder.new.build

Constant Summary collapse

DEFAULT_DATABASE_CONFIG =
'./config/database.yml'
DEFAULT_SPEC_FIXTURE_DIRECTORY =
'./spec/fixtures'
DEFAULT_TEST_FIXTURE_DIRECTORY =
'./test/fixtures'

Instance Method Summary collapse

Instance Method Details

#buildObject

Truncates the database and imports the fixtures. Returns a Sequel::Fixture object, containing hashes of all the fixtures by table name (github.com/whitepages/sequel-fixture).


71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gamera/builders/sequel_fixture_builder.rb', line 71

def build
  # Truncate all tables
  unless skip_database_cleaner
    cleaner = Utils::DatabaseCleaner.new(db, database_cleaner_tables)
    cleaner.clean
  end

  fixture_path, fixture_dir = File.split(path_to_fixtures)

  Sequel::Fixture.path = fixture_path

  Sequel::Fixture.new(fixture_dir.to_sym, db)
end

#dbObject

The Sequel database connection. Raises Gamera::DatabaseNotConfigured if it fails to initialize the database from the given config or defaults.


89
90
91
# File 'lib/gamera/builders/sequel_fixture_builder.rb', line 89

def db
  @db ||= self.class.db(database_config)
end

#path_to_fixturesObject

Finds the full path to the fixtures directory. Uses the given fixture_directory if given. Otherwise tries to use ./spec/fixtures or ./test/fixtures.

Raises Gamera::DatabaseNotConfigured if it cannot find


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/gamera/builders/sequel_fixture_builder.rb', line 98

def path_to_fixtures
  @path_to_fixtures ||= begin
    if fixture_directory && !fixture_directory.empty?
      unless File.exist?(fixture_directory)
        fail DatabaseNotConfigured, "Invalid fixture directory #{fixture_directory}"
      end
      fixture_directory
    elsif File.exist?(DEFAULT_SPEC_FIXTURE_DIRECTORY)
      DEFAULT_SPEC_FIXTURE_DIRECTORY
    elsif File.exist?(DEFAULT_TEST_FIXTURE_DIRECTORY)
      DEFAULT_TEST_FIXTURE_DIRECTORY
    else
      fail DatabaseNotConfigured, 'Unable to find fixtures to load'
    end
  end
end