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
74
# File 'lib/samovar/flags.rb', line 62

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

Instance Attribute Details

#alternativesObject (readonly)

Returns the value of attribute alternatives.



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

def alternatives
  @alternatives
end

#prefixObject (readonly)

Returns the value of attribute prefix.



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

def prefix
  @prefix
end

#textObject (readonly)

Returns the value of attribute text.



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

def text
  @text
end

#valueObject (readonly)

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#keyObject



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

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

#parse(input) ⇒ Object



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

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)


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

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

#to_sObject



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

def to_s
	@text
end