Class: MethodParser

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

Overview

Parses method code

Constant Summary collapse

TYPES_MAPPINGS =
{
  'String'         => :string,
  'Integer'        => :int,
  'Fixnum'         => :int,
  'Float'          => :float,
  'Boolean'        => :boolean,
  'Array(String)'  => :strings,
  'Array(Integer)' => :ints,
  'Array(Fixnum)'  => :ints,
  'Array(Float)'   => :floats,
  'Array(Boolean)' => :booleans
}.freeze

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/method_parser.rb', line 29

def initialize(method)
  @method              = method
  @name                = @method.name
  @text                = FileParser.select_runnable_tags(@method).map(&:text).join("\n")
  @parameters          = @method.parameters
  @default_values      = default_params
  @param_tags          = FileParser.select_param_tags @method
  @option_tags         = FileParser.select_option_tags @method
  @required_parameters = @param_tags.select { |t| t.tag_name == 'param' }.map(&:name)
  @cmd_opts            = nil
  same_params          = param_tags_names & option_tags_names
  unless same_params.count.zero?
    raise(
        RubyFireCLIError,
        "You have the same name for @param and @option attribute(s): #{same_params.join(', ')}.
Use different names to `ruby_fire_cli` be able to run #{@name} method."
    )
  end
  @trollop_opts = prepare_opts_for_trollop
end

Instance Attribute Details

#cmd_optsObject

Returns the value of attribute cmd_opts.



13
14
15
# File 'lib/method_parser.rb', line 13

def cmd_opts
  @cmd_opts
end

#default_valuesObject (readonly)

Returns the value of attribute default_values.



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

def default_values
  @default_values
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

#parametersObject (readonly)

Returns the value of attribute parameters.



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

def parameters
  @parameters
end

#required_parametersObject (readonly)

Returns the value of attribute required_parameters.



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

def required_parameters
  @required_parameters
end

#textObject (readonly)

Returns the value of attribute text.



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

def text
  @text
end

#trollop_optsObject (readonly)

Returns the value of attribute trollop_opts.



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

def trollop_opts
  @trollop_opts
end

Instance Method Details

#option_tags_namesArray(String)

Returns Names of options.

Returns:

  • (Array(String))

    Names of options



118
119
120
# File 'lib/method_parser.rb', line 118

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



126
127
128
# File 'lib/method_parser.rb', line 126

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



113
114
115
# File 'lib/method_parser.rb', line 113

def param_tags_names
  param_tags.map(&:name)
end

#params_arrayObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/method_parser.rb', line 87

def params_array
  options_groups = {}
  get_params     = {}
  @parameters.map(&:first).map.with_index { |p, i| [i, p] }.to_h.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      = value.nil?
    result      = value == {} if options_groups[index]
    stop_delete = true unless result
    next if stop_delete
  end
  get_params.sort_by { |a| a[0] }.map { |a| a[1] }
end

#prepare_opts_for_trollopObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/method_parser.rb', line 50

def prepare_opts_for_trollop
  result = []
  param_tags.each do |tag|
    tag_name = tag.name
    tag_text = tag.text
    tag_type = tag.type
    if tag_type == "Hash"
      options = option_tags.select { |t| t.name == tag.name }
      if options.count > 0
        options.each do |option|
          option_name = option.pair.name.delete(':')
          option_text = option.pair.text
          option_type = option.pair.type
          result << [
            option_name.to_sym,
            "(Ruby class: #{option_type}) " + option_text.to_s,
            type: parse_type(option_type)
          ]
        end
      else
        result << [
          tag_name.to_sym,
          "(Ruby class: #{tag_type}) " + tag_text.to_s,
          type: parse_type(tag_type)
        ]
      end
    else
      result << [
        tag_name.to_sym,
        "(Ruby class: #{tag_type}) " + tag_text.to_s,
        type: parse_type(tag_type)
      ]
    end
  end
  result
end