lib/ginny/models/class.rb in ginny-0.6.1 vs lib/ginny/models/class.rb in ginny-0.6.2

- old
+ new

@@ -26,18 +26,23 @@ # @return [Boolean] attr_accessor :default_constructor # String to prepend to the name of the generated file. # @return [String] attr_accessor :file_prefix + # By default, names are *classified* using [Dry::Inflector#classify](https://github.com/dry-rb/dry-inflector#usage). + # Set this to `false` to disable *classification* and use raw `name` input. + # @return [Boolean] + attr_accessor :classify_name # @return [void] def initialize() self.attrs = [] self.modules = [] self.file_prefix = "" self.body = "" self.default_constructor = false + self.classify_name = true end # Constructor for a Class. Use `create`, not `new`. # # @param args [Hash<Symbol>] @@ -49,10 +54,11 @@ 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.classify_name = args[:classify_name] unless args[:classify_name].nil? c.default_constructor = args[:default_constructor] return c end # Write generated code out to a file. @@ -72,11 +78,10 @@ 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 @@ -89,11 +94,10 @@ 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 # @return [String] @@ -102,9 +106,12 @@ return self.attrs.map(&:render).join("\n").indent(2) end # @return [String] def class_name() + return self.name if !self.classify_name + # Don't classify two letter names. + # return self.name if self.name =~ /\A^[A-Za-z]{2}$\Z/ 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))