Class: Hiiro::Matcher::PathResult

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:, prefix:, key: nil, block: nil) ⇒ PathResult

Returns a new instance of PathResult.



230
231
232
233
234
235
236
# File 'lib/hiiro/matcher.rb', line 230

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

Instance Attribute Details

#all_itemsObject (readonly)

Returns the value of attribute all_items.



228
229
230
# File 'lib/hiiro/matcher.rb', line 228

def all_items
  @all_items
end

#blockObject (readonly)

Returns the value of attribute block.



228
229
230
# File 'lib/hiiro/matcher.rb', line 228

def block
  @block
end

#keyObject (readonly)

Returns the value of attribute key.



228
229
230
# File 'lib/hiiro/matcher.rb', line 228

def key
  @key
end

#matcherObject (readonly)

Returns the value of attribute matcher.



228
229
230
# File 'lib/hiiro/matcher.rb', line 228

def matcher
  @matcher
end

#prefixObject (readonly)

Returns the value of attribute prefix.



228
229
230
# File 'lib/hiiro/matcher.rb', line 228

def prefix
  @prefix
end

Instance Method Details

#ambiguous?Boolean

Returns:

  • (Boolean)


258
259
260
# File 'lib/hiiro/matcher.rb', line 258

def ambiguous?
  count > 1
end

#countObject



254
255
256
# File 'lib/hiiro/matcher.rb', line 254

def count
  matches.count
end

#exact?Boolean

Returns:

  • (Boolean)


274
275
276
# File 'lib/hiiro/matcher.rb', line 274

def exact?
  !exact_match.nil?
end

#exact_matchObject



262
263
264
# File 'lib/hiiro/matcher.rb', line 262

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

#firstObject

Returns the first matching item (for find semantics)



288
289
290
# File 'lib/hiiro/matcher.rb', line 288

def first
  matches.first
end

#matchObject



266
267
268
# File 'lib/hiiro/matcher.rb', line 266

def match
  one? ? matches.first : nil
end

#match?Boolean

Returns:

  • (Boolean)


270
271
272
# File 'lib/hiiro/matcher.rb', line 270

def match?
  matches.any?
end

#matchesObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/hiiro/matcher.rb', line 238

def matches
  @matches ||= begin
    prefixes = prefix.to_s.split('/')

    items_with_paths = all_items.map { |item|
      [item, item.extracted_item.to_s.split('/')]
    }

    prefixes.each_with_index do |seg, i|
      items_with_paths = items_with_paths.select { |_, path| path[i]&.start_with?(seg) }
    end

    items_with_paths.map(&:first)
  end
end

#one?Boolean

Returns:

  • (Boolean)


278
279
280
# File 'lib/hiiro/matcher.rb', line 278

def one?
  count == 1
end

#resolvedObject

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



283
284
285
# File 'lib/hiiro/matcher.rb', line 283

def resolved
  exact_match || match
end