Class: Nextgen::Generators
- Inherits:
-
Object
- Object
- Nextgen::Generators
- Defined in:
- lib/nextgen/generators.rb
Class Method Summary collapse
Instance Method Summary collapse
- #activate(*optional_generators) ⇒ Object
- #add(name, node: false, prompt: nil, description: nil) ⇒ Object
- #all_active ⇒ Object
- #deactivate_node ⇒ Object
-
#initialize ⇒ Generators
constructor
A new instance of Generators.
- #node_active? ⇒ Boolean
- #optional ⇒ Object
- #to_ruby_script ⇒ Object
Constructor Details
#initialize ⇒ Generators
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.("../../config/generators.yml", __dir__) new.tap do |g| YAML.load_file(yaml_path).each do |name, | ||= {} requirements = Array(["requires"]) next unless requirements.all? { |req| rails_opts.public_send(:"#{req}?") } g.add( name.to_sym, prompt: ["prompt"], description: ["description"], node: !!["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
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_active ⇒ Object
35 36 37 38 39 |
# File 'lib/nextgen/generators.rb', line 35 def all_active generators.each_with_object([]) do |(name, ), result| result << name if [:active] end end |
#deactivate_node ⇒ Object
64 65 66 |
# File 'lib/nextgen/generators.rb', line 64 def deactivate_node generators.fetch(:node)[:active] = false end |
#node_active? ⇒ Boolean
31 32 33 |
# File 'lib/nextgen/generators.rb', line 31 def node_active? !!generators.fetch(:node)[:active] end |
#optional ⇒ Object
49 50 51 52 53 |
# File 'lib/nextgen/generators.rb', line 49 def optional generators.each_with_object({}) do |(name, ), result| result[[:prompt]] = name if [:prompt] end end |
#to_ruby_script ⇒ Object
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.("../nextgen", __dir__).inspect} extend Nextgen::Actions with_nextgen_source_path do #{apply_statements.join("\n ")} end SCRIPT end |