Class: Ginny::Func
- Inherits:
-
Object
- Object
- Ginny::Func
- Defined in:
- lib/ginny/models/func.rb
Overview
Used to generate a method.
Instance Attribute Summary collapse
-
#body ⇒ String
String to write into the body of the function.
-
#description ⇒ String
Description of the function.
-
#modules ⇒ String
List of modules to declare the function inside of.
-
#name ⇒ String
Name of the function.
-
#params ⇒ Array<Param>
An array of Params.
-
#return_type ⇒ String
Return type of the function.
Class Method Summary collapse
-
.create(args = {}) ⇒ Ginny::Func
Constructor for a Func.
Instance Method Summary collapse
- #initialize ⇒ void constructor
-
#render ⇒ String
Return generated code as a string.
- #render_description ⇒ String
- #render_params ⇒ String
- #render_return_type ⇒ String
Constructor Details
#initialize ⇒ void
28 29 30 31 |
# File 'lib/ginny/models/func.rb', line 28 def initialize() self.params = [] self.modules = [] end |
Instance Attribute Details
#body ⇒ String
String to write into the body of the function.
19 20 21 |
# File 'lib/ginny/models/func.rb', line 19 def body @body end |
#description ⇒ String
Description of the function. Markdown is supported.
13 14 15 |
# File 'lib/ginny/models/func.rb', line 13 def description @description end |
#modules ⇒ String
List of modules to declare the function inside of.
22 23 24 |
# File 'lib/ginny/models/func.rb', line 22 def modules @modules end |
#name ⇒ String
Name of the function.
10 11 12 |
# File 'lib/ginny/models/func.rb', line 10 def name @name end |
#params ⇒ Array<Param>
An array of Params.
25 26 27 |
# File 'lib/ginny/models/func.rb', line 25 def params @params end |
#return_type ⇒ String
Return type of the function.
16 17 18 |
# File 'lib/ginny/models/func.rb', line 16 def return_type @return_type end |
Class Method Details
.create(args = {}) ⇒ Ginny::Func
Constructor for a Func. Use create
, not new
.
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ginny/models/func.rb', line 37 def self.create(args = {}) f = Ginny::Func.new() f.name = args[:name] f.description = args[:description] f.return_type = args[:return_type] f.body = args[:body] f.modules = args[:modules] unless args[:modules].nil? f.params = Ginny::Param.from_array(args[:params]) if args[:params]&.is_a?(Array) return f end |
Instance Method Details
#render ⇒ String
Return generated code as a string.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ginny/models/func.rb', line 51 def render() # return self.render_compact() if self.body.nil? && self.params.length == 0 parts = [] parts << self.render_description() parts << self.params.map(&:render_doc).join("\n") unless self.params.length == 0 parts << self.render_return_type() parts << "def " + self.name + self.render_params() parts << self.body.indent(2) unless self.body.nil? parts << "end" body = parts.compact.join("\n").gsub(/(\s+)$/, "") return Ginny.mod(body, self.modules) if self.modules.length > 0 return body end |
#render_description ⇒ String
83 84 85 |
# File 'lib/ginny/models/func.rb', line 83 def render_description() return (self.description&.length&.positive? ? self.description.comment.strip : nil) end |
#render_params ⇒ String
68 69 70 71 72 73 74 |
# File 'lib/ginny/models/func.rb', line 68 def render_params() return "()" if self.params.length == 0 # if self.params.length >= 5 # return "(\n" + self.params.map(&:render).join(",\n").indent(2) + "\n)" # end return "(" + self.params.map(&:render).join(", ") + ")" end |
#render_return_type ⇒ String
77 78 79 80 |
# File 'lib/ginny/models/func.rb', line 77 def render_return_type type = self.return_type.nil? ? "void" : self.return_type return "# @return [#{type}]" end |