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 =
[]
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name, *names, &block) ⇒ Parameter
Returns a new instance of Parameter.
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/main/parameter.rb', line 76
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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
# File 'lib/main/parameter.rb', line 191
def add_handlers e
sc =
class << e
self
end
this = self
if before_handler?
sc.module_eval do
define_method(:before_handler){|main| main.instance_eval &this.before_handler}
end
end
if error_handler?
sc.module_eval do
define_method(:handler){|main| main.instance_eval &this.error_handler}
end
end
if after_handler?
sc.module_eval do
define_method(:after_handler){|main| main.instance_eval &this.after_handler}
end
end
end
|
#add_value(value) ⇒ Object
112
113
114
115
|
# File 'lib/main/parameter.rb', line 112
def add_value value
given true
values << value
end
|
#adding_handlers ⇒ Object
218
219
220
221
222
223
224
225
|
# File 'lib/main/parameter.rb', line 218
def adding_handlers
begin
yield
rescue Exception => e
add_handlers e
raise
end
end
|
#apply_casting ⇒ Object
173
174
175
176
177
178
179
180
|
# File 'lib/main/parameter.rb', line 173
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
121
122
123
|
# File 'lib/main/parameter.rb', line 121
def argument_none?
argument.nil?
end
|
#argument_optional? ⇒ Boolean
129
130
131
132
|
# File 'lib/main/parameter.rb', line 129
def argument_optional?
argument and
argument.to_s.downcase.to_sym == :optional
end
|
#argument_required? ⇒ Boolean
125
126
127
128
|
# File 'lib/main/parameter.rb', line 125
def argument_required?
argument and
argument.to_s.downcase.to_sym == :required
end
|
#check_arity ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/main/parameter.rb', line 151
def check_arity
(raise Arity, "#{ 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
if values.size < arity
if argument_required? or argument_none?
raise Arity, "#{ typename }) #{ values.size }/#{ sign }#{ arity }" if(values.size < arity)
elsif argument_optional?
raise Arity, "#{ typename }) #{ values.size }/#{ sign }#{ arity }" if(values.size < arity and values.size > 0)
end
end
end
|
#check_validation ⇒ Object
182
183
184
185
186
187
188
189
|
# File 'lib/main/parameter.rb', line 182
def check_validation
if validate?
values.each do |value|
validate[value] or
raise InValid, "invalid: #{ typename }=#{ value.inspect }"
end
end
end
|
103
104
105
|
# File 'lib/main/parameter.rb', line 103
def default
defaults.first
end
|
99
100
101
|
# File 'lib/main/parameter.rb', line 99
def name
names.first
end
|
#optional=(bool) ⇒ Object
137
138
139
|
# File 'lib/main/parameter.rb', line 137
def optional= bool
self.required !bool
end
|
#optional? ⇒ Boolean
134
135
136
|
# File 'lib/main/parameter.rb', line 134
def optional?
not required?
end
|
141
142
143
144
145
146
147
148
149
|
# File 'lib/main/parameter.rb', line 141
def setup!
return false unless given?
adding_handlers do
check_arity
apply_casting
check_validation
end
true
end
|
107
108
109
110
|
# File 'lib/main/parameter.rb', line 107
def typename
prefix = '--' if type.to_s =~ %r/option/
"#{ type }(#{ prefix }#{ name })"
end
|
117
118
119
|
# File 'lib/main/parameter.rb', line 117
def value
values.first
end
|