Class: Boxen::Check

Inherits:
Object
  • Object
show all
Defined in:
lib/boxen/check.rb

Overview

The superclass for preflight and postflight sanity checks.

Direct Known Subclasses

Postflight, Preflight

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Check

Returns a new instance of Check.



33
34
35
# File 'lib/boxen/check.rb', line 33

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



31
32
33
# File 'lib/boxen/check.rb', line 31

def config
  @config
end

Class Method Details

.checks(config) ⇒ Object

A collection of preflight instances for ‘config`. An instance is created for every constant under `self` that’s also a subclass of ‘self`.



13
14
15
16
17
# File 'lib/boxen/check.rb', line 13

def self.checks(config)
  constants.map { |n| const_get n }.
    select { |c| c < self }.
    map { |c| c.new config }
end

.register(dir) ⇒ Object

Search ‘dir` and load all Ruby files under it.



21
22
23
# File 'lib/boxen/check.rb', line 21

def self.register(dir)
  Dir["#{dir}/*.rb"].sort.each { |f| load f }
end

.run(config) ⇒ Object

Check each instance against ‘config`.



27
28
29
# File 'lib/boxen/check.rb', line 27

def self.run(config)
  checks(config).each { |check| check.run unless check.ok? }
end

Instance Method Details

#abort(message, *extras) ⇒ Object

A fancier ‘abort` and `warn`. This will probably really annoy someone at some point because it’s overriding a Kernel method, but it’s limited to checks.



53
54
55
56
57
# File 'lib/boxen/check.rb', line 53

def abort(message, *extras)
  extras << { :color => :red }
  warn message, *extras
  exit 1
end

#ok?Boolean

Is everything good to go? Implemented by subclasses.

Returns:

  • (Boolean)


39
40
41
# File 'lib/boxen/check.rb', line 39

def ok?
  raise "Subclasses must implement this method."
end

#runObject

Warn, fix, or abort. Implemented by subclasses.



45
46
47
# File 'lib/boxen/check.rb', line 45

def run
  raise "Subclasses must implement this method."
end

#warn(message, *extras) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/boxen/check.rb', line 59

def warn(message, *extras)
  options = Hash === extras.last ? extras.pop : {}
  color   = options[:color] || :yellow

  $stderr.puts ANSI.send(color) { "--> #{message}" }

  unless extras.empty?
    extras.each { |line| $stderr.puts "    #{line}" }
  end

  $stderr.puts
end