Class: Toys::Completion::Candidate
- Inherits:
-
Object
- Object
- Toys::Completion::Candidate
- Includes:
- Comparable
- Defined in:
- lib/toys/completion.rb
Overview
A candidate for completing a string fragment.
A candidate includes a string representing the potential completed word, as well as a flag indicating whether it is a partial completion (i.e. a prefix that could still be added to) versus a final word. Generally, tab completion systems should add a trailing space after a final completion but not after a partial completion.
Instance Attribute Summary collapse
-
#string ⇒ String
(also: #to_s)
readonly
Get the candidate string.
Class Method Summary collapse
-
.new_multi(array, partial: false) ⇒ Array<Toys::Completion::Candidate>
Create an array of candidates given an array of strings.
Instance Method Summary collapse
-
#final? ⇒ boolean
Determine whether the candidate is a final completion.
-
#initialize(string, partial: false) ⇒ Candidate
constructor
Create a new candidate.
-
#partial? ⇒ boolean
Determine whether the candidate is partial completion.
-
#with_prefix(prefix) ⇒ Toys::Completion::Candidate
Returns a copy of this candidate with the given string prepended.
Constructor Details
#initialize(string, partial: false) ⇒ Candidate
Create a new candidate
158 159 160 161 |
# File 'lib/toys/completion.rb', line 158 def initialize(string, partial: false) @string = string.to_s @partial = partial ? true : false end |
Instance Attribute Details
#string ⇒ String (readonly) Also known as: to_s
Get the candidate string.
167 168 169 |
# File 'lib/toys/completion.rb', line 167 def string @string end |
Class Method Details
.new_multi(array, partial: false) ⇒ Array<Toys::Completion::Candidate>
Create an array of candidates given an array of strings.
226 227 228 |
# File 'lib/toys/completion.rb', line 226 def self.new_multi(array, partial: false) array.map { |s| new(s, partial: partial) } end |
Instance Method Details
#final? ⇒ boolean
Determine whether the candidate is a final completion.
182 183 184 |
# File 'lib/toys/completion.rb', line 182 def final? !@partial end |
#partial? ⇒ boolean
Determine whether the candidate is partial completion.
174 175 176 |
# File 'lib/toys/completion.rb', line 174 def partial? @partial end |
#with_prefix(prefix) ⇒ Toys::Completion::Candidate
Returns a copy of this candidate with the given string prepended. Use this when a completion handles only the tail of the word being completed, and must restore the head to each candidate so that the candidate remains a replacement for the whole word.
195 196 197 |
# File 'lib/toys/completion.rb', line 195 def with_prefix(prefix) Candidate.new("#{prefix}#{@string}", partial: @partial) end |