Class: Main::Parameter
Defined Under Namespace
Classes: AmbigousOption, Argument, Arity, DSL, Environment, Error, InValid, InvalidOption, Keyword, List, MissingArgument, NeedlessArgument, NoneSuch, NotGiven, Option, Table
Constant Summary
collapse
- Types =
[ Parameter ]
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name, *names, &block) ⇒ Parameter
Returns a new instance of Parameter.
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/main/parameter.rb', line 77
def initialize name, *names, &block
@names = Cast.list_of_string name, *names
@names.map! do |name|
if name =~ %r/^-+/
name.gsub! %r/^-+/, ''
end
if name =~ %r/=.*$/
argument( name =~ %r/=\s*\[.*$/ ? :optional : :required )
name.gsub! %r/=.*$/, ''
end
name
end
@names = @names.sort.reverse
@names[1..-1].each do |name|
raise ArgumentError, "only one long name allowed (#{ @names.inspect })" if
name.size > 1
end
DSL.evaluate(self, &block) if block
sanity_check!
end
|
Class Method Details
.class_for(type) ⇒ Object
41
42
43
44
45
46
|
# File 'lib/main/parameter.rb', line 41
def class_for type
sym = type.to_s.downcase.to_sym
c = Types.detect{|t| t.sym == sym}
raise ArgumentError, type.inspect unless c
c
end
|
.create(type, *a, &b) ⇒ Object
48
49
50
51
52
53
54
|
# File 'lib/main/parameter.rb', line 48
def create type, *a, &b
c = class_for type
obj = c.allocate
obj.type = c.sym
obj.instance_eval{ initialize *a, &b }
obj
end
|
.inherited(other) ⇒ Object
33
34
35
|
# File 'lib/main/parameter.rb', line 33
def inherited other
Types << other
end
|
37
38
39
|
# File 'lib/main/parameter.rb', line 37
def sym
@sym ||= name.split(%r/::/).last.downcase.to_sym
end
|
.wrap_errors ⇒ Object
24
25
26
27
28
29
30
|
# File 'lib/main/parameter.rb', line 24
def wrap_errors
begin
yield
rescue => e
raise wrapped_error(e)
end
end
|
.wrapped_error(w) ⇒ Object
17
18
19
20
21
22
|
# File 'lib/main/parameter.rb', line 17
def wrapped_error w
e = Error.new "(#{ w.message } (#{ w.class }))"
e.wrapped = w
e.set_backtrace(w.backtrace || [])
e
end
|
Instance Method Details
#add_handlers(e) ⇒ Object
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
# File 'lib/main/parameter.rb', line 219
def add_handlers e
esc =
class << e
self
end
this = self
%w[ before instead after ].each do |which|
getter = "error_handler_#{ which }"
query = "error_handler_#{ which }?"
if send(query)
handler = send getter
esc.module_eval do
define_method(getter) do |main|
main.instance_eval_block self, &handler
end
end
end
end
end
|
#add_value(value) ⇒ Object
118
119
120
121
|
# File 'lib/main/parameter.rb', line 118
def add_value value
given true
values << value
end
|
#adding_handlers ⇒ Object
241
242
243
244
245
246
247
248
|
# File 'lib/main/parameter.rb', line 241
def adding_handlers
begin
yield
rescue Exception => e
add_handlers e
raise
end
end
|
#apply_casting ⇒ Object
201
202
203
204
205
206
207
208
|
# File 'lib/main/parameter.rb', line 201
def apply_casting
if cast?
op = cast.respond_to?('call') ? cast : Cast[cast]
values.map! do |val|
Parameter.wrap_errors{ op.call val }
end
end
end
|
#argument_none? ⇒ Boolean
127
128
129
|
# File 'lib/main/parameter.rb', line 127
def argument_none?
argument.nil?
end
|
#argument_optional? ⇒ Boolean
135
136
137
138
|
# File 'lib/main/parameter.rb', line 135
def argument_optional?
argument and
argument.to_s.downcase.to_sym == :optional
end
|
#argument_required? ⇒ Boolean
131
132
133
134
|
# File 'lib/main/parameter.rb', line 131
def argument_required?
argument and
argument.to_s.downcase.to_sym == :required
end
|
#check_arity ⇒ Object
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
# File 'lib/main/parameter.rb', line 167
def check_arity
return true if not given? and optional?
ex = values.size == 0 ? NotGiven : Arity
(raise ex, "#{ typename })" if values.size.zero? and argument_required?) unless arity == -1
if arity >= 0
min = arity
sign = ''
else
min = arity.abs - 1
sign = '-'
end
arity = min
=begin
puts
p :values => values
p :arity => arity
p :argument_required => argument_required?
p :argument_none => argument_none?
puts
=end
if values.size < arity
if argument_optional?
raise ex, "#{ typename }) #{ values.size }/#{ sign }#{ arity }" if(values.size < arity and values.size > 0)
elsif argument_required? or argument_none?
raise ex, "#{ typename }) #{ values.size }/#{ sign }#{ arity }" if(values.size < arity)
end
end
end
|
#check_validation ⇒ Object
210
211
212
213
214
215
216
217
|
# File 'lib/main/parameter.rb', line 210
def check_validation
if validate?
values.each do |value|
validate[value] or
raise InValid, "invalid: #{ typename }=#{ value.inspect }"
end
end
end
|
109
110
111
|
# File 'lib/main/parameter.rb', line 109
def default
defaults.first
end
|
105
106
107
|
# File 'lib/main/parameter.rb', line 105
def name
names.first
end
|
#optional=(bool) ⇒ Object
143
144
145
|
# File 'lib/main/parameter.rb', line 143
def optional= bool
self.required !bool
end
|
#optional? ⇒ Boolean
140
141
142
|
# File 'lib/main/parameter.rb', line 140
def optional?
not required?
end
|
#sanity_check! ⇒ Object
101
102
103
|
# File 'lib/main/parameter.rb', line 101
def sanity_check!
raise Arity, "#{ name } with arity -1 (zero or more args) cannot be required" if(arity == -1 and required?)
end
|
def setup!
return false unless given?
adding_handlers do
check_arity
apply_casting
check_validation
end
true
end
159
160
161
162
163
164
165
|
# File 'lib/main/parameter.rb', line 159
def setup!
adding_handlers do
check_arity
apply_casting
check_validation
end
end
|
113
114
115
116
|
# File 'lib/main/parameter.rb', line 113
def typename
prefix = '--' if type.to_s =~ %r/option/
"#{ type }(#{ prefix }#{ name })"
end
|
123
124
125
|
# File 'lib/main/parameter.rb', line 123
def value
values.first
end
|