Class: Toys::Completion::Enum

Inherits:
Base
  • Object
show all
Defined in:
lib/toys/completion.rb

Overview

A Completion whose candidates come from a static list of strings.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values, prefix_constraint: "") ⇒ Enum

Create a completion from a list of values.

Parameters:

  • values (Array<String>)
  • prefix_constraint (String, Regexp) (defaults to: "")

    Constraint on the fragment prefix. Defaults to requiring the prefix be empty.



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_constraintString, Regexp (readonly)

Constraint on the fragment prefix.

Returns:

  • (String, Regexp)


355
356
357
# File 'lib/toys/completion.rb', line 355

def prefix_constraint
  @prefix_constraint
end

#valuesArray<String> (readonly)

The array of completion candidates.

Returns:

  • (Array<String>)


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.

Parameters:

Returns:



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