Class: Ginny::Class
- Inherits:
-
Object
- Object
- Ginny::Class
- Defined in:
- lib/ginny/models/class.rb
Overview
Used to generate a class.
Instance Attribute Summary collapse
-
#attrs ⇒ Array<Ginny::Attr>
An array of Attrs.
-
#body ⇒ String
String to write into the body of the class.
-
#description ⇒ String
Description of the class.
-
#file_prefix ⇒ String
String to prepend to the name of the generated file.
-
#modules ⇒ String
List of modules to declare the class inside.
-
#name ⇒ String
Name of the class.
-
#parent ⇒ String
Name of a class to inherit from.
Class Method Summary collapse
-
.create(args = {}) ⇒ Class
Constructor for a Class.
Instance Method Summary collapse
- #class_name ⇒ String
- #file_name ⇒ String
-
#generate(folder = ".") ⇒ String
Write generated code out to a file.
- #initialize ⇒ void constructor
-
#render ⇒ String
Return generated code as a string.
- #render_attributes ⇒ String
Constructor Details
#initialize ⇒ void
30 31 32 33 34 |
# File 'lib/ginny/models/class.rb', line 30 def initialize() self.attrs = [] self.modules = [] self.file_prefix = "" end |
Instance Attribute Details
#attrs ⇒ Array<Ginny::Attr>
An array of Attrs.
21 22 23 |
# File 'lib/ginny/models/class.rb', line 21 def attrs @attrs end |
#body ⇒ String
String to write into the body of the class.
24 25 26 |
# File 'lib/ginny/models/class.rb', line 24 def body @body end |
#description ⇒ String
Description of the class. Markdown is supported.
12 13 14 |
# File 'lib/ginny/models/class.rb', line 12 def description @description end |
#file_prefix ⇒ String
String to prepend to the name of the generated file.
27 28 29 |
# File 'lib/ginny/models/class.rb', line 27 def file_prefix @file_prefix end |
#modules ⇒ String
List of modules to declare the class inside.
18 19 20 |
# File 'lib/ginny/models/class.rb', line 18 def modules @modules end |
#name ⇒ String
Name of the class.
9 10 11 |
# File 'lib/ginny/models/class.rb', line 9 def name @name end |
#parent ⇒ String
Name of a class to inherit from. (Ex: YourNewClass < Parent
)
15 16 17 |
# File 'lib/ginny/models/class.rb', line 15 def parent @parent end |
Class Method Details
.create(args = {}) ⇒ Class
Constructor for a Class. Use create
, not new
.
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ginny/models/class.rb', line 40 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] || "" return c end |
Instance Method Details
#class_name ⇒ String
86 87 88 89 90 91 92 |
# File 'lib/ginny/models/class.rb', line 86 def class_name() 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 |
#file_name ⇒ String
95 96 97 98 99 100 101 |
# File 'lib/ginny/models/class.rb', line 95 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 = ".") ⇒ String
Write generated code out to a file.
56 57 58 59 60 |
# File 'lib/ginny/models/class.rb', line 56 def generate(folder = ".") path = File.join(File.(folder), self.file_name()) File.open(path, "a") { |f| f.write(self.render() + "\n") } return path end |
#render ⇒ String
Return generated code as a string.
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/ginny/models/class.rb', line 65 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 Ginny.mod(body, self.modules) end return parts.compact.join("\n").gsub(/(\s+)$/, "") end |
#render_attributes ⇒ String
80 81 82 83 |
# File 'lib/ginny/models/class.rb', line 80 def render_attributes() return nil unless self.attrs.length > 0 return self.attrs.map(&:render).join("\n").indent(2) end |