Class: Flow

Inherits:
Object
  • Object
show all
Defined in:
lib/args.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFlow

Returns a new instance of Flow.



129
130
131
# File 'lib/args.rb', line 129

def initialize
  @cells = [ ]
end

Instance Attribute Details

#errorObject

Returns the value of attribute error.



127
128
129
# File 'lib/args.rb', line 127

def error
  @error
end

#pre_condObject

Returns the value of attribute pre_cond.



127
128
129
# File 'lib/args.rb', line 127

def pre_cond
  @pre_cond
end

#restObject

Returns the value of attribute rest.



127
128
129
# File 'lib/args.rb', line 127

def rest
  @rest
end

#usage_stringObject

Returns the value of attribute usage_string.



127
128
129
# File 'lib/args.rb', line 127

def usage_string
  @usage_string
end

Instance Method Details

#add(*cells) ⇒ Object



133
134
135
136
137
# File 'lib/args.rb', line 133

def add(*cells)
  cells.each { |cell|
    @cells << cell
  }
end

#add_errline(errmsg) ⇒ Object



239
240
241
242
243
244
245
246
# File 'lib/args.rb', line 239

def add_errline(errmsg)
  return if errmsg.nil?
  if @error.nil?
    @error = errmsg
  else
    @error = @error + "\n" + errmsg
  end
end

#arghashObject



220
221
222
223
224
225
226
227
# File 'lib/args.rb', line 220

def arghash
  result = { }
  @cells.each { |cell|
    result[cell.name] = cell.value
  }
  result["command"] = command
  result
end

#commandObject



214
215
216
217
218
# File 'lib/args.rb', line 214

def command
  if @cells[-1]
    @cells[-1].command
  end
end

#finishedObject



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/args.rb', line 159

def finished
  if command && @cells[-1].full?
    # the command was seen
    return true
  end
  @cells.each { |cell|
    if ! cell.full?
      return false
    end
  }
  return true
end

#missing_argsObject



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/args.rb', line 192

def missing_args
  missing = [ ]
  @cells.each { |cell|
    if cell.missing_value
      missing << cell.name
    end
  }
  if missing.length > 0
    add_errline("missing args: #{missing}")
  end
end

#parse(args, obj = nil) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/args.rb', line 139

def parse(args, obj = nil)
  @target = obj
  @rest = [ ]
  args.each_with_index { |arg, index|
    if finished  || @error
      @rest = args[index..-1]
      break
    end
    name, value = Util.parse_line(arg)
    if value.nil?
      value = name
      setvalue(value)
    else
      set(name, value)
    end
  }
  missing = missing_args
  @error.nil?
end

#set(name, value) ⇒ Object



204
205
206
207
208
209
210
211
212
# File 'lib/args.rb', line 204

def set(name, value)
  @cells.each { |cell|
    if cell.name == name
      add_errline(cell.set(value, @target))
      return
    end
  }
  add_errline("argument named #{name} not found")
end

#setvalue(value) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/args.rb', line 172

def setvalue(value)
  @cells.each { |cell|
    if cell.full?
      next
    else
      error = cell.set(value, @target)
      if error
        if cell.repeatable
          next   # repeatable cell is now full; try to set next cell
        else
          add_errline(error)
          return
        end
      end            
      return
    end
  }
  return
end

#usageObject



229
230
231
232
233
234
235
236
237
# File 'lib/args.rb', line 229

def usage
  names = [ ]
  desc = [ ]
  @cells.each { |cell|
    names << cell.name
    desc << cell.desc
  }
  return names, desc
end