Module: Chemlab::CLI::Generator

Extended by:
Generator
Included in:
Generator
Defined in:
lib/chemlab/cli/generator.rb

Overview

Base Chemlab class generator ($ chemlab generate …)

Constant Summary collapse

INVALID_GENERATOR_ERROR =
<<~ERR
  Cannot generate `%s` as the generator does not exist.
  Possible options are %s.
ERR

Instance Method Summary collapse

Instance Method Details

#generate(what, name, args) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/chemlab/cli/generator.rb', line 16

def generate(what, name, args)
  raise ArgumentError, 'Please specify what to generate' unless what
  raise ArgumentError, 'Please specify a name' unless name

  unless possible_generators.has_key?(what)
    raise ArgumentError, INVALID_GENERATOR_ERROR % what,
          possible_generators.keys.join(',')
  end

  data = args.each_with_object({}) do |arg, h|
    k, v = arg.split('=')
    h[k] = v
  end

  # render the erb
  $stdout.puts ERB.new(File.read(possible_generators[what]),
                       trim_mode: '%<>').result_with_hash({ data: data, name: name })
end