Module: Ensurable

Defined in:
lib/ensurable.rb,
lib/ensurable/now.rb,
lib/ensurables/git.rb,
lib/ensurables/redis.rb,
lib/ensurable/version.rb

Defined Under Namespace

Modules: Now Classes: Git, NotInstalled, NotRunning, Redis

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.ensure(file = nil) { ... } ⇒ Object

Ensures that the given programs are installed/running.

Parameters:

  • optional (File)

    file to read defining the ensurables

Yields:

  • optional block to allow any ensurables to be required



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ensurable.rb', line 14

def ensure(file = nil, &bloc)
  abortables = []

  begin
    abortables << Rails.env
  rescue NameError, NoMethodError
  end
  abortables << ENV['RAILS_ENV']
  abortables << ENV['RACK_ENV']

  if abortables.any? {|a| a == 'production'}
    $stderr.puts("Ensurable gem is not meant for use in production, nooping.")
    return
  end

  if file
    instance_eval file.read
  else
    instance_eval &bloc
  end
end

.installed(prog) ⇒ Object

Ensures the the given program needs to be installed on the local system

Parameters:

  • prog (String)

    the name of the program that should be installed. Requires that the constant Ensurable::program exists and follows that API.

Raises:

  • (NotInstalled)

    raised when the given program is not installed on the local system



41
42
43
44
45
46
# File 'lib/ensurable.rb', line 41

def installed(prog)
  # Would be nice to have a nicer fail mechanism if the constant doesn't exist
  klass = const_get(prog.capitalize)

  raise NotInstalled.new("Looks like you don't have #{prog} installed.") unless klass.new.installed?
end

.running(prog) ⇒ Object

Ensures that the given program is running on the local system

Parameters:

  • prog (String)

    the name of the program that should be running. Requires that the constant Ensurable::program exists and follows that API.

Raises:

  • (NotRunning)

    raised when the given program is not running on the local system



52
53
54
55
56
# File 'lib/ensurable.rb', line 52

def running(prog)
  klass = const_get(prog.capitalize)

  raise NotRunning.new("Looks like you don't have #{prog} running.") unless klass.new.running?
end