Class: Hiiro::Matcher::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/hiiro/matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matcher:, all_items:, pattern:, match_type: :prefix, key: nil, block: nil) ⇒ Result

Returns a new instance of Result.



161
162
163
164
165
166
167
168
# File 'lib/hiiro/matcher.rb', line 161

def initialize(matcher:, all_items:, pattern:, match_type: :prefix, key: nil, block: nil)
  @matcher = matcher
  @all_items = all_items
  @pattern = pattern
  @match_type = match_type
  @key = key
  @block = block
end

Instance Attribute Details

#all_itemsObject (readonly)

Returns the value of attribute all_items.



159
160
161
# File 'lib/hiiro/matcher.rb', line 159

def all_items
  @all_items
end

#blockObject (readonly)

Returns the value of attribute block.



159
160
161
# File 'lib/hiiro/matcher.rb', line 159

def block
  @block
end

#keyObject (readonly)

Returns the value of attribute key.



159
160
161
# File 'lib/hiiro/matcher.rb', line 159

def key
  @key
end

#match_typeObject (readonly)

Returns the value of attribute match_type.



159
160
161
# File 'lib/hiiro/matcher.rb', line 159

def match_type
  @match_type
end

#matcherObject (readonly)

Returns the value of attribute matcher.



159
160
161
# File 'lib/hiiro/matcher.rb', line 159

def matcher
  @matcher
end

#patternObject (readonly)

Returns the value of attribute pattern.



159
160
161
# File 'lib/hiiro/matcher.rb', line 159

def pattern
  @pattern
end

Instance Method Details

#ambiguous?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/hiiro/matcher.rb', line 192

def ambiguous?
  count > 1
end

#countObject



188
189
190
# File 'lib/hiiro/matcher.rb', line 188

def count
  matches.count
end

#exact?Boolean

Returns:

  • (Boolean)


208
209
210
# File 'lib/hiiro/matcher.rb', line 208

def exact?
  !exact_match.nil?
end

#exact_matchObject



196
197
198
# File 'lib/hiiro/matcher.rb', line 196

def exact_match
  all_items.find { |item| item.extracted_item == pattern }
end

#firstObject

Returns the first matching item (for find semantics)



222
223
224
# File 'lib/hiiro/matcher.rb', line 222

def first
  matches.first
end

#matchObject



200
201
202
# File 'lib/hiiro/matcher.rb', line 200

def match
  one? ? matches.first : nil
end

#match?Boolean

Returns:

  • (Boolean)


204
205
206
# File 'lib/hiiro/matcher.rb', line 204

def match?
  matches.any?
end

#matchesObject



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/hiiro/matcher.rb', line 175

def matches
  @matches ||= all_items.select { |item|
    case match_type
    when :prefix
      item.extracted_item.to_s.start_with?(pattern.to_s)
    when :substring
      item.extracted_item.to_s.include?(pattern.to_s)
    else
      item.extracted_item.to_s.start_with?(pattern.to_s)
    end
  }
end

#one?Boolean

Returns:

  • (Boolean)


212
213
214
# File 'lib/hiiro/matcher.rb', line 212

def one?
  count == 1
end

#prefixObject

Alias for backward compatibility



171
172
173
# File 'lib/hiiro/matcher.rb', line 171

def prefix
  pattern
end

#resolvedObject

Returns item for resolve semantics: exact match, or single match, otherwise nil



217
218
219
# File 'lib/hiiro/matcher.rb', line 217

def resolved
  exact_match || match
end