Class: MethodParser

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

Overview

Parses method code

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method) ⇒ MethodParser

Returns a new instance of MethodParser.

Parameters:

  • method (YARD::CodeObjects::MethodObject)

    YARD method object to be parsed



11
12
13
14
15
16
17
18
# File 'lib/method_parser.rb', line 11

def initialize(method)
  @method      = method
  @name        = @method.name
  @parameters  = @method.parameters
  @param_tags  = FileParser.select_param_tags @method
  @option_tags = FileParser.select_option_tags @method
  @cmd_opts    = nil
end

Instance Attribute Details

#cmd_optsObject

Returns the value of attribute cmd_opts.



8
9
10
# File 'lib/method_parser.rb', line 8

def cmd_opts
  @cmd_opts
end

#methodObject (readonly)

Returns the value of attribute method.



3
4
5
# File 'lib/method_parser.rb', line 3

def method
  @method
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/method_parser.rb', line 3

def name
  @name
end

#option_tagsObject (readonly)

Returns the value of attribute option_tags.



3
4
5
# File 'lib/method_parser.rb', line 3

def option_tags
  @option_tags
end

#param_tagsObject (readonly)

Returns the value of attribute param_tags.



3
4
5
# File 'lib/method_parser.rb', line 3

def param_tags
  @param_tags
end

Instance Method Details

#default_paramsHash

Returns default values for parameters.

Returns:

  • (Hash)

    default values for parameters



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/method_parser.rb', line 75

def default_params
  @parameters.map do |array|
    array.map do |a|
      if a
        ['"', "'"].include?(a[0]) && ['"', "'"].include?(a[-1]) ? a[1..-2] : a
      else
        a
      end
    end
  end.to_h
end

#option_tags_namesArray(String)

Returns Names of options.

Returns:

  • (Array(String))

    Names of options



62
63
64
# File 'lib/method_parser.rb', line 62

def option_tags_names
  option_tags.map { |t| t.pair.name.delete(':') }
end

#options_group?(param_name) ⇒ Boolean

Check if the name is an option

Parameters:

  • param_name (String)

    name of parameter to be verified

Returns:

  • (Boolean)

    true if current parameter name is an option key



70
71
72
# File 'lib/method_parser.rb', line 70

def options_group?(param_name)
  option_tags.any? { |t| t.name == param_name }
end

#param_tags_namesArray(String)

Returns Names of parameters.

Returns:

  • (Array(String))

    Names of parameters



57
58
59
# File 'lib/method_parser.rb', line 57

def param_tags_names
  param_tags.map(&:name)
end

#params_arrayObject

Prepare



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/method_parser.rb', line 21

def params_array
  expected_params = @parameters.map(&:first).map.with_index { |p, i| [i, p] }.to_h
  options_groups  = {}
  get_params      = {}

  expected_params.each do |index, name|
    if options_group?(name)
      options_groups[index] = name
      get_params[index]     = option_as_hash(name)
    else
      get_params[index] = @cmd_opts[name.to_sym]
    end
  end
  get_params = get_params.to_a.sort_by { |a| a[0] }.reverse

  stop_delete = false
  get_params.delete_if do |a|
    index  = a[0]
    value  = a[1]
    result = false

    if options_groups[index]
      result = value == {}
    else
      result = value == nil
    end
    stop_delete = true unless result
    next if stop_delete
    result
  end

  get_params.sort_by { |a| a[0] }.map { |a| a[1] }
end