class FoodTruck::Class
Used to generate a [class](ruby-doc.org/core-2.6.5/Class.html)
Attributes
An array of {FoodTruck::Attr}s. @return [Array<FoodTruck::Attr>]
String
to write into the body of the class. @return [String]
Description of the class. [Markdown](github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) is supported. @return [String]
String
to prepend to the name of the generated file. @return [String]
List of modules to declare the class inside. @return [String]
Name of the class. @return [String]
Name of a class to inherit from. (Ex: `YourNewClass < Parent`) @return [String]
Public Class Methods
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
@return [void]
# File lib/food_truck/models/class.rb, line 30 def initialize() self.attrs = [] self.modules = [] self.file_prefix = "" end
Public Instance Methods
@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
@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
@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
@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
@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