Class: DbAgile::Environment

Inherits:
Object
  • Object
show all
Includes:
Buffering, Interactions, OnError, Repository
Defined in:
lib/dbagile/environment.rb,
lib/dbagile/environment/testing.rb,
lib/dbagile/environment/on_error.rb,
lib/dbagile/environment/buffering.rb,
lib/dbagile/environment/delegator.rb,
lib/dbagile/environment/repository.rb,
lib/dbagile/environment/interactions.rb

Overview

Defines the contract to be an environment for dbagile.

Defined Under Namespace

Modules: Buffering, Delegator, Interactions, OnError, Repository, Testing

Instance Attribute Summary

Attributes included from Interactions

#asking_buffer, #message_buffer

Attributes included from Buffering

#input_buffer, #output_buffer

Attributes included from OnError

#show_backtrace

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Repository

#each_database, #friendly_repository_path, #repository, #repository_exists?, #repository_path, #repository_path=, #with_connection, #with_current_connection, #with_current_database, #with_database, #with_repository

Methods included from Interactions

#ask, #ask!, #color, #console_width, #console_width=, #display, #interaction_highline, #interactive!, #interactive=, #interactive?, #say

Methods included from Buffering

#flush, #gets

Methods included from OnError

#on_error, #show_backtrace?

Constructor Details

#initializeEnvironment

Creates a default Environment instance with following options:

  • repository_path -> what Environment::default_repository_path returns

  • input_buffer -> STDIN

  • output_buffer -> STDOUT

  • interactive? -> true

  • asking_buffer -> STDIN

  • message_buffer -> STDERR

  • show_backtrace -> false



41
42
43
44
45
46
47
48
49
# File 'lib/dbagile/environment.rb', line 41

def initialize
  @repository_path = Environment::default_repository_path
  @input_buffer    = STDIN
  @output_buffer   = STDOUT
  @interactive     = true
  @asking_buffer   = STDIN
  @message_buffer  = STDERR
  @show_backtrace  = false
end

Class Method Details

.default(load_repository = false) ⇒ Object

Returns the default environment to use.

The algorithm implemented tries to locate a repository folder in the following order:

  • dbagile.idx file in the current directory -> it’s parent folder

  • dbagile folder in the current directory -> ./dbagile

  • .dbagile folder in user’s home -> ~/.dbagile

If none of those files exists, a default environment will be created mapping to an unexisting repository folder in ~/.dbagile

By default, the repository is not loaded at all, and errors can therefore occur later, when the repository will be first accessed. Set load_repository to true to force immediate load.



106
107
108
109
110
111
112
# File 'lib/dbagile/environment.rb', line 106

def self.default(load_repository = false)
  env = Environment.new
  if load_repository
    env.repository
  end
  env
end

.default!Object

Convenient method for Environment::default(true)



117
118
119
# File 'lib/dbagile/environment.rb', line 117

def self.default!
  Environment::default(true)
end

.default_repository_pathObject

Returns the default path to use for a repository.

The algorithm implemented tries to locate a repository folder in the following order:

  • dbagile.idx file in the current directory -> it’s parent folder

  • dbagile folder in the current directory -> ./dbagile

  • .dbagile folder in user’s home -> ~/.dbagile

If none of those files exists, ~/.dbagile is returned



79
80
81
82
83
84
85
86
87
# File 'lib/dbagile/environment.rb', line 79

def self.default_repository_path
  if File.exists?("./dbagile.idx")
    "."
  elsif File.exists?("dbagile")
    "dbagile"
  else
    File.join(ENV['HOME'], '.dbagile')
  end
end

Instance Method Details

#dupObject

Duplicates the environment but removes any cached value (repository and so on)



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dbagile/environment.rb', line 55

def dup
  env = Environment.new
  env.repository_path = self.repository_path
  env.input_buffer    = self.input_buffer
  env.output_buffer   = self.output_buffer
  env.interactive     = self.interactive?
  env.asking_buffer   = self.asking_buffer
  env.message_buffer  = self.message_buffer
  env.show_backtrace  = self.show_backtrace?
  env
end

#with_testing_methods!(interactive = true) ⇒ Object

Installs the testing methods, StringIO on output buffers then returns self



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

def with_testing_methods!(interactive = true)
  require 'dbagile/environment/testing'
  extend(DbAgile::Environment::Testing)
  self.output_buffer  = StringIO.new
  self.message_buffer = StringIO.new
  self.interactive = interactive
  self
end