Class: Ginny::Param

Inherits:
Object
  • Object
show all
Defined in:
lib/ginny/models/param.rb

Overview

Used to generate a function parameter.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#defaultString

Default value for the param. Set optional as true for a default nil value.

Returns:

  • (String)


20
21
22
# File 'lib/ginny/models/param.rb', line 20

def default
  @default
end

#descriptionString

Description of the param. Markdown is supported.

Returns:

  • (String)


13
14
15
# File 'lib/ginny/models/param.rb', line 13

def description
  @description
end

#keywordBoolean

If true, the param will be generated as a keyword argument.

Returns:

  • (Boolean)

    (false)



27
28
29
# File 'lib/ginny/models/param.rb', line 27

def keyword
  @keyword
end

#nameString

Name of the param.

Returns:

  • (String)


10
11
12
# File 'lib/ginny/models/param.rb', line 10

def name
  @name
end

#optionalBoolean

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.

Returns:

  • (Boolean)

    (false)



24
25
26
# File 'lib/ginny/models/param.rb', line 24

def optional
  @optional
end

#typeString

Type of the param.

Returns:

  • (String)


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.

Parameters:

  • args (Hash<Symbol>) (defaults to: {})

Options Hash (args):

  • :name (String)
  • :description (String)
  • :type (String)
  • :default (Object)
  • :keyword (Boolean) — default: false
  • :optional (Boolean) — default: false

Returns:



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>

Parameters:

  • array (Array<Hash>)

Returns:



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

#renderString

Return generated code as a string.

Returns:

  • (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_valueString?

Returns:

  • (String, nil)


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_docString

Returns:

  • (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