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

Types =
[ Parameter ]

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Parameter) initialize(name, *names, &block)



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

+ (Object) class_for(type)

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

+ (Object) create(type, main, *a, &b)



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

+ (Object) inherited(other)



33
34
35
# File 'lib/main/parameter.rb', line 33

def inherited other
  Types << other
end

+ (Object) sym



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

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

+ (Object) wrap_errors



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

+ (Object) wrapped_error(w)



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

- (Object) add_handlers(e)



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/main/parameter.rb', line 225

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

- (Object) add_value(value)



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

def add_value value
  given true
  values << value
end

- (Object) adding_handlers



247
248
249
250
251
252
253
254
# File 'lib/main/parameter.rb', line 247

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

- (Object) apply_casting



201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/main/parameter.rb', line 201

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

- (Boolean) argument_none?



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

def argument_none?
  argument.nil?
end

- (Boolean) argument_optional?



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

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

- (Boolean) argument_required?



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

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

- (Object) check_arity



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 175

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

- (Object) check_validation



216
217
218
219
220
221
222
223
# File 'lib/main/parameter.rb', line 216

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

- (Object) default(*values)



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

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

- (Object) default=(value)



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

def default=(value)
  default(value)
end

- (Object) name



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

def name
  names.first
end

- (Object) optional=(bool)



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

def optional= bool 
  self.required !bool
end

- (Boolean) optional?



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

def optional?
  not required?
end

- (Object) remove Also known as: remove!, ignore



256
257
258
# File 'lib/main/parameter.rb', line 256

def remove
  main.parameters.delete(self)
end

- (Object) sanity_check!

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

- (Object) setup!

def setup!

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


167
168
169
170
171
172
173
# File 'lib/main/parameter.rb', line 167

def setup!
  adding_handlers do
    check_arity
    apply_casting
    check_validation
  end
end

- (Object) typename



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

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

- (Object) value



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

def value
  values.first
end