Class: Prompt::Parameter

Inherits:
Object
  • Object
show all
Defined in:
lib/prompt/parameter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, desc = nil, values = nil, &block) ⇒ Parameter

Returns a new instance of Parameter.



7
8
9
10
11
12
13
# File 'lib/prompt/parameter.rb', line 7

def initialize(name, desc = nil, values = nil, &block)
  @name = name
  @desc = desc
  @values = values || []
  @proc = block if block_given?
  @cached_values = {}
end

Instance Attribute Details

#descObject (readonly)

Returns the value of attribute desc.



5
6
7
# File 'lib/prompt/parameter.rb', line 5

def desc
  @desc
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/prompt/parameter.rb', line 4

def name
  @name
end

Instance Method Details

#clear_cached_valuesObject



15
16
17
# File 'lib/prompt/parameter.rb', line 15

def clear_cached_values
  @cached_values = {}
end

#completions(starting_with = "") ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/prompt/parameter.rb', line 19

def completions(starting_with = "")
  all = if @proc
    @cached_values[starting_with] ||= @proc.call(starting_with)
  else
    @values
  end

  all.map(&:to_s).grep /^#{starting_with}/
end