Class: Toys::Completion::Enum
Overview
A Completion whose candidates come from a static list of strings.
Instance Attribute Summary collapse
-
#prefix_constraint ⇒ String, Regexp
readonly
Constraint on the fragment prefix.
-
#values ⇒ Array<String>
readonly
The array of completion candidates.
Instance Method Summary collapse
-
#call(context) ⇒ Array<Toys::Completion::Candidate>
Returns candidates for the current completion.
-
#initialize(values, prefix_constraint: "") ⇒ Enum
constructor
Create a completion from a list of values.
Constructor Details
#initialize(values, prefix_constraint: "") ⇒ Enum
Create a completion from a list of values.
339 340 341 342 343 |
# File 'lib/toys/completion.rb', line 339 def initialize(values, prefix_constraint: "") super() @values = values.flatten.map { |v| Candidate.new(v) }.sort @prefix_constraint = prefix_constraint end |
Instance Attribute Details
#prefix_constraint ⇒ String, Regexp (readonly)
Constraint on the fragment prefix.
355 356 357 |
# File 'lib/toys/completion.rb', line 355 def prefix_constraint @prefix_constraint end |
#values ⇒ Array<String> (readonly)
The array of completion candidates.
349 350 351 |
# File 'lib/toys/completion.rb', line 349 def values @values end |
Instance Method Details
#call(context) ⇒ Array<Toys::Completion::Candidate>
Returns candidates for the current completion.
364 365 366 367 368 |
# File 'lib/toys/completion.rb', line 364 def call(context) return [] unless @prefix_constraint === context.fragment_prefix fragment = context.fragment @values.find_all { |val| val.string.start_with?(fragment) } end |