Class: Samovar::Options

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title = "Options", key: :options) ⇒ Options

Returns a new instance of Options.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/samovar/options.rb', line 33

def initialize(title = "Options", key: :options)
	@title = title
	@ordered = []
	
	# We use this flag to option cache to improve parsing performance:
	@keyed = {}
	
	@key = key
	
	@defaults = {}
end

Instance Attribute Details

#defaultsObject (readonly)

Returns the value of attribute defaults.



57
58
59
# File 'lib/samovar/options.rb', line 57

def defaults
  @defaults
end

#keyObject (readonly)

Returns the value of attribute key.



56
57
58
# File 'lib/samovar/options.rb', line 56

def key
  @key
end

#orderedObject (readonly)

Returns the value of attribute ordered.



54
55
56
# File 'lib/samovar/options.rb', line 54

def ordered
  @ordered
end

#titleObject (readonly)

Returns the value of attribute title.



53
54
55
# File 'lib/samovar/options.rb', line 53

def title
  @title
end

Class Method Details

.parse(*arguments, **options, &block) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/samovar/options.rb', line 25

def self.parse(*arguments, **options, &block)
	options = self.new(*arguments, **options)
	
	options.instance_eval(&block) if block_given?
	
	return options.freeze
end

Instance Method Details

#<<(option) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/samovar/options.rb', line 89

def << option
	@ordered << option
	option.flags.each do |flag|
		@keyed[flag.prefix] = option
		
		flag.alternatives.each do |alternative|
			@keyed[alternative] = option
		end
	end
	
	if default = option.default
		@defaults[option.key] = option.default
	end
end

#each(&block) ⇒ Object



71
72
73
# File 'lib/samovar/options.rb', line 71

def each(&block)
	@ordered.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
	@ordered.empty?
end

#freezeObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/samovar/options.rb', line 59

def freeze
	return self if frozen?
	
	@ordered.freeze
	@keyed.freeze
	@defaults.freeze
	
	@ordered.each(&:freeze)
	
	super
end

#initialize_dup(source) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/samovar/options.rb', line 45

def initialize_dup(source)
	super
	
	@ordered = @ordered.dup
	@keyed = @keyed.dup
	@defaults = @defaults.dup
end

#merge!(options) ⇒ Object



83
84
85
86
87
# File 'lib/samovar/options.rb', line 83

def merge!(options)
	options.each do |option|
		self << option
	end
end

#option(*arguments, **options, &block) ⇒ Object



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

def option(*arguments, **options, &block)
	self << Option.new(*arguments, **options, &block)
end

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



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/samovar/options.rb', line 104

def parse(input, parent = nil, default = nil)
	values = (default || @defaults).dup
	
	while option = @keyed[input.first]
		if result = option.parse(input)
			values[option.key] = result
		end
	end
	
	return values
end

#to_sObject



116
117
118
# File 'lib/samovar/options.rb', line 116

def to_s
	@ordered.collect(&:to_s).join(' ')
end

#usage(rows) ⇒ Object



120
121
122
123
124
# File 'lib/samovar/options.rb', line 120

def usage(rows)
	@ordered.each do |option|
		rows << option
	end
end