Class: Nextgen::Generators

Inherits:
Object
  • Object
show all
Defined in:
lib/nextgen/generators.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGenerators

Returns a new instance of Generators.



27
28
29
# File 'lib/nextgen/generators.rb', line 27

def initialize
  @generators = {}
end

Class Method Details

.compatible_with(rails_opts:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/nextgen/generators.rb', line 7

def self.compatible_with(rails_opts:)
  yaml_path = File.expand_path("../../config/generators.yml", __dir__)
  new.tap do |g|
    YAML.load_file(yaml_path).each do |name, options|
      options ||= {}
      requirements = Array(options["requires"])
      next unless requirements.all? { |req| rails_opts.public_send(:"#{req}?") }

      g.add(
        name.to_sym,
        prompt: options["prompt"],
        description: options["description"],
        node: !!options["node"]
      )
    end

    g.deactivate_node unless rails_opts.requires_node?
  end
end

Instance Method Details

#activate(*optional_generators) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/nextgen/generators.rb', line 55

def activate(*optional_generators)
  optional_generators.each do |name|
    name = name.to_sym
    gen = generators.fetch(name)
    gen[:active] = true
    activate(:node) if name != :node && gen[:node]
  end
end

#add(name, node: false, prompt: nil, description: nil) ⇒ Object

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
# File 'lib/nextgen/generators.rb', line 41

def add(name, node: false, prompt: nil, description: nil)
  name = name.to_sym
  raise ArgumentError, "Generator #{name.inspect} was already added" if generators.key?(name)

  generators[name] = {node:, prompt:, description:}
  activate(name) unless prompt
end

#all_activeObject



35
36
37
38
39
# File 'lib/nextgen/generators.rb', line 35

def all_active
  generators.each_with_object([]) do |(name, meta), result|
    result << name if meta[:active]
  end
end

#deactivate_nodeObject



64
65
66
# File 'lib/nextgen/generators.rb', line 64

def deactivate_node
  generators.fetch(:node)[:active] = false
end

#node_active?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/nextgen/generators.rb', line 31

def node_active?
  !!generators.fetch(:node)[:active]
end

#optionalObject



49
50
51
52
53
# File 'lib/nextgen/generators.rb', line 49

def optional
  generators.each_with_object({}) do |(name, meta), result|
    result[meta[:prompt]] = name if meta[:prompt]
  end
end

#to_ruby_scriptObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/nextgen/generators.rb', line 68

def to_ruby_script
  apply_statements = all_active.map do |generator|
    description = generators.fetch(generator)[:description]
    path = Nextgen.generators_path.join("#{generator}.rb")
    "apply_as_git_commit #{path.to_s.inspect}, message: #{description.inspect}"
  end

  <<~SCRIPT
    require #{File.expand_path("../nextgen", __dir__).inspect}
    extend Nextgen::Actions

    with_nextgen_source_path do
      #{apply_statements.join("\n  ")}
    end
  SCRIPT
end