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



37
38
39
40
41
42
43
44
# File 'lib/ginny/models/class.rb', line 37

def initialize()
  self.attrs = []
  self.modules = []
  self.file_prefix = ""
  self.body = ""
  self.default_constructor = false
  self.classify_name = true
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

#classify_nameBoolean

By default, names are classified using Dry::Inflector#classify. Set this to false to disable classification and use raw name input.

Returns:

  • (Boolean)


34
35
36
# File 'lib/ginny/models/class.rb', line 34

def classify_name
  @classify_name
end

#default_constructorBoolean

If true, a method similar to ActiveRecord::Base.create will be generated for the class.

Returns:

  • (Boolean)


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

def default_constructor
  @default_constructor
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)


30
31
32
# File 'lib/ginny/models/class.rb', line 30

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:



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ginny/models/class.rb', line 50

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.classify_name = args[:classify_name] unless args[:classify_name].nil?
  c.default_constructor = args[:default_constructor]
  return c
end

Instance Method Details

#class_nameString

Returns:

  • (String)


112
113
114
115
116
117
118
119
120
121
# File 'lib/ginny/models/class.rb', line 112

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))
end

#constructorString

Returns:

  • (String)


133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ginny/models/class.rb', line 133

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_nameString

Returns:

  • (String)


124
125
126
127
128
129
130
# File 'lib/ginny/models/class.rb', line 124

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 = ".", file: nil) ⇒ String

Write generated code out to a file.

Parameters:

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

Returns:

  • (String)


69
70
71
72
73
74
# File 'lib/ginny/models/class.rb', line 69

def generate(folder = ".", file: nil)
  name = file.nil? ? self.file_name() : file
  path = File.join(File.expand_path(folder), name)
  File.open(path, "a") { |f| f.write(self.render() + "\n") }
  return path
end

#renderString

Return generated code as a string.

Returns:

  • (String)


79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ginny/models/class.rb', line 79

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 << "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_attributesString

Returns:

  • (String)


106
107
108
109
# File 'lib/ginny/models/class.rb', line 106

def render_attributes()
  return nil unless self.attrs.length > 0
  return self.attrs.map(&:render).join("\n").indent(2)
end

#render_bodyString?

Returns:

  • (String, nil)


94
95
96
97
98
99
100
101
102
103
# File 'lib/ginny/models/class.rb', line 94

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
  return "\n" + self.constructor().indent(2) if self.default_constructor
  return nil
end