class FoodTruck::Class

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

Attributes

attrs[RW]

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

body[RW]

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

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/food_truck/models/class.rb, line 40
def self.create(args = {})
  c = FoodTruck::Class.new()
  c.name        = args[:name]
  c.description = args[:description]
  c.parent      = args[:parent]
  c.modules     = args[:modules] unless args[:modules].nil?
  c.attrs       = FoodTruck::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] || ""
  return c
end
new() click to toggle source

@return [void]

# File lib/food_truck/models/class.rb, line 30
def initialize()
  self.attrs = []
  self.modules = []
  self.file_prefix = ""
end

Public Instance Methods

class_name() click to toggle source

@return [String]

# File lib/food_truck/models/class.rb, line 82
def class_name()
  inflector = Dry::Inflector.new
  return inflector.classify(inflector.underscore(self.name))
end
file_name() click to toggle source

@return [String]

# File lib/food_truck/models/class.rb, line 88
def file_name()
  inflector = Dry::Inflector.new
  return self.file_prefix + inflector.underscore(self.name) + ".rb"
end
generate(folder = ".") click to toggle source

@param folder [String] @return [String]

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

@return [String]

# File lib/food_truck/models/class.rb, line 61
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.body&.length&.positive? ? self.body.indent(2) : nil)
  parts << "end"
  if self.modules.length > 0
    body = parts.compact.join("\n").gsub(/\s+$/, "")
    return FoodTruck.mod(body, self.modules)
  end
  return parts.compact.join("\n").gsub(/\s+$/, "")
end
render_attributes() click to toggle source

@return [String]

# File lib/food_truck/models/class.rb, line 76
def render_attributes()
  return nil unless self.attrs.length > 0
  return self.attrs.map(&:render).join("\n").indent(2)
end