Class: RCheck::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/rcheck/command.rb

Constant Summary collapse

DEFINITIONS =

Command definition dictionary that stores named definitions

{}
HIDDEN =

Command definitions not included in listings

[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, desc, *args) ⇒ Command

Returns a new instance of Command.



49
50
51
52
53
54
55
56
# File 'lib/rcheck/command.rb', line 49

def initialize(name, desc, *args)
  @name             = name  # if this is a named command definition
  @desc             = desc  # help text
  @implied_commands = []
  @config           = {}
  @cache            = {}
  expand args
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



47
48
49
# File 'lib/rcheck/command.rb', line 47

def config
  @config
end

#descObject (readonly)

Returns the value of attribute desc.



47
48
49
# File 'lib/rcheck/command.rb', line 47

def desc
  @desc
end

#implied_commandsObject (readonly)

Returns the value of attribute implied_commands.



47
48
49
# File 'lib/rcheck/command.rb', line 47

def implied_commands
  @implied_commands
end

#nameObject (readonly)

Returns the value of attribute name.



47
48
49
# File 'lib/rcheck/command.rb', line 47

def name
  @name
end

Class Method Details

.activeObject

Return the currently running RCheck command. RCheck may only be invoked once during the life cycle of the running Ruby interpreter.



12
13
14
# File 'lib/rcheck/command.rb', line 12

def active
  @active || raise("tried to look up command outside command")
end

.active=(inv) ⇒ Object

Sets the invoked command. Can only be done once.



24
25
26
27
# File 'lib/rcheck/command.rb', line 24

def active=(inv)
  @active && raise('already invoked')
  @active = inv
end

.active?Boolean

Returns true if a command has been invoked

Returns:

  • (Boolean)


21
# File 'lib/rcheck/command.rb', line 21

def active?()     !!@active                         end

.availableObject

Returns an array of available commands



35
36
37
# File 'lib/rcheck/command.rb', line 35

def available
  DEFINITIONS.values.reject { |c| HIDDEN.include? c.name }
end

.define(name, *args) ⇒ Object

Defines a named command



30
31
32
# File 'lib/rcheck/command.rb', line 30

def define(name, *args)
  DEFINITIONS[name] = new(name, *args)
end

.find(name) ⇒ Object

Find and return a named command definition



19
# File 'lib/rcheck/command.rb', line 19

def find(name)    DEFINITIONS[name]                 end

.hide(*args) ⇒ Object

Hide a named command definition from listings



17
# File 'lib/rcheck/command.rb', line 17

def hide(*args)   HIDDEN.push(*args.flatten)        end

.invoke!(*args) ⇒ Object

Invokes an RCheck command by expanding and using the given commands and options.



41
42
43
44
# File 'lib/rcheck/command.rb', line 41

def invoke!(*args)
  args << :_default if args.none? {|a| a.is_a? Symbol } 
  new(nil, '(main)', *(args.unshift(:_base))).invoke!
end

Instance Method Details

#[](param) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/rcheck/command.rb', line 73

def [](param)
  @cache[param] ||= OptionExpander[param, read(param)]
  if @cache[param].nil?
    raise(Errors::ConfigName,
          "no configuration called #{param.inspect}")
  end; @cache[param] # TODO
end

#dispatchObject



87
88
89
90
91
92
# File 'lib/rcheck/command.rb', line 87

def dispatch
  [:pry, :how].each do |option|
    return send option if Conf[option]
  end
  run_tests
end

#howObject



94
95
96
97
98
99
100
101
102
# File 'lib/rcheck/command.rb', line 94

def how
  print 'Command chain: '
  print implied_commands.map(&:inspect).join(', ')
  puts  ' ' + @config.inspect
  puts
  Options.names.each do |name|
    puts "  %15s: #{self[name]}" % [name.to_s]
  end
end

#invoke!Object



81
82
83
84
85
# File 'lib/rcheck/command.rb', line 81

def invoke!
  self.class.active = self
  require_initializers
  dispatch
end

#pryObject



104
105
106
107
# File 'lib/rcheck/command.rb', line 104

def pry
  require 'pry'
  RCheck.binding.pry
end

#read(param) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/rcheck/command.rb', line 63

def read(param)
  @config[param].tap {|v| return v unless v.nil? }
  @implied_commands.reverse.each do |name|
    inv = self.class.find(name) ||
      raise(Errors::InvocationName,
            "unknown invocation #{name.inspect}")
    inv.read(param).tap {|v| return v unless v.nil? }
  end; nil
end

#run_testsObject



109
110
111
112
113
114
115
116
117
# File 'lib/rcheck/command.rb', line 109

def run_tests
  Colors.cputs :quiet, Array(self[:headers]).map(&:call)
  puts if self[:progress].any?
  require_test_files
  [:progress, :report].each {|v| make_space v }
  suite.report!
  exit [:pass, :pending].include?(suite.severity(:total)) ?
    self[:success_code] : self[:fail_code]
end

#suiteObject



58
59
60
61
# File 'lib/rcheck/command.rb', line 58

def suite()
  RCheck[self[:suite]] ||
    raise(Errors::NoSuchSuite, "no suite called #{self[:suite]}")
end