Class: Samovar::Flag

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Flag

Returns a new instance of Flag.



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/samovar/flags.rb', line 62

def initialize(text)
	@text = text
	
	if text =~ /(.*?)\s(\<.*?\>)/
		@prefix = $1
		@value = $2
	else
		@prefix = @text
	end
	
	*@alternatives, @prefix = @prefix.split('/')
end

Instance Attribute Details

#alternativesObject (readonly)

Returns the value of attribute alternatives.



77
78
79
# File 'lib/samovar/flags.rb', line 77

def alternatives
  @alternatives
end

#prefixObject (readonly)

Returns the value of attribute prefix.



76
77
78
# File 'lib/samovar/flags.rb', line 76

def prefix
  @prefix
end

#textObject (readonly)

Returns the value of attribute text.



75
76
77
# File 'lib/samovar/flags.rb', line 75

def text
  @text
end

#valueObject (readonly)

Returns the value of attribute value.



78
79
80
# File 'lib/samovar/flags.rb', line 78

def value
  @value
end

Instance Method Details

#keyObject



88
89
90
# File 'lib/samovar/flags.rb', line 88

def key
	@key ||= @prefix.sub(/^-*/, '').gsub('-', '_').to_sym
end

#parse(input) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/samovar/flags.rb', line 92

def parse(input)
	if prefix?(input.first)
		if @value
			input.shift(2).last
		else
			input.shift; key
		end
	end
end

#prefix?(token) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/samovar/flags.rb', line 84

def prefix?(token)
	@prefix == token or @alternatives.include?(token)
end

#to_sObject



80
81
82
# File 'lib/samovar/flags.rb', line 80

def to_s
	@text
end