Class: Ramaze::GemSetup

Inherits:
Object show all
Defined in:
lib/ramaze/setup.rb

Overview

Class responsible for installing and loading all the gems.

Author:

Since:

Instance Method Summary (collapse)

Constructor Details

- (GemSetup) initialize(options = {}) { ... }

Creates a new instance of the class and saves the parameters that were set.

Parameters:

  • options (Hash) (defaults to: {})

    Hash containing various options to pass to the GemSetup class.

Options Hash (options):

  • :verbose (Object)

    When set to true Ramaze will log various actions such as messages about the installation process.

Yields:

  • block

Author:

  • Michael Fellinger (manveru)

Since:

  • 19-05-2009



45
46
47
48
49
50
51
# File 'lib/ramaze/setup.rb', line 45

def initialize(options = {}, &block)
  @gems = []
  @options = options.dup
  @verbose = @options.delete(:verbose)

  run(&block)
end

Instance Method Details

- (Object) gem(name, version = nil, options = {})

Adds the given gem to the list of required gems.

Examples:

gem('json', '>=1.5.1')

Parameters:

  • name (String)

    The name of the gem to load.

  • version (String) (defaults to: nil)

    The version of the gem.

  • options (Hash) (defaults to: {})

    Additional options to use when loading the gem.

Options Hash (options):

  • :lib (Object)

    The name to load the gem as.

Author:

  • Michael Fellinger (manveru)

Since:

  • 19-05-2009



80
81
82
83
84
85
86
87
88
# File 'lib/ramaze/setup.rb', line 80

def gem(name, version = nil, options = {})
  if version.respond_to?(:merge!)
    options = version
  else
    options[:version] = version
  end

  @gems << [name, options]
end

- (Object) install_gem(name, options)

Tell Rubygems to install a gem.

Parameters:

  • name (String)

    The name of the gem to activate.

  • options (Hash)

    The options to use for installing the gem.

Author:

  • Michael Fellinger (manveru)

Since:

  • 19-05-2009



135
136
137
138
139
140
141
142
# File 'lib/ramaze/setup.rb', line 135

def install_gem(name, options)
  installer = Gem::DependencyInstaller.new(options)

  temp_argv(options[:extconf]) do
    log "Installing gem #{name}"
    installer.install(name, options[:version])
  end
end

- (Object) run(&block)

Executes the data inside the block, loading all the gems and optionally installing them.

Parameters:

  • block (Proc)

    A block containing all the calls to gem().

Author:

  • Michael Fellinger (manveru)

Since:

  • 19-05-2009



61
62
63
64
65
# File 'lib/ramaze/setup.rb', line 61

def run(&block)
  return unless block_given?
  instance_eval(&block)
  setup
end

- (Object) setup

Tries to install all the gems.

Author:

  • Michael Fellinger (manveru)

Since:

  • 19-05-2009



96
97
98
99
100
101
102
103
# File 'lib/ramaze/setup.rb', line 96

def setup
  require 'rubygems'
  require 'rubygems/dependency_installer'

  @gems.each do |name, options|
    setup_gem(name, options)
  end
end

- (Object) setup_gem(name, options)

First try to activate, install and try to activate again if activation fails the first time.

Parameters:

  • name (String)

    The name of the gem to activate.

  • options (Hash)

    The options from GemSetup#initialize.

Author:

  • Michael Fellinger (manveru)

Since:

  • 19-05-2009



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ramaze/setup.rb', line 114

def setup_gem(name, options)
  version  = [options[:version]].compact
  lib_name = options[:lib] || name

  log "Activating gem #{name}"

  activate(name, lib_name, *version)
# Gem not installed yet
rescue Gem::LoadError
  install_gem(name, options)
  activate(name, lib_name, *version)
end

- (Object) temp_argv(extconf)

Prepare ARGV for rubygems installer

Parameters:

  • extconf (String)

Author:

  • Michael Fellinger (manveru)

Since:

  • 19-05-2009



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/ramaze/setup.rb', line 151

def temp_argv(extconf)
  if extconf ||= @options[:extconf]
    old_argv = ARGV.clone
    ARGV.replace(extconf.split(' '))
  end

  yield

ensure
  ARGV.replace(old_argv) if extconf
end