SamovarSourceSamovarCompletionSuggestion

class Suggestion

A single completion suggestion.

Definitions

def self.wrap(value)

Wrap a raw completion value in a suggestion.

Signature

parameter value Suggestion | Hash | Object

The value to wrap.

returns Suggestion

The normalized suggestion.

Implementation

def self.wrap(value)
	case value
	when self
		return value
	when Hash
		value = value.dup
		suggestion = value.fetch(:value)
		value.delete(:value)
		
		return self.new(suggestion, **value)
	else
		return self.new(value)
	end
end

def initialize(value, type: nil, description: nil, **options)

Initialize a new completion suggestion.

Signature

parameter value Object

The completion value.

parameter type Symbol | String | Nil

The completion type.

parameter description String | Nil

The completion description.

parameter options Hash

Additional completion metadata.

Implementation

def initialize(value, type: nil, description: nil, **options)
	@type = type
	@value = value
	@description = description
	@options = options
end

attr :type

Signature

attribute Symbol | String | Nil

The completion type.

attr :value

Signature

attribute Object

The completion value.

attr :description

Signature

attribute String | Nil

The completion description.

attr :options

Signature

attribute Hash

Additional completion metadata.

def start_with?(prefix)

Whether this suggestion starts with the given prefix.

Signature

parameter prefix String

The prefix to check.

returns Boolean

True if the suggestion starts with the given prefix.

Implementation

def start_with?(prefix)
	to_s.start_with?(prefix)
end

def to_record

Convert the suggestion to a tab-separated completion record.

Signature

returns String

The escaped completion record.

Implementation

def to_record
	fields = [
		escape(@type),
		escape(@value),
		escape(@description),
	]
	@options.each do |key, value|
		next if value.nil?
		
		fields << "#{escape(key)}=#{escape(value)}"
	end
	
	return fields.join("\t")
end

def to_s

Convert the suggestion to its value.

Signature

returns String

The suggestion value.

Implementation

def to_s
	@value.to_s
end