Method: Generators::TDriverBehaviourGenerator#process_method_arguments_section

Defined in:
lib/tdriver-devtools/behaviour/xml/rdoc_behaviour_xml_generator.rb

#process_method_arguments_section(source, params_array) ⇒ Object



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# File 'lib/tdriver-devtools/behaviour/xml/rdoc_behaviour_xml_generator.rb', line 468

def process_method_arguments_section( source, params_array )

  result = []

  current_argument = nil

  current_argument_type = nil

  current_section = nil

  argument_index = -1

  arguments_found = 0

  source.lines.to_a.each_with_index{ | line, index | 
    
    # remove cr/lf
    line.chomp!

    # remove trailing whitespaces
    line.rstrip!

    # count nesting depth
    line.match( /^(\s*)/ )

    nesting = $1.size

    # remove leading whitespaces
    line.lstrip!

    if nesting == 0

      line =~ /^([*|&]{0,1}\w+(\#\w+?)*)$/i

      unless $1.nil?

        # argument name
        current_argument = $1 

        arguments_found += 1 unless line.include?( "#" )

        current_section = nil

        current_argument_type = nil

        result << { current_argument => { :argument_type_order => [], :types => {} } }

        argument_index += 1

      end

    else

      # is line content class name? (argument variable type)
      line =~ /^(.*)$/i

      if !$1.nil? && ( 65..90 ).include?( $1[0] ) && nesting == 1 # "Array", "String", "Integer"

        #Kernel.const_get( $1 ) rescue abort( "Line %s: \"%s\" is not valid argument variable type. (e.g. OK: \"String\", \"Array\", \"Fixnum\" etc) " % [ index +1, $1 ] )

        current_argument_type = $1

        result[ argument_index ][ current_argument ][ :argument_type_order ] << $1

        result[ argument_index ][ current_argument ][ :types ][ current_argument_type ] = {}

        #result[ argument_index ][ current_argument ][ current_argument_type ] = {}

        current_section = nil

      else

        raise_error("Unable add argument details (line %s: \"%s\") for \"%s\" due to argument variable type must be defined first.\nPlease note that argument type must start with capital letter (e.g. OK: \"String\" NOK: \"string\")" % [ index + 1, line, current_argument  ] ) if current_argument_type.nil?

        line =~ /^(.*?)\:{1}($|[\r\n\t\s]{1})(.*)$/i

        if $1.nil?

          raise_error("Unable add argument details (line %s: \"%s\") for \"%s\" due to section name not defined. Sections names are written in lowercase with trailing colon and whitespace (e.g. OK: \"example: 10\", NOK: \"example:10\")" % [ index +1, line, current_argument]) if $1.nil? && current_section.nil?

          # remove leading & trailing whitespaces
          section_content = line.strip

        else

          current_section = $1

          if result[ argument_index ][ current_argument ][ :types ].has_key?( current_argument_type )

            #unless result[ argument_index ][ current_argument ][ current_argument_type ].has_key?( current_section )
            unless result[ argument_index ][ current_argument ][ :types ][ current_argument_type ].has_key?( current_section )

              #result[ argument_index ][ current_argument ][ current_argument_type ][ current_section ] = ""
              result[ argument_index ][ current_argument ][ :types ][ current_argument_type ][ current_section ] = ""

            end
        

          end

          section_content = $3.strip

        end

        raise_error("Unable add argument details due to argument not defined. Argument name must start from pos 1 of comment. (e.g. \"# my_variable\" NOK: \"#  my_variable\", \"#myvariable\")") if current_argument.nil?  

        if result[ argument_index ][ current_argument ][ :types ].has_key?( current_argument_type )

          if result[ argument_index ][ current_argument ][ :types ][ current_argument_type ].has_key?( current_section )


            # add one leading whitespace if current_section value is not empty 
            #section_content = " " + section_content unless result[ argument_index ][ current_argument ][ current_argument_type ][ current_section ].empty?
            section_content = " " + section_content unless result[ argument_index ][ current_argument ][ :types ][ current_argument_type ][ current_section ].empty?

            # store section_content to current_section
            #result[ argument_index ][ current_argument ][ current_argument_type ][ current_section ] << section_content
            result[ argument_index ][ current_argument ][ :types ][ current_argument_type ][ current_section ] << section_content

        end 

        end

      end

    end

  }

  order = []
  
  params_array = params_array.collect{ | o | [ o.first, Hash[:default, o[1], :optional, o.last] ] }

  params_hash = Hash[ params_array ]

  default_value_already_set = []

  params_array.each{ | param |

    if ( item = result.select{ | arg | arg.keys.include?( param.first ) }.to_a).empty? # Array conversion for ruby 1.9 compatibility

      raise_error("Error: Argument '#{ param.first }' is implemented but not documented in '#{ @current_method.name }' ($MODULE).\nNote that documented argument and variable name must be identical.", [ 'writer', 'accessor' ].include?( @processing ) ? 'attr_argument' : 'arguments' ) unless param.first.to_s.include?("#")

      order << { param.first => {} }

    else

      arg = item.first
      arg_name = arg.keys.first

      # apply overriding default argument value from documentation
      arg[ arg_name ][ :argument_type_order ].each{ | type_name |

        type_hash = arg[ arg_name ][ :types ][ type_name ]

        unless type_hash["default"].nil?

          if arg[ arg_name ][ :default ].nil?

            if params_hash[  arg_name ][ :optional ] == false

              raise_error("Error: Default value given for mandatory argument #{ arg_name } (type: #{ type_name }).", 'default_value_mandatory_argument' )

            else

              arg[ arg_name ][ :default ] = type_hash["default"] 

            end

          else
            raise_error("Error: Default value already given for #{ arg_name } (type: #{ type_name }).", 'default_argument_value_already_given' )
          end
 
        end
      }
  
      # if optional parameter and no overriding value defined, add one from implementation
      if arg[ arg_name ][:default].nil?

        arg[ arg_name ][ :default ] = params_hash[ arg_name ][ :default ] if params_hash[ arg_name ][ :optional ] == true

      end

      order << arg
    
    end
  
  }
  
  # collect all argument names and add block arguments if any   
  found_keys = order.collect{ | pair | pair.keys }.flatten

  # collect all documented argument names
  documented_arguments = result.collect{ | arg | arg.keys }.flatten

  unimplemented_arguments = ( documented_arguments - found_keys )

  unless [ :attributes ].include?( @processing )

    unless unimplemented_arguments.empty?

      unimplemented_arguments.each{ | argument |

        raise_error("Error: Argument '#{ argument }' is documented but not implemented in '#{ @current_method.name }' ($MODULE).\nNote that documented argument and variable name must be identical.", [ :attributes ].include?( @processing ) ? 'attr_argument' : 'arguments' )

      }

      # remove unimplemented argument documentation
      result = result.select{ | documented_argument |

        # always add block parameters
        unimplemented_arguments.include?( documented_argument.to_a.flatten.first ) == false || documented_argument.to_a.flatten.first.include?("#")

      }.to_a # Array conversion for ruby 1.9 compatiblity

    end

  end
  
  # add missing/undocumented arguments to order list
  missing = result.collect{ | value | order << value unless found_keys.include?( value.keys.first ) }

  [ order, arguments_found ]

end