Class: Exemplar::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/exemplar/runner.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(examples, options = nil) ⇒ Runner

Returns a new instance of Runner.



7
8
9
10
# File 'lib/exemplar/runner.rb', line 7

def initialize(examples, options = nil)
  @examples = examples
  @match_regex = options && options[:match]
end

Instance Attribute Details

#examplesObject (readonly)

Returns the value of attribute examples.



5
6
7
# File 'lib/exemplar/runner.rb', line 5

def examples
  @examples
end

Class Method Details

.parse_options!(argv) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/exemplar/runner.rb', line 20

def self.parse_options!(argv)
  options = {}

  OptionParser.new do |opt|
    opt.banner = "Exemplar automatic example runner.\n"
    opt.banner << "Usage: #{$0} [options]"

    opt.on
    opt.on("-e", "--example [REGEX]", "Run examples matching the regex.") do |regex|
      options[:match] = Regexp.new(regex)
    end

    opt.on_tail
  end.parse!(argv)

  options
rescue OptionParser::ParseError => e
  puts "%s. Run `#{$0} --help` for help." % e
  exit false
end

Instance Method Details

#runObject



12
13
14
# File 'lib/exemplar/runner.rb', line 12

def run
  examples.select { |example| run?(example) }.each { |example| example.run }
end

#run?(example) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/exemplar/runner.rb', line 16

def run?(example)
  @match_regex.nil? || example.name =~ @match_regex
end