Class: Dumper::Stack

Inherits:
Object
  • Object
show all
Includes:
Utility::ObjectFinder
Defined in:
lib/dumper/stack.rb

Constant Summary collapse

DATABASES =
{
  :mysql      =>  Dumper::Database::MySQL,
  :postgresql =>  Dumper::Database::PostgreSQL,
  :mongodb    =>  Dumper::Database::MongoDB,
  :redis      =>  Dumper::Database::Redis,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utility::ObjectFinder

#find_instance_in_object_space

Constructor Details

#initialize(options = {}) ⇒ Stack

Returns a new instance of Stack.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dumper/stack.rb', line 14

def initialize(options = {})
  @databases = {}

  # Rackup?
  @rackup = defined?(Rack::Server) && find_instance_in_object_space(Rack::Server)

  # Rails?
  if defined?(::Rails)
    @framework = :rails
    @rails_env = Rails.env.to_s
    @rails_version = Rails::VERSION::STRING
    @is_supported_rails_version = (::Rails::VERSION::MAJOR >= 3)
  else
    @framework = :ruby
  end

  if defined?(MongoMapper)
    MongoMapper.database # Trigger to create a Mongo::DB instance
  end

  DATABASES.each do |key, klass|
    database = klass.new
    next unless database.set_config_for(@rails_env) || database.set_config_for(options[:additional_env])
    if options[key].is_a?(Hash)
      database.custom_options = options[key][:custom_options]
      database.format         = options[key][:format]
    end
    @databases[key] = database
  end

  # Which dispatcher?
  [ :puma, :unicorn, :passenger, :thin, :mongrel, :webrick, :pow, :resque ].find do |name|
    @dispatcher = send("#{name}?") ? name : nil
  end
end

Instance Attribute Details

#databasesObject

Returns the value of attribute databases.



12
13
14
# File 'lib/dumper/stack.rb', line 12

def databases
  @databases
end

#dispatcherObject

Returns the value of attribute dispatcher.



12
13
14
# File 'lib/dumper/stack.rb', line 12

def dispatcher
  @dispatcher
end

#frameworkObject

Returns the value of attribute framework.



12
13
14
# File 'lib/dumper/stack.rb', line 12

def framework
  @framework
end

#rackupObject

Returns the value of attribute rackup.



12
13
14
# File 'lib/dumper/stack.rb', line 12

def rackup
  @rackup
end

#rails_envObject

Returns the value of attribute rails_env.



12
13
14
# File 'lib/dumper/stack.rb', line 12

def rails_env
  @rails_env
end

Instance Method Details

#mongrel?Boolean

Returns:

  • (Boolean)


84
85
86
87
# File 'lib/dumper/stack.rb', line 84

def mongrel?
  # defined?(::Mongrel::HttpServer)
  @rackup and @rackup.server.to_s.demodulize == 'Mongrel'
end

#passenger?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/dumper/stack.rb', line 75

def passenger?
  defined?(::PhusionPassenger) || defined?(::Passenger::AbstractServer) || defined?(::IN_PHUSION_PASSENGER)
end

#pow?Boolean

Returns:

  • (Boolean)


94
95
96
97
# File 'lib/dumper/stack.rb', line 94

def pow?
  # https://github.com/josh/nack/blob/master/bin/nack_worker
  defined?(::Nack::Server) && find_instance_in_object_space(::Nack::Server)
end

#puma?Boolean

Dispatcher

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/dumper/stack.rb', line 66

def puma?
  defined?(::Puma::Runner) && find_instance_in_object_space(::Puma::Runner) || # puma 2.3.0 or later
    (@rackup && @rackup.server.to_s.demodulize == 'Puma')
end

#resque?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/dumper/stack.rb', line 99

def resque?
  defined?(::Resque) && (ENV['QUEUES'] || ENV['QUEUE'])
end

#supported?Boolean

Compatibility

Returns:

  • (Boolean)


61
62
63
# File 'lib/dumper/stack.rb', line 61

def supported?
  @is_supported_rails_version && !!@dispatcher && !@databases.empty?
end

#thin?Boolean

Returns:

  • (Boolean)


79
80
81
82
# File 'lib/dumper/stack.rb', line 79

def thin?
  defined?(::Thin::Server) && find_instance_in_object_space(::Thin::Server) ||
    (@rackup && @rackup.server.to_s.demodulize == 'Thin')
end

#to_hashObject



50
51
52
53
54
55
56
57
58
# File 'lib/dumper/stack.rb', line 50

def to_hash
  {
    framework: @framework,
    rails_env: @rails_env,
    rails_version: @rails_version,
    dispatcher: @dispatcher,
    configs: Hash[@databases.map{|k, database| [ k, database.config.reject{|k,v| k == :password } ] }]
  }
end

#unicorn?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/dumper/stack.rb', line 71

def unicorn?
  defined?(::Unicorn::HttpServer) && find_instance_in_object_space(::Unicorn::HttpServer)
end

#webrick?Boolean

Returns:

  • (Boolean)


89
90
91
92
# File 'lib/dumper/stack.rb', line 89

def webrick?
  # defined?(::WEBrick::VERSION)
  @rackup and @rackup.server.to_s.demodulize == 'WEBrick'
end