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
# 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])
    @databases[key] = database
  end

  # Which dispatcher?
  [ :unicorn, :passenger, :thin, :mongrel, :webrick, :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)


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

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

#passenger?Boolean

Returns:

  • (Boolean)


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

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

#resque?Boolean

Returns:

  • (Boolean)


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

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

#supported?Boolean

Compatibility

Returns:

  • (Boolean)


57
58
59
# File 'lib/dumper/stack.rb', line 57

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

#thin?Boolean

Returns:

  • (Boolean)


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

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

#to_hashObject



46
47
48
49
50
51
52
53
54
# File 'lib/dumper/stack.rb', line 46

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

Dispatcher

Returns:

  • (Boolean)


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

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

#webrick?Boolean

Returns:

  • (Boolean)


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

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