Class: Rookie::Tasks::Console

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/rookie/tasks/console.rb

Overview

This task provides an easy way to interactively test your gem by automatically loading and requiring it in an interactive ruby session.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec, opts = {}) {|_self| ... } ⇒ Console

Creates a new console task with the given parameters. Options may specify:

:program

the name of the program to invoke; irb by default.

:command

the full command to execute. Use if the command you want to run doesn’t take the -I and -r command line arguments.

Yields the instance if given a block.

Tasks do not get defined automatically; don’t forget to call #define_tasks!

Yields:

  • (_self)

Yield Parameters:



37
38
39
40
41
42
# File 'lib/rookie/tasks/console.rb', line 37

def initialize(spec, opts = {})
  self.spec = spec
  self.program = opts.fetch :program, :irb
  self.command = opts.fetch :command, nil
  yield self if block_given?
end

Instance Attribute Details

#commandObject

Unless set explicitly, will be automatically generated from the #program name and gem specification.



21
22
23
# File 'lib/rookie/tasks/console.rb', line 21

def command
  @command ||= generate_command_string
end

#programObject

The name of the program to invoke.



14
15
16
# File 'lib/rookie/tasks/console.rb', line 14

def program
  @program
end

#specObject

The gem specification.



11
12
13
# File 'lib/rookie/tasks/console.rb', line 11

def spec
  @spec
end

Instance Method Details

#define_tasks!Object

Defines the console task.



45
46
47
48
49
50
# File 'lib/rookie/tasks/console.rb', line 45

def define_tasks!
  desc 'Starts an interactive ruby session with the gem loaded'
  task :console do
    sh command
  end
end

#generate_command_stringObject

Generates a command string from this task’s #program name and gem specification. For example:

irb -I lib -r gem_name


56
57
58
59
60
61
62
63
# File 'lib/rookie/tasks/console.rb', line 56

def generate_command_string
  program.to_s.dup.tap do |command_string|
    spec.require_paths.each do |path|
      command_string << ' -I ' << path
    end
    command_string << ' -r ' << spec.name
  end
end