Class: Ginny::Class

Inherits:
Object
  • Object
show all
Defined in:
lib/ginny/models/class.rb

Overview

Used to generate a class.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializevoid



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

#attrsArray<Ginny::Attr>

An array of Attrs.

Returns:



21
22
23
# File 'lib/ginny/models/class.rb', line 21

def attrs
  @attrs
end

#bodyString

String to write into the body of the class.

Returns:

  • (String)


24
25
26
# File 'lib/ginny/models/class.rb', line 24

def body
  @body
end

#descriptionString

Description of the class. Markdown is supported.

Returns:

  • (String)


12
13
14
# File 'lib/ginny/models/class.rb', line 12

def description
  @description
end

#file_prefixString

String to prepend to the name of the generated file.

Returns:

  • (String)


27
28
29
# File 'lib/ginny/models/class.rb', line 27

def file_prefix
  @file_prefix
end

#modulesString

List of modules to declare the class inside.

Returns:

  • (String)


18
19
20
# File 'lib/ginny/models/class.rb', line 18

def modules
  @modules
end

#nameString

Name of the class.

Returns:

  • (String)


9
10
11
# File 'lib/ginny/models/class.rb', line 9

def name
  @name
end

#parentString

Name of a class to inherit from. (Ex: YourNewClass < Parent)

Returns:

  • (String)


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.

Parameters:

  • args (Hash<Symbol>) (defaults to: {})

Returns:



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_nameString

Returns:

  • (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_nameString

Returns:

  • (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.

Parameters:

  • folder (String) (defaults to: ".")

Returns:

  • (String)


56
57
58
59
60
# File 'lib/ginny/models/class.rb', line 56

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

#renderString

Return generated code as a string.

Returns:

  • (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_attributesString

Returns:

  • (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