Class: Hiiro::Matcher::PathResult
- Inherits:
-
Object
- Object
- Hiiro::Matcher::PathResult
- Defined in:
- lib/hiiro/matcher.rb
Instance Attribute Summary collapse
-
#all_items ⇒ Object
readonly
Returns the value of attribute all_items.
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#matcher ⇒ Object
readonly
Returns the value of attribute matcher.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
Instance Method Summary collapse
- #ambiguous? ⇒ Boolean
- #count ⇒ Object
- #exact? ⇒ Boolean
- #exact_match ⇒ Object
-
#first ⇒ Object
Returns the first matching item (for find semantics).
-
#initialize(matcher:, all_items:, prefix:, key: nil, block: nil) ⇒ PathResult
constructor
A new instance of PathResult.
- #match ⇒ Object
- #match? ⇒ Boolean
- #matches ⇒ Object
- #one? ⇒ Boolean
-
#resolved ⇒ Object
Returns item for resolve semantics: exact match, or single match, otherwise nil.
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_items ⇒ Object (readonly)
Returns the value of attribute all_items.
228 229 230 |
# File 'lib/hiiro/matcher.rb', line 228 def all_items @all_items end |
#block ⇒ Object (readonly)
Returns the value of attribute block.
228 229 230 |
# File 'lib/hiiro/matcher.rb', line 228 def block @block end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
228 229 230 |
# File 'lib/hiiro/matcher.rb', line 228 def key @key end |
#matcher ⇒ Object (readonly)
Returns the value of attribute matcher.
228 229 230 |
# File 'lib/hiiro/matcher.rb', line 228 def matcher @matcher end |
#prefix ⇒ Object (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
258 259 260 |
# File 'lib/hiiro/matcher.rb', line 258 def ambiguous? count > 1 end |
#count ⇒ Object
254 255 256 |
# File 'lib/hiiro/matcher.rb', line 254 def count matches.count end |
#exact? ⇒ Boolean
274 275 276 |
# File 'lib/hiiro/matcher.rb', line 274 def exact? !exact_match.nil? end |
#exact_match ⇒ Object
262 263 264 |
# File 'lib/hiiro/matcher.rb', line 262 def exact_match all_items.find { |item| item.extracted_item == prefix } end |
#first ⇒ Object
Returns the first matching item (for find semantics)
288 289 290 |
# File 'lib/hiiro/matcher.rb', line 288 def first matches.first end |
#match ⇒ Object
266 267 268 |
# File 'lib/hiiro/matcher.rb', line 266 def match one? ? matches.first : nil end |
#match? ⇒ Boolean
270 271 272 |
# File 'lib/hiiro/matcher.rb', line 270 def match? matches.any? end |
#matches ⇒ Object
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
278 279 280 |
# File 'lib/hiiro/matcher.rb', line 278 def one? count == 1 end |
#resolved ⇒ Object
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 |