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.
-
#default_constructor ⇒ Boolean
If
true
, a method similar to ActiveRecord::Base.create will be generated for 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
- #constructor ⇒ 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
- #render_body ⇒ String?
Constructor Details
#initialize ⇒ void
33 34 35 36 37 38 39 |
# File 'lib/ginny/models/class.rb', line 33 def initialize() self.attrs = [] self.modules = [] self.file_prefix = "" self.body = "" self.default_constructor = false 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 |
#default_constructor ⇒ Boolean
If true
, a method similar to ActiveRecord::Base.create will be generated for the class.
27 28 29 |
# File 'lib/ginny/models/class.rb', line 27 def default_constructor @default_constructor 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.
30 31 32 |
# File 'lib/ginny/models/class.rb', line 30 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
.
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ginny/models/class.rb', line 45 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.default_constructor = args[:default_constructor] return c end |
Instance Method Details
#class_name ⇒ String
106 107 108 109 110 111 112 |
# File 'lib/ginny/models/class.rb', line 106 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 |
#constructor ⇒ String
124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/ginny/models/class.rb', line 124 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 ⇒ String
115 116 117 118 119 120 121 |
# File 'lib/ginny/models/class.rb', line 115 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.
62 63 64 65 66 |
# File 'lib/ginny/models/class.rb', line 62 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.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ginny/models/class.rb', line 71 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 << (self.body&.length&.positive? ? self.body.indent(2) : nil) 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 ⇒ String
100 101 102 103 |
# File 'lib/ginny/models/class.rb', line 100 def render_attributes() return nil unless self.attrs.length > 0 return self.attrs.map(&:render).join("\n").indent(2) end |
#render_body ⇒ String?
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ginny/models/class.rb', line 87 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 # binding.pry return "\n" + self.constructor().indent(2) if self.default_constructor return nil end |