Class: Web::Testing::MultiHashTree

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

Overview

could I get an explanation of this? just for me? please? not to long… probably not longer than this comment here.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields = {}) ⇒ MultiHashTree

Returns a new instance of MultiHashTree.



454
455
456
457
458
459
460
461
462
# File 'lib/web/testing.rb', line 454

def initialize fields = {}
  @fields = {}
  @unmodified_fields = {}
  fields.each do |k,value_array|
    value_array.each{ |v|
      push_field k, v
    }
  end
end

Instance Attribute Details

#fieldsObject (readonly)

:nodoc:



452
453
454
# File 'lib/web/testing.rb', line 452

def fields
  @fields
end

#unmodified_fieldsObject (readonly)

:nodoc:



452
453
454
# File 'lib/web/testing.rb', line 452

def unmodified_fields
  @unmodified_fields
end

Class Method Details

.flatten(node, newhash = {}, nameroot = "") ⇒ Object

:nodoc:



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# File 'lib/web/testing.rb', line 519

def MultiHashTree.flatten node, newhash = {} , nameroot = "" # :nodoc:
  if node.kind_of? Hash
    node.each do |k,v|
      if nameroot == ""
        varname = k
      else
        varname = "#{nameroot}.#{k}" 
      end
      flatten( v, newhash, varname )
    end
  elsif node.kind_of? Array
    unless node.find { |i| !i.kind_of? String }
      newhash[nameroot] = node
    else
      node.each_with_index do |v,i|
        flatten( v, newhash, "#{nameroot}[#{i}]" )
      end
    end
  else
    newhash[ nameroot ]=node
  end
  newhash
end

Instance Method Details

#params(values) ⇒ Object



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/web/testing.rb', line 491

def params values
  retval = @unmodified_fields.clone
  values.each do |key,value|
    if value.kind_of? SelectHash
      selected_options = []
      value.each{ |k,v|
        selected_options.push k if v
      }
      
      if m = Web::CGI::MULTIPLE_KEY.match(key)
        retval[key] ||= []
        retval[key] = (retval[key] + selected_options).uniq
      else
        retval[key] = selected_options.join(",")
      end
      
    else
      if m = Web::CGI::MULTIPLE_KEY.match(key)
        retval[key] ||= []
        retval[key] << value
      else
        retval[key] = value
      end
    end
  end
  retval
end

#push_field(name, value) ⇒ Object



485
486
487
488
489
# File 'lib/web/testing.rb', line 485

def push_field name, value
  @unmodified_fields[name] ||= []
  @unmodified_fields[name].push value
  push_impl @fields, name, value
end

#push_impl(hash, name, value) ⇒ Object



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/web/testing.rb', line 468

def push_impl hash, name, value
  if name =~ Web::CGI::MULTIPLE_KEY && !value.kind_of?(Web::Testing::SelectHash)
    hash[name] ||= []
    hash[name].push  value
  elsif m = /^(\w+(\[\])?)\.(.+)/.match(name)
    hash[m[1]] ||= {}
    push_impl(hash[m[1]], m[3], value)
  elsif m = /^(\w+)\[(\d+)\].(.+)/.match(name)
    hash[m[1]] ||= []
    hash[m[1]][m[2].to_i] ||= {}
    push_impl hash[m[1]][m[2].to_i], m[3], value
  else
    hash[name] ||= []
    hash[name].push(value)
  end
end

#valid_key?(aKey) ⇒ Boolean

Returns:

  • (Boolean)


464
465
466
# File 'lib/web/testing.rb', line 464

def valid_key? (aKey)
  unmodified_fields.has_key?(aKey) || fields.has_key?(aKey)
end