lib/ginny/models/class.rb in ginny-0.5.0 vs lib/ginny/models/class.rb in ginny-0.5.2
- old
+ new
@@ -1,9 +1,9 @@
require "dry/inflector"
module Ginny
- # Used to generate a [class](https://ruby-doc.org/core-2.6.5/Class.html)
+ # Used to generate a [class](https://ruby-doc.org/core-2.6.5/Class.html).
class Class
# Name of the class.
# @return [String]
attr_accessor :name
@@ -47,30 +47,34 @@
c.body = args[:body] unless args[:body].nil?
c.file_prefix = args[:file_prefix] || ""
return c
end
+ # Write generated code out to a file.
+ #
# @param folder [String]
# @return [String]
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 generated code as a string.
+ #
# @return [String]
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+$/, "")
+ body = parts.compact.join("\n").gsub(/(\s+)$/, "")
return Ginny.mod(body, self.modules)
end
- return parts.compact.join("\n").gsub(/\s+$/, "")
+ return parts.compact.join("\n").gsub(/(\s+)$/, "")
end
# @return [String]
def render_attributes()
return nil unless self.attrs.length > 0