Class: Omnizip::Models::MatchResult

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/omnizip/models/match_result.rb

Overview

Represents the result of pattern matching against archive entries

Contains matched entries with metadata about the matching process.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, matches: [], total_scanned: 0) ⇒ MatchResult

Initialize a new match result

Parameters:

  • pattern (Object)

    The pattern that was matched

  • matches (Array) (defaults to: [])

    Array of matched entries

  • total_scanned (Integer) (defaults to: 0)

    Total entries scanned



18
19
20
21
22
# File 'lib/omnizip/models/match_result.rb', line 18

def initialize(pattern, matches: [], total_scanned: 0)
  @pattern = pattern
  @matches = Array(matches)
  @total_scanned = total_scanned
end

Instance Attribute Details

#matchesObject (readonly)

Returns the value of attribute matches.



11
12
13
# File 'lib/omnizip/models/match_result.rb', line 11

def matches
  @matches
end

#patternObject (readonly)

Returns the value of attribute pattern.



11
12
13
# File 'lib/omnizip/models/match_result.rb', line 11

def pattern
  @pattern
end

#total_scannedObject (readonly)

Returns the value of attribute total_scanned.



11
12
13
# File 'lib/omnizip/models/match_result.rb', line 11

def total_scanned
  @total_scanned
end

Instance Method Details

#add_match(entry) ⇒ self

Add a matched entry

Parameters:

  • entry (Object)

    Entry that matched

Returns:

  • (self)


28
29
30
31
# File 'lib/omnizip/models/match_result.rb', line 28

def add_match(entry)
  @matches << entry
  self
end

#any?Boolean

Check if any matches were found

Returns:

  • (Boolean)


52
53
54
# File 'lib/omnizip/models/match_result.rb', line 52

def any?
  !@matches.empty?
end

#countInteger

Get the number of matches

Returns:

  • (Integer)


45
46
47
# File 'lib/omnizip/models/match_result.rb', line 45

def count
  @matches.size
end

#each {|entry| ... } ⇒ Enumerator, self

Iterate over matches

Yields:

  • (entry)

    Each matched entry

Returns:

  • (Enumerator, self)


83
84
85
86
87
88
# File 'lib/omnizip/models/match_result.rb', line 83

def each(&block)
  return matches.to_enum unless block

  matches.each(&block)
  self
end

#firstObject?

Get first match

Returns:

  • (Object, nil)


93
94
95
# File 'lib/omnizip/models/match_result.rb', line 93

def first
  @matches.first
end

#increment_scanned(count = 1) ⇒ self

Increment the scan counter

Parameters:

  • count (Integer) (defaults to: 1)

    Number to increment by

Returns:

  • (self)


37
38
39
40
# File 'lib/omnizip/models/match_result.rb', line 37

def increment_scanned(count = 1)
  @total_scanned += count
  self
end

#lastObject?

Get last match

Returns:

  • (Object, nil)


100
101
102
# File 'lib/omnizip/models/match_result.rb', line 100

def last
  @matches.last
end

#match_percentageFloat

Get match percentage

Returns:

  • (Float)

    Match percentage between 0.0 and 100.0



75
76
77
# File 'lib/omnizip/models/match_result.rb', line 75

def match_percentage
  match_rate * 100.0
end

#match_rateFloat

Get match rate (matches/scanned)

Returns:

  • (Float)

    Match rate between 0.0 and 1.0



66
67
68
69
70
# File 'lib/omnizip/models/match_result.rb', line 66

def match_rate
  return 0.0 if @total_scanned.zero?

  count.to_f / @total_scanned
end

#none?Boolean

Check if no matches were found

Returns:

  • (Boolean)


59
60
61
# File 'lib/omnizip/models/match_result.rb', line 59

def none?
  @matches.empty?
end

#to_aArray

Convert to array

Returns:

  • (Array)


107
108
109
# File 'lib/omnizip/models/match_result.rb', line 107

def to_a
  @matches.dup
end

#to_hHash

Get summary hash

Returns:

  • (Hash)


114
115
116
117
118
119
120
121
# File 'lib/omnizip/models/match_result.rb', line 114

def to_h
  {
    pattern: @pattern.to_s,
    matches: count,
    scanned: @total_scanned,
    match_rate: match_rate,
  }
end