Class: FlowField

Inherits:
Object
  • Object
show all
Defined in:
lib/taft_files/framework/zznamezz/watir/flows/flow_objects.rb

Overview

A class defining a field that is interacted with in some way as part of a flow. Valid types: :string (a text field); :button (a button); :link (a link); :list (a select list); :checkbox (a checkbox) TODO still want link to be valid here? What about FlowLink?

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type = :string, operation = :write, mandatory = true, size = nil, custom_valid_value_definition = nil, custom_invalid_value_definition = nil) ⇒ FlowField

Returns a new instance of FlowField.



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/taft_files/framework/zznamezz/watir/flows/flow_objects.rb', line 243

def initialize(name, type = :string, operation = :write, mandatory = true, size = nil, custom_valid_value_definition = nil, custom_invalid_value_definition = nil)
  raise "FlowField name must be a string" unless name.class == String
  @name = name
  @type = type
  @operation = operation
  raise "Cannot define FlowField #{@name} with operation of #{@operation.inspect}" unless @operation == :read || @operation == :write
  @mandatory = mandatory
  if size == nil
    @size = get_default_size
  else
    @size = size
  end
  
  custom_valid_value_definition = nil
  custom_invalid_value_definition = nil

  check_valid_type

  # TODO custom_valid_value_definition, etc
  case custom_valid_value_definition
    when NilClass
      #  take default valid field def based on @type
    when Symbol
      #run sub-case based on symbol
    when Regexp # ?
      #  define method
    when Array
      @custom_valid_value_definition = custom_valid_value_definition
    when block # ?
      #  define method
    else
      raise "Could not process custom_valid_value_definition specified for FlowField of name '#{@name}'"
  end

  case custom_invalid_value_definition
    when NilClass
      #  take default valid field def based on @type
    when Symbol
      #run sub-case based on symbol
    when Regexp # ?
      #  define method
    when Block # ?
      #  define method
    else
      raise "Could not process custom_invalid_value_definition specified for FlowField of name '#{@name}'"
  end
end

Instance Attribute Details

#custom_invalid_value_definitionObject

Returns the value of attribute custom_invalid_value_definition.



241
242
243
# File 'lib/taft_files/framework/zznamezz/watir/flows/flow_objects.rb', line 241

def custom_invalid_value_definition
  @custom_invalid_value_definition
end

#custom_valid_value_definitionObject

Returns the value of attribute custom_valid_value_definition.



241
242
243
# File 'lib/taft_files/framework/zznamezz/watir/flows/flow_objects.rb', line 241

def custom_valid_value_definition
  @custom_valid_value_definition
end

#mandatoryObject

Returns the value of attribute mandatory.



241
242
243
# File 'lib/taft_files/framework/zznamezz/watir/flows/flow_objects.rb', line 241

def mandatory
  @mandatory
end

#nameObject

Returns the value of attribute name.



241
242
243
# File 'lib/taft_files/framework/zznamezz/watir/flows/flow_objects.rb', line 241

def name
  @name
end

#operationObject

Returns the value of attribute operation.



241
242
243
# File 'lib/taft_files/framework/zznamezz/watir/flows/flow_objects.rb', line 241

def operation
  @operation
end

#sizeObject

Returns the value of attribute size.



241
242
243
# File 'lib/taft_files/framework/zznamezz/watir/flows/flow_objects.rb', line 241

def size
  @size
end

#typeObject

Returns the value of attribute type.



241
242
243
# File 'lib/taft_files/framework/zznamezz/watir/flows/flow_objects.rb', line 241

def type
  @type
end

Instance Method Details

#==(o) ⇒ Object

TODO needed?



343
344
# File 'lib/taft_files/framework/zznamezz/watir/flows/flow_objects.rb', line 343

def ==(o)
end

#check_valid_size(valid_size_for_type) ⇒ Object

Raises unless the supplied size is greater or equal to @size



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/taft_files/framework/zznamezz/watir/flows/flow_objects.rb', line 326

def check_valid_size(valid_size_for_type)
  valid = false
  case @size
    when NilClass
      valid = true if @size == nil
    when TrueClass # possible?
      valid = @size if something # TODO
    when FalseClass # possible?
      valid = @size if something # TODO
    when Fixnum, String
      valid = true if @size <= valid_size_for_type
  end

  raise "Defined size #{@size.inspect} for FlowField '#{@name}' is not valid for field of type '#{type}'" unless valid
end

#check_valid_typeObject

Valid types: :string (a text field); :button (a button); :link (a link); :list (a select list); :checkbox (a checkbox)



314
315
316
317
318
319
320
321
322
323
# File 'lib/taft_files/framework/zznamezz/watir/flows/flow_objects.rb', line 314

def check_valid_type
  case @type
    when :button, :link, :list, :checkbox
      check_valid_size(0)
    when :string, :p, :div
      check_valid_size(@size) # TODO a pointless call - @size will compared against itself!
    else
      raise "#{@type} is not a valid type for FlowField"
  end
end

#get_default_sizeObject

Retrieves default sizes for fields Assumes @type is set



293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/taft_files/framework/zznamezz/watir/flows/flow_objects.rb', line 293

def get_default_size
  size = 0
  case @type
    when :button, :link, :list, :checkbox
      # do nothing
    when :string
      size = 32
    when :p, :div # these will be read-only so this doesn't really matter
      size = 4000
    else
      raise "#{@type} is not a valid type for FlowField"
  end
  size
end

#random_valid_valueObject

Generate a random value based on its type



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/taft_files/framework/zznamezz/watir/flows/flow_objects.rb', line 354

def random_valid_value
  value = nil
  case @type
    when :string
      value = rand_string(@size) # TODO : vary size of random string?
    when :checkbox
      #      value = (rand(2) == 1) # TODO : need to have the object itself have defined what is a valid and invalid
      # value
      value = true # most checkboxes will want to be ticked, but it is plausable that the valid value for some of them
      # is to be unticked
    when :list
      # TODO difficult - need to pick a random item from the list. How do we know its contents?
      # Maybe pick a random number, not greater than the size of the list, then set by index/position?
      if @custom_valid_value_definition != nil # if not nil, custom_valid_value_definition should be an array of the valid options
        value = @custom_valid_value_definition.random
      end
    when :button
      # do nothing
    else
      raise "Do not know how to generate a random valid value for FlowField of type #{@type}"
  end
  value
end

#to_sObject

TODO needed?



347
348
349
350
351
# File 'lib/taft_files/framework/zznamezz/watir/flows/flow_objects.rb', line 347

def to_s
  s = ""
  s += "Flow field : #{@name}. Type : #{@type}. Mandatory : #{@mandatory}. Size : #{@size}"
  s
end