Class: FoodTruck::Param
- Inherits:
-
Object
- Object
- FoodTruck::Param
- Defined in:
- lib/food_truck/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 attribute.
Class Method Summary collapse
-
.create(args = {}) ⇒ FoodTruck::Param
Constructor for a Param.
- .from_array(array) ⇒ Array<FoodTruck::Param>
Instance Method Summary collapse
Instance Attribute Details
#default ⇒ String
Default value for the Param.
Set optional as true for a default nil value.
20 21 22 |
# File 'lib/food_truck/models/param.rb', line 20 def default @default end |
#description ⇒ String
Description of the param. Markdown is supported.
13 14 15 |
# File 'lib/food_truck/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/food_truck/models/param.rb', line 27 def keyword @keyword end |
#name ⇒ String
Name of the param.
10 11 12 |
# File 'lib/food_truck/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/food_truck/models/param.rb', line 24 def optional @optional end |
Class Method Details
.create(args = {}) ⇒ FoodTruck::Param
Constructor for a Param. Use create, not new.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/food_truck/models/param.rb', line 39 def self.create(args = {}) p = FoodTruck::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<FoodTruck::Param>
52 53 54 |
# File 'lib/food_truck/models/param.rb', line 52 def self.from_array(array) return array.map { |p| self.create(p) } end |
Instance Method Details
#render ⇒ String
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/food_truck/models/param.rb', line 57 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?
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/food_truck/models/param.rb', line 71 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 (FoodTruck::QUOTE + self.default + FoodTruck::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
85 86 87 |
# File 'lib/food_truck/models/param.rb', line 85 def render_doc return "# @param #{self.name} [#{self.type}] #{self.description}".strip end |