Class: Mongify::CLI::Command::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/mongify/cli/command/worker.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]},
  :sync => {:commands => ['sync', 'sy'], :description => "Takes a translation and process it to mongodb, only syncs (insert/update) new or updated records based on the updated_at column", :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 = "") ⇒ Worker

Returns a new instance of Worker.



40
41
42
43
44
45
# File 'lib/mongify/cli/command/worker.rb', line 40

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/command/worker.rb', line 8

def view
  @view
end

Class Method Details

.find_command(command) ⇒ Object

Finds a command array by any of the shortcut commands



33
34
35
36
37
38
# File 'lib/mongify/cli/command/worker.rb', line 33

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



24
25
26
27
28
29
30
# File 'lib/mongify/cli/command/worker.rb', line 24

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



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
75
76
77
78
79
80
81
# File 'lib/mongify/cli/command/worker.rb', line 48

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, "Database Configuration file is missing or cannot be found" 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)
  when :sync
    check_connections
    @translation.sync(@config.sql_connection, @config.no_sql_connection)
  else
    view.output("Unknown action #{@command}\n\n#{@parser}")
    return view.report_error
  end
  view.report_success
end

#find_command(command = @command) ⇒ Object

Passes find command to parent class



84
85
86
# File 'lib/mongify/cli/command/worker.rb', line 84

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