Class: MethodParser
- Inherits:
-
Object
- Object
- MethodParser
- 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
-
#cmd_opts ⇒ Object
Returns the value of attribute cmd_opts.
-
#default_values ⇒ Object
readonly
Returns the value of attribute default_values.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#option_tags ⇒ Object
readonly
Returns the value of attribute option_tags.
-
#param_tags ⇒ Object
readonly
Returns the value of attribute param_tags.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#required_parameters ⇒ Object
readonly
Returns the value of attribute required_parameters.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#trollop_opts ⇒ Object
readonly
Returns the value of attribute trollop_opts.
Instance Method Summary collapse
-
#initialize(method) ⇒ MethodParser
constructor
A new instance of MethodParser.
-
#option_tags_names ⇒ Array(String)
Names of options.
-
#options_group?(param_name) ⇒ Boolean
Check if the name is an option.
-
#param_tags_names ⇒ Array(String)
Names of parameters.
- #params_array ⇒ Object
- #prepare_opts_for_trollop ⇒ Object
Constructor Details
#initialize(method) ⇒ MethodParser
Returns a new instance of MethodParser.
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.(@method).map(&:text).join("\n") @parameters = @method.parameters @default_values = default_params @param_tags = FileParser. @method @option_tags = FileParser. @method @required_parameters = @param_tags.select { |t| t.tag_name == 'param' }.map(&:name) @cmd_opts = nil same_params = & 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_opts ⇒ Object
Returns the value of attribute cmd_opts.
13 14 15 |
# File 'lib/method_parser.rb', line 13 def cmd_opts @cmd_opts end |
#default_values ⇒ Object (readonly)
Returns the value of attribute default_values.
3 4 5 |
# File 'lib/method_parser.rb', line 3 def default_values @default_values end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
3 4 5 |
# File 'lib/method_parser.rb', line 3 def method @method end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/method_parser.rb', line 3 def name @name end |
#option_tags ⇒ Object (readonly)
Returns the value of attribute option_tags.
3 4 5 |
# File 'lib/method_parser.rb', line 3 def @option_tags end |
#param_tags ⇒ Object (readonly)
Returns the value of attribute param_tags.
3 4 5 |
# File 'lib/method_parser.rb', line 3 def @param_tags end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
3 4 5 |
# File 'lib/method_parser.rb', line 3 def parameters @parameters end |
#required_parameters ⇒ Object (readonly)
Returns the value of attribute required_parameters.
3 4 5 |
# File 'lib/method_parser.rb', line 3 def required_parameters @required_parameters end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
3 4 5 |
# File 'lib/method_parser.rb', line 3 def text @text end |
#trollop_opts ⇒ Object (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_names ⇒ Array(String)
Returns Names of options.
118 119 120 |
# File 'lib/method_parser.rb', line 118 def .map { |t| t.pair.name.delete(':') } end |
#options_group?(param_name) ⇒ Boolean
Check if the name is an option
126 127 128 |
# File 'lib/method_parser.rb', line 126 def (param_name) .any? { |t| t.name == param_name } end |
#param_tags_names ⇒ Array(String)
Returns Names of parameters.
113 114 115 |
# File 'lib/method_parser.rb', line 113 def .map(&:name) end |
#params_array ⇒ Object
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 = {} get_params = {} @parameters.map(&:first).map.with_index { |p, i| [i, p] }.to_h.each do |index, name| if (name) [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 [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_trollop ⇒ Object
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 = [] .each do |tag| tag_name = tag.name tag_text = tag.text tag_type = tag.type if tag_type == "Hash" = .select { |t| t.name == tag.name } if .count > 0 .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 |