Class: DbAgile::Core::Repository::Builder

Inherits:
Object
  • Object
show all
Includes:
Environment::Delegator
Defined in:
lib/dbagile/core/repository/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Builder

Creates a builder instance



11
12
13
# File 'lib/dbagile/core/repository/builder.rb', line 11

def initialize(env)
  @environment = env
end

Instance Attribute Details

#environmentObject (readonly)

Environment to use



8
9
10
# File 'lib/dbagile/core/repository/builder.rb', line 8

def environment
  @environment
end

Instance Method Details

#confirm(msg, on_no_msg = nil) ⇒ Object

Yields the block if the user confirms something and returns block execution. Returns nil otherwise



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dbagile/core/repository/builder.rb', line 51

def confirm(msg, on_no_msg = nil)
  say("\n")
  say(msg, :magenta)
  answer = environment.ask(""){|q| q.validate = /^y(es)?|n(o)?|q(uit)?/i}
  case answer.strip
    when /^n/i, /^q/i
      say("\n")
      say(on_no_msg, :magenta) unless on_no_msg.nil?
    when /^y/i
      yield
  end
end

#ensure_repository(&block) ⇒ Object

Ensures that the repository exists. If not, ask the user about creating one in interactive mode; raises an error otherwise. Continues execution with the provided block if required.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dbagile/core/repository/builder.rb', line 20

def ensure_repository(&block)
  env = self.environment
  if env.repository_exists?
    block.call if block
  elsif env.interactive?
    where = env.friendly_repository_path
    msg = <<-EOF.gsub(/^\s*\| ?/, '')
    | DbAgile's repository #{where} does not exist. Maybe it's the first time you
    | lauch dba. Do you want to create a fresh repository now?
    EOF
    confirm(msg, "Have a look at 'dba help repo:create'"){
      # create it!
      say("Creating repository #{where}...")
      DbAgile::Core::Repository::create!(env.repository_path)
      say("Repository has been successfully created.")
    
      # continue?
      if block
        msg = "Do you want to continue with previous command execution?"
        confirm(msg, &block)
      end
    }
  else
    # to force an error
    environment.repository
  end
end