Class: Hiiro::Matcher
- Inherits:
-
Object
- Object
- Hiiro::Matcher
- Defined in:
- lib/hiiro/matcher.rb
Defined Under Namespace
Classes: Item, PathResult, Result
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#original_items ⇒ Object
readonly
Returns the value of attribute original_items.
Class Method Summary collapse
-
.by_prefix(items, prefix, key: nil, &block) ⇒ Object
Prefix matching class methods (explicit) - returns Result object.
-
.by_substring(items, substring, key: nil, &block) ⇒ Object
Substring matching class methods - returns Result object.
-
.find(items, prefix, key: nil, &block) ⇒ Object
Prefix matching class methods - return unwrapped values for backward compatibility.
- .find_all(items, prefix, key: nil, &block) ⇒ Object
- .find_all_paths(items, prefix, key: nil, &block) ⇒ Object
- .find_path(items, prefix, key: nil, &block) ⇒ Object
- .resolve(items, prefix, key: nil, &block) ⇒ Object
- .resolve_path(items, prefix, key: nil, &block) ⇒ Object
Instance Method Summary collapse
- #all_items(key = nil, &block) ⇒ Object
-
#by_prefix(prefix, key = nil, &block) ⇒ Object
Primary prefix matching method.
-
#by_substring(substring, key = nil, &block) ⇒ Object
Primary substring matching method.
- #find(prefix, key = nil, &block) ⇒ Object
- #find_all(prefix, key = nil, &block) ⇒ Object
- #find_all_paths(prefix, key = nil, &block) ⇒ Object
-
#find_path(prefix, key = nil, &block) ⇒ Object
Path-based matching.
-
#initialize(items, key = nil, &block) ⇒ Matcher
constructor
A new instance of Matcher.
- #resolve(prefix, key = nil, &block) ⇒ Object
- #resolve_path(prefix, key = nil, &block) ⇒ Object
-
#search(prefix, key = nil, &block) ⇒ Object
Legacy method aliases for backward compatibility.
- #search_path(prefix, key = nil, &block) ⇒ Object
Constructor Details
#initialize(items, key = nil, &block) ⇒ Matcher
Returns a new instance of Matcher.
48 49 50 51 52 |
# File 'lib/hiiro/matcher.rb', line 48 def initialize(items, key = nil, &block) @original_items = items @key = key @block = block end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
46 47 48 |
# File 'lib/hiiro/matcher.rb', line 46 def block @block end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
46 47 48 |
# File 'lib/hiiro/matcher.rb', line 46 def key @key end |
#original_items ⇒ Object (readonly)
Returns the value of attribute original_items.
46 47 48 |
# File 'lib/hiiro/matcher.rb', line 46 def original_items @original_items end |
Class Method Details
.by_prefix(items, prefix, key: nil, &block) ⇒ Object
Prefix matching class methods (explicit) - returns Result object
41 42 43 |
# File 'lib/hiiro/matcher.rb', line 41 def by_prefix(items, prefix, key: nil, &block) new(items, key, &block).by_prefix(prefix) end |
.by_substring(items, substring, key: nil, &block) ⇒ Object
Substring matching class methods - returns Result object
36 37 38 |
# File 'lib/hiiro/matcher.rb', line 36 def by_substring(items, substring, key: nil, &block) new(items, key, &block).by_substring(substring) end |
.find(items, prefix, key: nil, &block) ⇒ Object
Prefix matching class methods - return unwrapped values for backward compatibility
5 6 7 8 |
# File 'lib/hiiro/matcher.rb', line 5 def find(items, prefix, key: nil, &block) result = new(items, key, &block).by_prefix(prefix) result.first&.item end |
.find_all(items, prefix, key: nil, &block) ⇒ Object
10 11 12 13 |
# File 'lib/hiiro/matcher.rb', line 10 def find_all(items, prefix, key: nil, &block) result = new(items, key, &block).by_prefix(prefix) result.matches.map(&:item) end |
.find_all_paths(items, prefix, key: nil, &block) ⇒ Object
25 26 27 28 |
# File 'lib/hiiro/matcher.rb', line 25 def find_all_paths(items, prefix, key: nil, &block) result = new(items, key, &block).find_all_paths(prefix) result.matches.map(&:item) end |
.find_path(items, prefix, key: nil, &block) ⇒ Object
20 21 22 23 |
# File 'lib/hiiro/matcher.rb', line 20 def find_path(items, prefix, key: nil, &block) result = new(items, key, &block).find_path(prefix) result.first&.item end |
.resolve(items, prefix, key: nil, &block) ⇒ Object
15 16 17 18 |
# File 'lib/hiiro/matcher.rb', line 15 def resolve(items, prefix, key: nil, &block) result = new(items, key, &block).by_prefix(prefix) result.resolved&.item end |
.resolve_path(items, prefix, key: nil, &block) ⇒ Object
30 31 32 33 |
# File 'lib/hiiro/matcher.rb', line 30 def resolve_path(items, prefix, key: nil, &block) result = new(items, key, &block).resolve_path(prefix) result.resolved&.item end |
Instance Method Details
#all_items(key = nil, &block) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/hiiro/matcher.rb', line 54 def all_items(key = nil, &block) use_key = key.nil? && !block_given? ? @key : key use_block = key.nil? && !block_given? ? @block : block @all_items_cache ||= {} cache_key = [use_key, use_block].hash @all_items_cache[cache_key] ||= original_items.map { |item| Item.new( item: item, extracted_item: extract(item, use_key, &use_block), key: use_key, block: use_block ) } end |
#by_prefix(prefix, key = nil, &block) ⇒ Object
Primary prefix matching method
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/hiiro/matcher.rb', line 72 def by_prefix(prefix, key = nil, &block) Result.new( matcher: self, all_items: all_items(key, &block), pattern: prefix, match_type: :prefix, key: key || @key, block: block || @block ) end |
#by_substring(substring, key = nil, &block) ⇒ Object
Primary substring matching method
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/hiiro/matcher.rb', line 84 def by_substring(substring, key = nil, &block) Result.new( matcher: self, all_items: all_items(key, &block), pattern: substring, match_type: :substring, key: key || @key, block: block || @block ) end |
#find(prefix, key = nil, &block) ⇒ Object
100 101 102 |
# File 'lib/hiiro/matcher.rb', line 100 def find(prefix, key = nil, &block) by_prefix(prefix, key, &block) end |
#find_all(prefix, key = nil, &block) ⇒ Object
104 105 106 |
# File 'lib/hiiro/matcher.rb', line 104 def find_all(prefix, key = nil, &block) by_prefix(prefix, key, &block) end |
#find_all_paths(prefix, key = nil, &block) ⇒ Object
117 118 119 |
# File 'lib/hiiro/matcher.rb', line 117 def find_all_paths(prefix, key = nil, &block) search_path(prefix, key, &block) end |
#find_path(prefix, key = nil, &block) ⇒ Object
Path-based matching
113 114 115 |
# File 'lib/hiiro/matcher.rb', line 113 def find_path(prefix, key = nil, &block) search_path(prefix, key, &block) end |
#resolve(prefix, key = nil, &block) ⇒ Object
108 109 110 |
# File 'lib/hiiro/matcher.rb', line 108 def resolve(prefix, key = nil, &block) by_prefix(prefix, key, &block) end |
#resolve_path(prefix, key = nil, &block) ⇒ Object
121 122 123 |
# File 'lib/hiiro/matcher.rb', line 121 def resolve_path(prefix, key = nil, &block) search_path(prefix, key, &block) end |
#search(prefix, key = nil, &block) ⇒ Object
Legacy method aliases for backward compatibility
96 97 98 |
# File 'lib/hiiro/matcher.rb', line 96 def search(prefix, key = nil, &block) by_prefix(prefix, key, &block) end |
#search_path(prefix, key = nil, &block) ⇒ Object
125 126 127 128 129 130 131 132 133 |
# File 'lib/hiiro/matcher.rb', line 125 def search_path(prefix, key = nil, &block) PathResult.new( matcher: self, all_items: all_items(key, &block), prefix: prefix, key: key || @key, block: block || @block ) end |