Class: OptBuilder
- Inherits:
-
Object
show all
- Defined in:
- lib/opt_builder.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.collection(sym, opts = {}) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/opt_builder.rb', line 52
def self.collection(sym,opts={})
attr_writer sym
self.collection_opts << {
:sym => sym,
:force_restart => opts[:force_restart],
:alias_for => opts[:alias_for] || sym.to_s.dasherize
}
self.class_eval do
define_method sym do
value = self.send(:instance_variable_get,"@#{sym.to_s}") || []
self.send(:instance_variable_set,"@#{sym.to_s}", value)
value
end
end
end
|
.inherited(sub) ⇒ Object
11
12
13
14
|
# File 'lib/opt_builder.rb', line 11
def self.inherited(sub)
sub.single_opts = []
sub.collection_opts = []
end
|
.map_with_collection_opts(&block) ⇒ Object
25
26
27
28
29
30
31
32
|
# File 'lib/opt_builder.rb', line 25
def self.map_with_collection_opts(&block)
result = self.collection_opts.map do |config|
yield config
end
result += self.superclass.map_with_collection_opts(&block) if (self.superclass.respond_to? :map_with_single_opts)
result
end
|
.map_with_single_opts(&block) ⇒ Object
16
17
18
19
20
21
22
23
|
# File 'lib/opt_builder.rb', line 16
def self.map_with_single_opts(&block)
result = self.single_opts.map do |config|
yield config
end
result += self.superclass.map_with_single_opts(&block) if (self.superclass.respond_to? :map_with_single_opts)
result
end
|
.single(sym, opts = {}) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/opt_builder.rb', line 35
def self.single(sym,opts={})
attr_writer sym
self.single_opts << {
:sym => sym,
:alias_for => opts[:alias_for] || sym.to_s.dasherize
}
self.class_eval do
define_method sym do
result = self.send(:instance_variable_get,"@#{sym.to_s}")
result = opts[:default] if result.nil?
result
end
end
end
|
Instance Method Details
#to_s ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/opt_builder.rb', line 84
def to_s
opts = self.class.map_with_single_opts do |config|
sym = config[:sym]
"-#{config[:alias_for]}=#{value_as_option_value(send(sym))}" unless send(sym).nil?
end
opts += self.class.map_with_collection_opts do |hash|
sym = hash[:sym];
restart = hash[:force_restart]
add_char = ""
send(sym).map do |value|
add_char = "+" unless restart
restart = false if restart
"-#{hash[:alias_for]}#{add_char}=#{value_as_option_value(value)}" unless send(sym).nil?
end
end
opts.flatten.reject{ |item| item.nil? }.join " "
end
|
#value_array_as_option_value(value_array) ⇒ Object
76
77
78
79
80
81
82
|
# File 'lib/opt_builder.rb', line 76
def value_array_as_option_value(value_array)
ary = value_array.map do |value|
value_as_option_value(value)
end
ary.join " "
end
|
#value_as_option_value(value) ⇒ Object
71
72
73
74
|
# File 'lib/opt_builder.rb', line 71
def value_as_option_value(value)
return value_array_as_option_value(value) if value.is_a? Array
Shell.escape value
end
|