Module: DbAgile::Environment::Repository

Included in:
DbAgile::Environment
Defined in:
lib/dbagile/environment/repository.rb

Instance Method Summary collapse

Instance Method Details

#each_database(&block) ⇒ Object

Yields the block with each database in turn

As this method is a wrapper on repository, it shares the specification about exceptions.

Raises:

  • ArgumentError if no block is provided



72
73
74
75
# File 'lib/dbagile/environment/repository.rb', line 72

def each_database(&block)
  raise ArgumentError, "Missing block" unless block_given?
  repository.each(&block)
end

#friendly_repository_pathObject

Returns a friendly path for the repository, to display to the user. If a repository is loaded, the call is delegated. Otherwise, the repository_path is displayed, without attempting to load repository.



23
24
25
26
27
28
29
# File 'lib/dbagile/environment/repository.rb', line 23

def friendly_repository_path
  if @repository
    @repository.friendly_path
  else
    DbAgile::FileSystemTools::friendly_path!(repository_path)
  end
end

#repositoryRepository

Ensures that repository is loaded and returns the Repository instance.

ATTENTION: the Repository instance is kept in cache. It will not be synchronized with modifications of the underlying files made by another process/thread.

Returns:

Raises:

  • IOError if the repository does not exists or something goes wrong with repository dir/files

  • CorruptedRepositoryError if something goes wrong when parsing the repository index file

See Also:

  • Core::Repository::load


60
61
62
# File 'lib/dbagile/environment/repository.rb', line 60

def repository
  @repository ||= DbAgile::Core::Repository::load(repository_path)
end

#repository_exists?Boolean

Checks if the repository exists or should be created.

If the repository is currently loaded, this method returns true. Otherwise it returns true if the repository path exists. Note that repository consistency itself (index file) is not checked. You have to load it and catch exception to have a finer grained info.

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/dbagile/environment/repository.rb', line 13

def repository_exists?
  return true if @repository
  File.exists?(repository_path)
end

#repository_pathObject

Returns path to the repository



34
35
36
# File 'lib/dbagile/environment/repository.rb', line 34

def repository_path
  @repository_path
end

#repository_path=(path) ⇒ Object

Sets the path to the .dbagile file



41
42
43
44
# File 'lib/dbagile/environment/repository.rb', line 41

def repository_path=(path)
  @repository = nil
  @repository_path = path
end

#with_connection(db, conn_options = {}, &block) ⇒ Object

Yields the block with a connection on a given database; diconnect after that.

As this method relies on repository, it shares its exception contract.

Returns:

  • block execution result

Raises:

  • ArgumentError if no block is provided

  • NoSuchDatabaseError if the database cannot be found.



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/dbagile/environment/repository.rb', line 134

def with_connection(db, conn_options = {}, &block)
  case db
    when Symbol
      db = repository.database(db)
    when DbAgile::Core::Database
    else
      raise ArgumentError, "Invalid database name #{db}"
  end
  raise NoSuchDatabaseError if db.nil?
  db.with_connection(conn_options, &block)
end

#with_current_connection(conn_options = {}, &block) ⇒ Object

Yields the block with a connection on the current database.

Same contract as with_connection, expect for parameters.

Raises:

  • NoDefaultDatabaseError if the database cannot be found.



153
154
155
156
157
# File 'lib/dbagile/environment/repository.rb', line 153

def with_current_connection(conn_options = {}, &block)
  with_current_database{|db|
    with_connection(db, conn_options, &block)
  }
end

#with_current_database {|db| ... } ⇒ Object

Yields the block with the Database instance for the current one in repository.

As this method relies on repository, it shares its exception contract.

Yields:

  • (db)

Returns:

  • block execution result

Raises:

  • ArgumentError if no block is provided

  • NoDefaultDatabaseError if the database cannot be found.



118
119
120
121
122
123
# File 'lib/dbagile/environment/repository.rb', line 118

def with_current_database
  raise ArgumentError, "Missing block" unless block_given?
  db = repository.current_database
  raise DbAgile::NoDefaultDatabaseError, "No default database set (try 'dba use ...' first)" if db.nil?
  yield(db)
end

#with_database(name) {|db| ... } ⇒ Object

Yields the block with a Database instance found by name in the repository

As this method relies on repository, it shares its exception contract.

Yields:

  • (db)

Returns:

  • block execution result

Raises:

  • ArgumentError if no block is provided

  • NoSuchDatabaseError if the database cannot be found.



101
102
103
104
105
106
# File 'lib/dbagile/environment/repository.rb', line 101

def with_database(name)
  raise ArgumentError, "Missing block" unless block_given?
  db = repository.database(name)
  raise NoSuchDatabaseError if db.nil?
  yield(db)
end

#with_repository {|repository| ... } ⇒ ...

Yields the block with the Repository instance (loaded using repository)

As this method is a wrapper on repository, it shares the specification about exceptions.

Yields:

Returns:

  • (...)

    result of the block execution

Raises:

  • ArgumentError if no block is provided



86
87
88
89
# File 'lib/dbagile/environment/repository.rb', line 86

def with_repository
  raise ArgumentError, "Missing block" unless block_given?
  yield(repository)
end