Module: Exportr::Command

Extended by:
Base, Core, Helpers, Messages
Defined in:
lib/exportr/command.rb,
lib/exportr/command/core.rb,
lib/exportr/command/helpers.rb,
lib/exportr/command/messages.rb

Defined Under Namespace

Modules: Core, Helpers, Messages

Constant Summary

Constants included from Base

Base::CONFIG_FILE, Base::SYSTEM_FILE_PATH

Constants included from Messages

Messages::NOT_RAILS, Messages::NO_CONFIG_FILE

Class Method Summary collapse

Methods included from Base

config_file, extended, rails_root, system_env_file

Methods included from Core

add, clear, help, list, remove, version

Methods included from Helpers

error, in_rails_application?, log

Methods included from Messages

extended

Class Method Details

.commentsObject



72
73
74
# File 'lib/exportr/command.rb', line 72

def self.comments
  read_config.match(/(^\#.*\n)+/).to_s
end

.core_option(name, *args) ⇒ Object



22
23
24
# File 'lib/exportr/command.rb', line 22

def self.core_option name, *args
  core_options << { :name => name, :args => args }
end

.core_optionsObject



18
19
20
# File 'lib/exportr/command.rb', line 18

def self.core_options
  @global_options ||= []
end

.dump_config(vars) ⇒ Object



68
69
70
# File 'lib/exportr/command.rb', line 68

def self.dump_config vars
  YAML.dump(vars)
end

.hashify(str) ⇒ Object



76
77
78
79
80
81
# File 'lib/exportr/command.rb', line 76

def self.hashify str
  arr = str.split('=') rescue []
  hash = Hash.new
  hash[arr[0]] = arr[1]
  hash
end

.load_configObject



64
65
66
# File 'lib/exportr/command.rb', line 64

def self.load_config
  YAML.load(read_config) || Hash.new
end

.parserObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/exportr/command.rb', line 38

def self.parser
  OptionParser.new do |parser|
    parser.banner = "Usage:\n    exportr [options]"
    parser.separator "\nOptions: "
    core_options.each do |opt|
      parser.on *opt[:args] do |val| 
        send(opt[:name], hashify(val)) rescue send opt[:name]
      end
    end
  end
end

.read_configObject



50
51
52
53
# File 'lib/exportr/command.rb', line 50

def self.read_config
  error NO_CONFIG_FILE unless File.exists?(config_file)
  File.read config_file
end

.run(*argv) ⇒ Object



33
34
35
36
# File 'lib/exportr/command.rb', line 33

def self.run *argv
  error NOT_RAILS unless in_rails_application?
  argv.any? ? parser.parse!(argv) : help
end

.write_config(vars) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/exportr/command.rb', line 55

def self.write_config vars
  cm = comments
  File.open config_file, 'w+' do |f|
    f.write cm << "\n" << dump_config(vars)
  end
  log "Done."
  log
end