Class: Mongify::CLI::WorkerCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/mongify/cli/worker_command.rb

Overview

A command to run the different commands in the application (related to Mongifying).

Constant Summary collapse

AVAILABLE_COMMANDS =

A hash of available commands Including description, additional shortcuts to run the commands and requirements to run the command

{
  :check => {:commands => ['check', 'ck'], :description => "Checks connection for sql and no_sql databases", :required => [:configuration_file]},
  :translation => {:commands => ['translation', 'tr'], :description => "Outputs a translation file from a sql connection", :required => [:configuration_file]},
  :process => {:commands => ['process', 'pr'], :description => "Takes a translation and process it to mongodb", :required => [:configuration_file, :translation_file]}
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, config = nil, translation_file = nil, parser = "") ⇒ WorkerCommand

Returns a new instance of WorkerCommand.



35
36
37
38
39
40
# File 'lib/mongify/cli/worker_command.rb', line 35

def initialize(command, config=nil, translation_file=nil, parser="")
  @command = command.to_s.downcase
  @config = config
  @translation_file = translation_file
  @parser = parser
end

Instance Attribute Details

#viewObject

Returns the value of attribute view.



8
9
10
# File 'lib/mongify/cli/worker_command.rb', line 8

def view
  @view
end

Class Method Details

.find_command(command) ⇒ Object

Finds a command array by any of the shortcut commands



28
29
30
31
32
33
# File 'lib/mongify/cli/worker_command.rb', line 28

def self.find_command(command)
  AVAILABLE_COMMANDS.each do |key, options|
    return [key, options] if(options[:commands].include?(command.to_s.downcase))
  end
  'unknown'
end

.list_commandsObject

Prints out a nice display of the list of commands



19
20
21
22
23
24
25
# File 'lib/mongify/cli/worker_command.rb', line 19

def self.list_commands
  [].tap do |commands|
    AVAILABLE_COMMANDS.each do |key, obj|
      commands << "#{obj[:commands].map{|w| %["#{w}"]}.to_sentence(:two_words_connector => ' or ', :last_word_connector => ', or ').ljust(25)} >> #{obj[:description]}#{ " [#{obj[:required].join(', ')}]" if obj[:required]}"
    end
  end.sort
end

Instance Method Details

#execute(view) ⇒ Object

Executes the worked based on a given command



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mongify/cli/worker_command.rb', line 43

def execute(view)
  self.view = view
  
  current_command, command_options = find_command
  
  if command_options
    #FIXME: Should parse configuration file in this action, (when I know it's required)
    raise ConfigurationFileNotFound, "Configuration file is required" if command_options[:required] && command_options[:required].include?(:configuration_file) && @config.nil?
    if command_options[:required] && command_options[:required].include?(:translation_file)
      raise TranslationFileNotFound, "Translation file is required for command '#{current_command}'" unless @translation_file
      raise TranslationFileNotFound, "Unable to find Translation File at #{@translation_file}" unless File.exists?(@translation_file)
      @translation = Translation.parse(@translation_file)
    end
  end
  
  case current_command
  when :translation
    check_connections
    view.output(Mongify::Translation.load(@config.sql_connection).print)
  when :check
    view.output("SQL connection works") if check_sql_connection
    view.output("NoSQL connection works") if check_nosql_connection
  when :process
    check_connections
    @translation.process(@config.sql_connection, @config.no_sql_connection)
  else
    view.output("Unknown action #{@command}\n\n#{@parser}")
    view.report_error
    return
  end
  view.report_success
end

#find_command(command = @command) ⇒ Object

Passes find command to parent class



77
78
79
# File 'lib/mongify/cli/worker_command.rb', line 77

def find_command(command=@command)
  self.class.find_command(command)
end