Class: Orats::Common

Inherits:
Object
  • Object
show all
Includes:
Postgres, Process, Redis, Shell, UI, Thor::Actions, Thor::Base, Thor::Shell
Defined in:
lib/orats/common.rb

Overview

common class that other CLI driven classes subclass

Instance Method Summary collapse

Methods included from Redis

#drop_namespace, #exit_if_redis_unreachable, #redis_bin

Methods included from Postgres

#create_database, #drop_database, #exit_if_database_exists, #exit_if_postgres_unreachable, #postgres_bin

Methods included from Process

#exit_if_process

Methods included from UI

#error, #log, #results, #task

Methods included from Shell

#commit, #run_from

Constructor Details

#initialize(target_path = '', options = {}) ⇒ Common

Returns a new instance of Common.



19
20
21
22
23
24
25
# File 'lib/orats/common.rb', line 19

def initialize(target_path = '', options = {})
  @target_path = target_path
  @options     = options

  self.destination_root = Dir.pwd
  @behavior             = :invoke
end

Instance Method Details

#base_pathObject



27
28
29
# File 'lib/orats/common.rb', line 27

def base_path
  File.join(File.expand_path(File.dirname(__FILE__)))
end

#exit_if_invalid_systemObject



62
63
64
65
66
67
68
# File 'lib/orats/common.rb', line 62

def exit_if_invalid_system
  exit_if_process :not_found, 'rails', 'git', 'psql', 'redis-cli'
  exit_if_process :not_running, 'postgres', 'redis'

  exit_if_postgres_unreachable
  exit_if_redis_unreachable
end

#exit_if_path_exists(extend_path = '') ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/orats/common.rb', line 47

def exit_if_path_exists(extend_path = '')
  task 'Check if path exists'

  extended_path = @target_path.dup

  unless extend_path.empty?
    extended_path = File.join(extended_path, extend_path)
  end

  return unless Dir.exist?(extended_path) || File.exist?(extended_path)

  error 'Path already exists', extended_path
  exit 1
end

#file_to_string(path) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/orats/common.rb', line 38

def file_to_string(path)
  if File.exist?(path) && File.file?(path)
    IO.read path
  else
    error 'Path not found', path
    exit 1
  end
end

#run_rake(command) ⇒ Object



70
71
72
73
74
# File 'lib/orats/common.rb', line 70

def run_rake(command)
  task 'Run rake command'

  run_from @target_path, "bundle exec rake #{command}"
end

#url_to_string(url) ⇒ Object



31
32
33
34
35
36
# File 'lib/orats/common.rb', line 31

def url_to_string(url)
  open(url).read
  rescue *[OpenURI::HTTPError, SocketError] => ex
    error 'Unable to access URL', ex
    exit 1
end