Skip to content

Commit

Permalink
Extract interactive repository creation in a builder class.
Browse files Browse the repository at this point in the history
  • Loading branch information
blambeau committed Aug 1, 2010
1 parent b2e06d2 commit f26293b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 47 deletions.
2 changes: 1 addition & 1 deletion lib/dbagile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ def default_environment
end # module DbAgile
require 'dbagile/robustness'
require 'dbagile/errors'
require 'dbagile/environment'
require 'dbagile/contract'
require 'dbagile/tools'
require 'dbagile/io'
require 'dbagile/core'
require 'dbagile/adapter'
require 'dbagile/plugin'
require 'dbagile/environment'
require 'dbagile/command'
47 changes: 1 addition & 46 deletions lib/dbagile/command/dba.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def unsecure_run(requester_file, argv)
def invoke_subcommand(requester_file, argv)
# Command execution
if argv.size >= 1
ensure_repository{
DbAgile::Core::Repository::Builder.new(environment).ensure_repository{
command = has_command!(argv.shift, environment)
command.run(requester_file, argv)
}
Expand All @@ -115,51 +115,6 @@ def invoke_subcommand(requester_file, argv)
end
end

#
# Ensures that the repository exists. If not, ask the user about creating
# one in interactive mode; raises an error otherwise. Continues execution
# with the block if required.
#
def ensure_repository(&block)
if environment.repository_exists?
block.call
elsif environment.interactive?
where = environment.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!(environment.repository_path)
say("Repository has been successfully created.")

# continue?
msg = "Do you want to continue with previous command execution?"
confirm(msg, &block)
}
else
# to force an error
environment.repository
end
end

# Yields the block if the user confirms something and returns block
# execution. Returns nil otherwise
#
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/, /^q/
say("\n")
say(on_no_msg, :magenta) unless on_no_msg.nil?
when /^y/
yield
end
end

end # class DbA
end # class Command
Expand Down
1 change: 1 addition & 0 deletions lib/dbagile/core/repository.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'dbagile/core/repository/yaml_methods'
require 'dbagile/core/repository/builder'
module DbAgile
module Core
class Repository
Expand Down
67 changes: 67 additions & 0 deletions lib/dbagile/core/repository/builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module DbAgile
module Core
class Repository
class Builder
include DbAgile::Environment::Delegator

# Environment to use
attr_reader :environment

# Creates a builder instance
def initialize(env)
@environment = env
end

#
# 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.
#
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

# Yields the block if the user confirms something and returns block
# execution. Returns nil otherwise
#
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/, /^q/
say("\n")
say(on_no_msg, :magenta) unless on_no_msg.nil?
when /^y/
yield
end
end

end # class Builder
end # class Repository
end # module Core
end # module DbAgile

0 comments on commit f26293b

Please sign in to comment.