class Toys::Completion::Enum
A Completion
whose candidates come from a static list of strings.
Attributes
prefix_constraint[R]
Constraint on the fragment prefix. @return [String,Regexp]
values[R]
The array of completion candidates. @return [Array<String>]
Public Class Methods
new(values, prefix_constraint: "")
click to toggle source
Create a completion from a list of values.
@param values [Array<String>] @param prefix_constraint
[String,Regexp] Constraint on the fragment
prefix. Defaults to requiring the prefix be empty.
Calls superclass method
# File lib/toys/completion.rb, line 331 def initialize(values, prefix_constraint: "") super() @values = values.flatten.map { |v| Candidate.new(v) }.sort @prefix_constraint = prefix_constraint end
Public Instance Methods
call(context)
click to toggle source
Returns candidates for the current completion.
@param context [Toys::Completion::Context] the current completion
context including the string fragment.
@return [Array<Toys::Completion::Candidate>] an array of candidates
# File lib/toys/completion.rb, line 356 def call(context) return [] unless @prefix_constraint === context.fragment_prefix fragment = context.fragment @values.find_all { |val| val.string.start_with?(fragment) } end