class Ginny::Attr

Used to generate an instance variable with getters/setters.

[attr]: docs.ruby-lang.org/en/master/Module.html#method-i-attr [attr_accessor]: docs.ruby-lang.org/en/master/Module.html#method-i-attr_accessor [attr_reader]: docs.ruby-lang.org/en/master/Module.html#method-i-attr_reader

Attributes

default[RW]

Default value for the attribute; set in it's Class's `initialize` function. @return [String]

description[RW]

Description of the attribute. [Markdown](github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) is supported. @return [String]

name[RW]

Name of the attribute. @return [String]

read_only[RW]

If `true`, an `attr_reader` will be generated for the attribute instead of an `attr_accessor`. @return [Boolean]

type[RW]

[Type](rubydoc.info/gems/yard/file/docs/GettingStarted.md#Declaring_Types) of the attribute. @return [String]

Public Class Methods

create(args = {}) click to toggle source

Constructor for an Attr. Use `create`, not `new`.

@param args [Hash] @return [Attr]

# File lib/ginny/models/attr.rb, line 34
def self.create(args = {})
  a = Ginny::Attr.new()
  a.name        = args[:name]
  a.description = args[:description]
  a.type        = args[:type]
  a.read_only   = args[:read_only]
  return a
end
from_array(array) click to toggle source

@param array [Array<Hash>] @return [Array<Ginny::Attr>]

# File lib/ginny/models/attr.rb, line 45
def self.from_array(array)
  return array.map { |f| self.create(f) }
end
new() click to toggle source

@return [void]

# File lib/ginny/models/attr.rb, line 26
def initialize()
  self.read_only = false
end

Public Instance Methods

render() click to toggle source

Return generated code as a string.

@return [String]

# File lib/ginny/models/attr.rb, line 52
def render()
  parts = []
  parts << (@description&.length&.positive? ? @description.comment : nil)
  parts << "@return [#{self.type}]".comment
  parts << "attr_#{self.read_only ? 'reader' : 'accessor'} :#{self.name.downcase}"
  return parts.compact.join("\n").gsub(/\s+$/, "")
end
render_dynamic() click to toggle source

Used for documenting attributes that are “declared dynamically via meta-programming”. See the documentation on [YARD directives] for more info.

[1]: www.rubydoc.info/gems/yard/file/docs/Tags.md#attribute

@return [String]

# File lib/ginny/models/attr.rb, line 66
def render_dynamic()
  parts = []
  parts << "@!attribute #{self.name.downcase} [#{self.read_only ? 'r' : 'rw'}]".comment
  parts << (@description&.length&.positive? ? @description.indent(2).comment : nil)
  parts << "@return [#{self.type}]".indent(2).comment
  return parts.compact.join("\n").gsub(/\s+$/, "")
end