class Ginny::Class

Used to generate a [class](ruby-doc.org/core-2.6.5/Class.html).

Attributes

attrs[RW]

An array of {Ginny::Attr}s. @return [Array<Ginny::Attr>]

body[RW]

String to write into the body of the class. @return [String]

classify_name[RW]

By default, names are classified using [Dry::Inflector#classify](github.com/dry-rb/dry-inflector#usage). Set this to `false` to disable classification and use raw `name` input. @return [Boolean]

default_constructor[RW]

If `true`, a method similar to [ActiveRecord::Base.create](apidock.com/rails/ActiveRecord/Persistence/ClassMethods/create) will be generated for the class. @return [Boolean]

description[RW]

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

file_prefix[RW]

String to prepend to the name of the generated file. @return [String]

modules[RW]

List of modules to declare the class inside. @return [String]

name[RW]

Name of the class. @return [String]

parent[RW]

Name of a class to inherit from. (Ex: `YourNewClass < Parent`) @return [String]

Public Class Methods

create(args = {}) click to toggle source

Constructor for a Class. Use `create`, not `new`.

@param args [Hash<Symbol>] @return [Class]

# File lib/ginny/models/class.rb, line 50
def self.create(args = {})
  c = Ginny::Class.new()
  c.name        = args[:name]
  c.description = args[:description]
  c.parent      = args[:parent]
  c.modules     = args[:modules] unless args[:modules].nil?
  c.attrs       = Ginny::Attr.from_array(args[:attrs]) if args[:attrs]&.is_a?(Array)
  c.body        = args[:body] unless args[:body].nil?
  c.file_prefix = args[:file_prefix] || ""
  c.classify_name = args[:classify_name] unless args[:classify_name].nil?
  c.default_constructor = args[:default_constructor]
  return c
end
new() click to toggle source

@return [void]

# File lib/ginny/models/class.rb, line 37
def initialize()
  self.attrs = []
  self.modules = []
  self.file_prefix = ""
  self.body = ""
  self.default_constructor = false
  self.classify_name = true
end

Public Instance Methods

class_name() click to toggle source

@return [String]

# File lib/ginny/models/class.rb, line 112
def class_name()
  return self.name if !self.classify_name
  # Don't classify two letter names.
  # return self.name if self.name =~ /\A^[A-Za-z]{2}$\Z/
  inflector = Dry::Inflector.new do |inflections|
    inflections.plural("data", "data")
    inflections.singular(/([t])a\z/i, '\1a')
  end
  return inflector.classify(inflector.underscore(self.name))
end
constructor() click to toggle source

@return [String]

# File lib/ginny/models/class.rb, line 133
def constructor()
  char = self.name.chr.downcase
  body = "#{char} = #{self.class_name}.new\n"
  body << self.attrs.map { |a| "#{char}.#{a.name} = params[:#{a.name}]\n" }.join()
  body << "return #{char}\n"
  Ginny::Func.create({
    name: "self.create",
    return_type: "self",
    params: [{ name: "params", type: "Hash", default: {} }],
    body: body,
  }).render()
end
file_name() click to toggle source

@return [String]

# File lib/ginny/models/class.rb, line 124
def file_name()
  inflector = Dry::Inflector.new do |inflections|
    inflections.plural("data", "data")
    inflections.singular(/([t])a\z/i, '\1a')
  end
  return self.file_prefix + inflector.underscore(self.name) + ".rb"
end
generate(folder = ".", file: nil) click to toggle source

Write generated code out to a file.

@param folder [String] @param file [String] @return [String]

# File lib/ginny/models/class.rb, line 69
def generate(folder = ".", file: nil)
  name = file.nil? ? self.file_name() : file
  path = File.join(File.expand_path(folder), name)
  File.open(path, "a") { |f| f.write(self.render() + "\n") }
  return path
end
render() click to toggle source

Return generated code as a string.

@return [String]

# File lib/ginny/models/class.rb, line 79
def render()
  parts = []
  parts << (self.description&.length&.positive? ? self.description.comment.strip : nil)
  parts << (self.parent.nil? ? "class #{self.class_name()}" : "class #{self.class_name()} < #{self.parent}")
  parts << self.render_attributes()
  parts << self.render_body()
  parts << "end"
  if self.modules.length > 0
    body = parts.compact.join("\n").gsub(/([[:blank:]]+)$/, "")
    return Ginny.mod(body, self.modules)
  end
  return parts.compact.join("\n").gsub(/([[:blank:]]+)$/, "")
end
render_attributes() click to toggle source

@return [String]

# File lib/ginny/models/class.rb, line 106
def render_attributes()
  return nil unless self.attrs.length > 0
  return self.attrs.map(&:render).join("\n").indent(2)
end
render_body() click to toggle source

@return [String,nil]

# File lib/ginny/models/class.rb, line 94
def render_body()
  if self.body.length > 0
    if self.default_constructor
      return ("\n" + self.constructor() + "\n\n" + self.body).indent(2)
    end
    return self.body.indent(2)
  end
  return "\n" + self.constructor().indent(2) if self.default_constructor
  return nil
end