Class: Samovar::Completion::Suggestion

Inherits:
Object
  • Object
show all
Defined in:
lib/samovar/completion/suggestion.rb

Overview

A single completion suggestion.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, type: nil, description: nil, **options) ⇒ Suggestion

Initialize a new completion suggestion.



35
36
37
38
39
40
# File 'lib/samovar/completion/suggestion.rb', line 35

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

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



49
50
51
# File 'lib/samovar/completion/suggestion.rb', line 49

def description
  @description
end

#optionsObject (readonly)

Returns the value of attribute options.



52
53
54
# File 'lib/samovar/completion/suggestion.rb', line 52

def options
  @options
end

#The completion description.(completiondescription.) ⇒ Object (readonly)



49
# File 'lib/samovar/completion/suggestion.rb', line 49

attr :description

#The completion type.(completiontype.) ⇒ Object (readonly)



43
# File 'lib/samovar/completion/suggestion.rb', line 43

attr :type

#typeObject (readonly)

Returns the value of attribute type.



43
44
45
# File 'lib/samovar/completion/suggestion.rb', line 43

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



46
47
48
# File 'lib/samovar/completion/suggestion.rb', line 46

def value
  @value
end

Class Method Details

.wrap(value) ⇒ Object

Wrap a raw completion value in a suggestion.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/samovar/completion/suggestion.rb', line 14

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

Instance Method Details

#Additional completion metadata.=(completionmetadata. = (value)) ⇒ Object



52
# File 'lib/samovar/completion/suggestion.rb', line 52

attr :options

#start_with?(prefix) ⇒ Boolean

Whether this suggestion starts with the given prefix.

Returns:

  • (Boolean)


58
59
60
# File 'lib/samovar/completion/suggestion.rb', line 58

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

#The completion value.=(completionvalue. = (value)) ⇒ Object



46
# File 'lib/samovar/completion/suggestion.rb', line 46

attr :value

#to_recordObject

Convert the suggestion to a tab-separated completion record.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/samovar/completion/suggestion.rb', line 65

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

#to_sObject

Convert the suggestion to its value.



83
84
85
# File 'lib/samovar/completion/suggestion.rb', line 83

def to_s
	@value.to_s
end