Class: Samovar::Split

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, description, marker: '--', default: nil, required: false) ⇒ Split

Returns a new instance of Split.



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

def initialize(key, description, marker: '--', default: nil, required: false)
	@key = key
	@description = description
	@marker = marker
	@default = default
	@required = required
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



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

def default
  @default
end

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

#markerObject (readonly)

Returns the value of attribute marker.



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

def marker
  @marker
end

#requiredObject (readonly)

Returns the value of attribute required.



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

def required
  @required
end

Instance Method Details

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



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

def parse(input, parent = nil, default = nil)
	if offset = input.index(@marker)
		input.pop(input.size - offset).tap(&:shift)
	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/split.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/split.rb', line 22

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