Class: Samovar::Many

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, description = nil, stop: /^-/, default: nil, required: false) ⇒ Many

Returns a new instance of Many.



8
9
10
11
12
13
14
# File 'lib/samovar/many.rb', line 8

def initialize(key, description = nil, stop: /^-/, default: nil, required: false)
	@key = key
	@description = description
	@stop = stop
	@default = default
	@required = required
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



19
20
21
# File 'lib/samovar/many.rb', line 19

def default
  @default
end

#descriptionObject (readonly)

Returns the value of attribute description.



17
18
19
# File 'lib/samovar/many.rb', line 17

def description
  @description
end

#keyObject (readonly)

Returns the value of attribute key.



16
17
18
# File 'lib/samovar/many.rb', line 16

def key
  @key
end

#requiredObject (readonly)

Returns the value of attribute required.



20
21
22
# File 'lib/samovar/many.rb', line 20

def required
  @required
end

#stopObject (readonly)

Returns the value of attribute stop.



18
19
20
# File 'lib/samovar/many.rb', line 18

def stop
  @stop
end

Instance Method Details

#parse(input, parent = nil, default = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/samovar/many.rb', line 38

def parse(input, parent = nil, default = nil)
	if @stop and stop_index = input.index{|item| @stop === item}
		input.shift(stop_index)
	elsif input.any?
		input.shift(input.size)
	elsif default ||= @default
		return default
	elsif @required
		raise MissingValueError.new(parent, self)
	end
end

#to_aObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/samovar/many.rb', line 26

def to_a
	usage = [to_s, @description]
	
	if @default
		usage << "(default: #{@default.inspect})"
	elsif @required
		usage << "(required)"
	end
	
	return usage
end

#to_sObject



22
23
24
# File 'lib/samovar/many.rb', line 22

def to_s
	"<#{key}...>"
end