Class: Recog::MatchReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/recog/match_reporter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, formatter) ⇒ MatchReporter

Returns a new instance of MatchReporter.



9
10
11
12
13
# File 'lib/recog/match_reporter.rb', line 9

def initialize(options, formatter)
  @options = options
  @formatter = formatter
  reset_counts
end

Instance Attribute Details

#fail_countObject (readonly)

Returns the value of attribute fail_count.



7
8
9
# File 'lib/recog/match_reporter.rb', line 7

def fail_count
  @fail_count
end

#formatterObject (readonly)

Returns the value of attribute formatter.



7
8
9
# File 'lib/recog/match_reporter.rb', line 7

def formatter
  @formatter
end

#line_countObject (readonly)

Returns the value of attribute line_count.



7
8
9
# File 'lib/recog/match_reporter.rb', line 7

def line_count
  @line_count
end

#match_countObject (readonly)

Returns the value of attribute match_count.



7
8
9
# File 'lib/recog/match_reporter.rb', line 7

def match_count
  @match_count
end

Instance Method Details

#failure(text) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/recog/match_reporter.rb', line 54

def failure(text)
  @fail_count += 1
  if @options.json_format
    new_object = {
      'data' => text,
      'match_failure' => true
    }
    if @options.multi_match
      new_object['matches'] = nil
    else
      new_object['match'] = nil
    end
    msg = new_object.to_json
  else
    msg = "FAIL: #{text}"
  end
  formatter.failure_message(msg.to_s)
end

#increment_line_countObject



27
28
29
# File 'lib/recog/match_reporter.rb', line 27

def increment_line_count
  @line_count += 1
end

#match(match_data) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/recog/match_reporter.rb', line 31

def match(match_data)
  @match_count += 1
  if @options.json_format
    # remove data field from all matches and promote to a top-level field
    data_field = match_data[0]['data']
    match_data.each { |h| h.delete('data') }
    new_object = {
      'data' => data_field
    }

    if @options.multi_match
      new_object['matches'] = match_data
    else
      new_object['match'] = match_data[0]
    end
    msg = new_object.to_json
  else
    match_prefix = match_data.size > 1 ? 'MATCHES' : 'MATCH'
    msg = "#{match_prefix}: #{match_data.map(&:inspect).join(',')}"
  end
  formatter.success_message(msg.to_s)
end


73
74
75
# File 'lib/recog/match_reporter.rb', line 73

def print_summary
  colorize_summary(summary_line)
end

#report {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



15
16
17
18
19
# File 'lib/recog/match_reporter.rb', line 15

def report
  reset_counts
  yield self
  summarize unless @options.quiet
end

#stop?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/recog/match_reporter.rb', line 21

def stop?
  return false unless @options.fail_fast

  @fail_count >= @options.stop_after
end