Class: Ginny::Param
- Inherits:
-
Object
- Object
- Ginny::Param
- Defined in:
- lib/ginny/models/param.rb
Overview
Used to generate a function parameter.
Instance Attribute Summary collapse
-
#default ⇒ String
Default value for the param.
-
#description ⇒ String
Description of the param.
-
#keyword ⇒ Boolean
If
true
, the param will be generated as a keyword argument. -
#name ⇒ String
Name of the param.
-
#optional ⇒ Boolean
If
true
, the default value will benil
. -
#type ⇒ String
Type of the param.
Class Method Summary collapse
-
.create(args = {}) ⇒ Ginny::Param
Constructor for a Param.
- .from_array(array) ⇒ Array<Ginny::Param>
Instance Method Summary collapse
-
#render ⇒ String
Return generated code as a string.
- #render_default_value ⇒ String?
- #render_doc ⇒ String
Instance Attribute Details
#default ⇒ String
Default value for the param.
Set optional
as true
for a default nil
value.
20 21 22 |
# File 'lib/ginny/models/param.rb', line 20 def default @default end |
#description ⇒ String
Description of the param. Markdown is supported.
13 14 15 |
# File 'lib/ginny/models/param.rb', line 13 def description @description end |
#keyword ⇒ Boolean
If true
, the param will be generated as a keyword argument.
27 28 29 |
# File 'lib/ginny/models/param.rb', line 27 def keyword @keyword end |
#name ⇒ String
Name of the param.
10 11 12 |
# File 'lib/ginny/models/param.rb', line 10 def name @name end |
#optional ⇒ Boolean
If true
, the default value will be nil
.
FIXME: This is a workaround for the fact that that passing nil
to default
messes with conditionals. Not sure of a simpler way to do this.
24 25 26 |
# File 'lib/ginny/models/param.rb', line 24 def optional @optional end |
#type ⇒ String
Type of the param.
16 17 18 |
# File 'lib/ginny/models/param.rb', line 16 def type @type end |
Class Method Details
.create(args = {}) ⇒ Ginny::Param
Constructor for a Param. Use create
, not new
.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ginny/models/param.rb', line 39 def self.create(args = {}) p = Ginny::Param.new() p.name = args[:name] p.description = args[:description] p.type = args[:type] p.default = args[:default] p.keyword = args[:keyword] || false p.optional = args[:optional] || false return p end |
.from_array(array) ⇒ Array<Ginny::Param>
52 53 54 |
# File 'lib/ginny/models/param.rb', line 52 def self.from_array(array) return array.map { |p| self.create(p) } end |
Instance Method Details
#render ⇒ String
Return generated code as a string.
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ginny/models/param.rb', line 59 def render parts = [] parts << @name default = self.render_default_value() if @keyword parts << ":" parts << (" " + default) if default elsif default parts << (" = " + default) end return parts.compact.join("").gsub(/\s+$/, "") end |
#render_default_value ⇒ String?
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ginny/models/param.rb', line 73 def render_default_value # `nil` default value return "nil" if self.optional # No default value return nil if self.default.nil? # Add quotes to a String. return (Ginny::QUOTE + self.default + Ginny::QUOTE) if self.type == "String" || self.default.is_a?(String) # Add colon to a Symbol. return ":#{self.default}" if self.type == "Symbol" || self.default.is_a?(Symbol) # `to_s` should handle everything else. return self.default.to_s end |
#render_doc ⇒ String
87 88 89 |
# File 'lib/ginny/models/param.rb', line 87 def render_doc return "# @param #{self.name} [#{self.type}] #{self.description}".strip end |