Sha256: c3803d2714767c3e616ca81839bc6ccc4d5b2a60de12fc36038fef86d3eba932

Contents?: true

Size: 886 Bytes

Versions: 2

Compression:

Stored size: 886 Bytes

Contents

# frozen_string_literal: true

class EnvironmentGenerator < Rails::Generators::NamedBase
  source_root File.expand_path('../templates', __FILE__)
  argument :environment, type: :string, required: false, default: nil,
                         desc: 'The environment template to copy'

  def create_environment_file
    assert_valid_environment!
    template('environment.rb', File.join('config/environments', class_path, "#{file_name}.rb"))
  end

  private

  ENVIRONMENTS = %w[development test production].freeze

  ENVIRONMENTS.each do |env|
    define_method("#{env}?") { environment == env }
  end

  def assert_valid_environment!
    return if environment.nil? || ENVIRONMENTS.include?(environment)

    raise ArgumentError, ["Unknown environment: #{environment.inspect}.",
                          "Valid options: #{ENVIRONMENTS.map(&:inspect).join(', ')}"].join(' ')
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_generator-5.2.0 lib/generators/environment/environment_generator.rb
active_generator-5.1.0 lib/generators/environment/environment_generator.rb