Class: Main::Parameter

Inherits:
Object show all
Defined in:
lib/main/parameter.rb

Direct Known Subclasses

Argument, Environment, Keyword, Option

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.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/main/parameter.rb', line 79

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_by{|name| name.size}.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

Raises:

  • (ArgumentError)


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, main, *a, &b) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/main/parameter.rb', line 48

def create(type, main, *a, &b)
  c = class_for(type)
  obj = c.allocate
  obj.type = c.sym
  obj.main = main
  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

.symObject



37
38
39
# File 'lib/main/parameter.rb', line 37

def sym
  @sym ||= name.split(%r/::/).last.downcase.to_sym
end

.wrap_errorsObject



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



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/main/parameter.rb', line 237

def add_handlers e
  esc = 
    class << e
      self
    end

  %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



126
127
128
129
# File 'lib/main/parameter.rb', line 126

def add_value value
  given true
  values << value
end

#adding_handlersObject



257
258
259
260
261
262
263
264
# File 'lib/main/parameter.rb', line 257

def adding_handlers
  begin
    yield
  rescue Exception => e
    add_handlers e
    raise
  end
end

#apply_castingObject



213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/main/parameter.rb', line 213

def apply_casting 
  if cast?
    op = cast.respond_to?('call') ? cast : Cast[cast]
    case op.arity
      when -1
        replacement = Parameter.wrap_errors{ op.call(*values) }
        values.replace(replacement)
      else
        values.map! do |val|
          Parameter.wrap_errors{ op.call val }
        end
    end
  end
end

#argument_none?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/main/parameter.rb', line 147

def argument_none?
  argument.nil?
end

#argument_optional?Boolean

Returns:

  • (Boolean)


155
156
157
158
# File 'lib/main/parameter.rb', line 155

def argument_optional?
  argument and
    argument.to_s.downcase.to_sym == :optional
end

#argument_required?Boolean

Returns:

  • (Boolean)


151
152
153
154
# File 'lib/main/parameter.rb', line 151

def argument_required?
  argument and 
    argument.to_s.downcase.to_sym == :required
end

#check_arityObject



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/main/parameter.rb', line 187

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

  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_validationObject



228
229
230
231
232
233
234
235
# File 'lib/main/parameter.rb', line 228

def check_validation 
  if validate?
    values.each do |value|
      validate[value] or 
        raise InValid, "invalid: #{ typename }=#{ value.inspect }"
    end
  end
end

#default(*values) ⇒ Object



112
113
114
115
# File 'lib/main/parameter.rb', line 112

def default(*values)
  defaults(values) unless values.empty?
  defaults.first
end

#default=(value) ⇒ Object



117
118
119
# File 'lib/main/parameter.rb', line 117

def default=(value)
  default(value)
end

#getObject



135
136
137
# File 'lib/main/parameter.rb', line 135

def get
  self.values.first
end

#nameObject



108
109
110
# File 'lib/main/parameter.rb', line 108

def name
  names.first
end

#optional=(bool) ⇒ Object



163
164
165
# File 'lib/main/parameter.rb', line 163

def optional= bool 
  self.required !bool
end

#optional?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/main/parameter.rb', line 160

def optional?
  not required?
end

#removeObject Also known as: remove!, ignore



266
267
268
# File 'lib/main/parameter.rb', line 266

def remove
  main.parameters.delete(self)
end

#sanity_check!Object

Raises:



104
105
106
# File 'lib/main/parameter.rb', line 104

def sanity_check!
  raise Arity, "#{ name } with arity -1 (zero or more args) cannot be required" if(arity == -1 and required?)
end

#set(*values) ⇒ Object



131
132
133
# File 'lib/main/parameter.rb', line 131

def set(*values)
  self.values.replace(values)
end

#setup!Object

def setup!

  return false unless given?
  adding_handlers do
    check_arity
    apply_casting
    check_validation
  end
  true
end


179
180
181
182
183
184
185
# File 'lib/main/parameter.rb', line 179

def setup!
  adding_handlers do
    check_arity
    apply_casting
    check_validation
  end
end

#typenameObject



121
122
123
124
# File 'lib/main/parameter.rb', line 121

def typename
  prefix = '--' if type.to_s =~ %r/option/
  "#{ type }(#{ prefix }#{ name })"
end

#valueObject



139
140
141
# File 'lib/main/parameter.rb', line 139

def value
  values.first
end

#value=(value) ⇒ Object



143
144
145
# File 'lib/main/parameter.rb', line 143

def value=(value)
  set(value)
end