Class: Hiiro::Matcher::Result
- Inherits:
-
Object
- Object
- Hiiro::Matcher::Result
- 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.
-
#match_type ⇒ Object
readonly
Returns the value of attribute match_type.
-
#matcher ⇒ Object
readonly
Returns the value of attribute matcher.
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
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:, pattern:, match_type: :prefix, key: nil, block: nil) ⇒ Result
constructor
A new instance of Result.
- #match ⇒ Object
- #match? ⇒ Boolean
- #matches ⇒ Object
- #one? ⇒ Boolean
-
#prefix ⇒ Object
Alias for backward compatibility.
-
#resolved ⇒ Object
Returns item for resolve semantics: exact match, or single match, otherwise nil.
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_items ⇒ Object (readonly)
Returns the value of attribute all_items.
159 160 161 |
# File 'lib/hiiro/matcher.rb', line 159 def all_items @all_items end |
#block ⇒ Object (readonly)
Returns the value of attribute block.
159 160 161 |
# File 'lib/hiiro/matcher.rb', line 159 def block @block end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
159 160 161 |
# File 'lib/hiiro/matcher.rb', line 159 def key @key end |
#match_type ⇒ Object (readonly)
Returns the value of attribute match_type.
159 160 161 |
# File 'lib/hiiro/matcher.rb', line 159 def match_type @match_type end |
#matcher ⇒ Object (readonly)
Returns the value of attribute matcher.
159 160 161 |
# File 'lib/hiiro/matcher.rb', line 159 def matcher @matcher end |
#pattern ⇒ Object (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
192 193 194 |
# File 'lib/hiiro/matcher.rb', line 192 def ambiguous? count > 1 end |
#count ⇒ Object
188 189 190 |
# File 'lib/hiiro/matcher.rb', line 188 def count matches.count end |
#exact? ⇒ Boolean
208 209 210 |
# File 'lib/hiiro/matcher.rb', line 208 def exact? !exact_match.nil? end |
#exact_match ⇒ Object
196 197 198 |
# File 'lib/hiiro/matcher.rb', line 196 def exact_match all_items.find { |item| item.extracted_item == pattern } end |
#first ⇒ Object
Returns the first matching item (for find semantics)
222 223 224 |
# File 'lib/hiiro/matcher.rb', line 222 def first matches.first end |
#match ⇒ Object
200 201 202 |
# File 'lib/hiiro/matcher.rb', line 200 def match one? ? matches.first : nil end |
#match? ⇒ Boolean
204 205 206 |
# File 'lib/hiiro/matcher.rb', line 204 def match? matches.any? end |
#matches ⇒ Object
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
212 213 214 |
# File 'lib/hiiro/matcher.rb', line 212 def one? count == 1 end |
#prefix ⇒ Object
Alias for backward compatibility
171 172 173 |
# File 'lib/hiiro/matcher.rb', line 171 def prefix pattern end |
#resolved ⇒ Object
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 |