Module: PlutoniumGenerators::Generator

Class Method Summary collapse

Methods included from Concerns::Logger

#debug, #error, #exception, #info, #success, #warn

Methods included from Concerns::Config

#read_config, #write_config

Class Method Details

.derive_association_name(from_model, to_model) ⇒ Object

Derives the association name for a reference, stripping shared namespace. e.g., derive_association_name(“Competition::TeamUser”, “Competition::Team”) => “team”



31
32
33
34
35
36
37
38
39
40
# File 'lib/generators/pu/lib/plutonium_generators/generator.rb', line 31

def self.derive_association_name(from_model, to_model)
  to_parts = to_model.underscore.split("/")

  if (shared = find_shared_namespace(from_model, to_model))
    shared_parts = shared.split("/")
    to_parts = to_parts.drop(shared_parts.length)
  end

  to_parts.join("_")
end

.find_shared_namespace(model1, model2, separator: "/") ⇒ Object

Finds the shared namespace prefix between two model names. Used to derive association names when models share a namespace. e.g., find_shared_namespace(“Competition::TeamUser”, “Competition::Team”) => “competition”



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/generators/pu/lib/plutonium_generators/generator.rb', line 16

def self.find_shared_namespace(model1, model2, separator: "/")
  parts1 = model1.underscore.split(separator)
  parts2 = model2.underscore.split(separator)

  shared = []
  [parts1.length, parts2.length].min.times do |i|
    break unless parts1[i] == parts2[i]
    shared << parts1[i]
  end

  shared.empty? ? nil : shared.join(separator)
end

.included(base) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/generators/pu/lib/plutonium_generators/generator.rb', line 42

def self.included(base)
  base.send :class_option, :interactive, type: :boolean, desc: "Show prompts. Default: true"
  base.send :class_option, :bundle, type: :boolean, desc: "Run bundle after setup. Default: true"
  base.send :class_option, :lint, type: :boolean, desc: "Run linter after generation. Default: false"

  base.include Concerns::PackageSelector
end