Sha256: ae5da22973a3b65d98dc99f52f3efacbf74c011b1c2cde1d9a2a5a68196cd652

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

require 'nrb/commands/inside_group'

module Nrb
  module Commands
    class Generate < InsideGroup
      desc_with_warning "Generate a resource (#{Nrb.config.directories.join(', ')})"

      argument :resource, type: :string, required: true,
        desc: 'resource to generate',
        banner: 'RESOURCE',
        enum: Nrb.config.directories.map(&:singularize)


      argument :name, type: :string, required: true,
        desc: 'name of the resource',
        banner: 'NAME'

      def valid_resource?
        valid_resources = Nrb.config.directories.map(&:singularize)
        return if valid_resources.include? resource
        say "RESOURCE must be one of: #{valid_resources.join(', ')}."
        exit
      end

      def generate_resource
        template "templates/#{resource}.rb.tt", target("#{name.underscore}.rb"),
          name: name.camelize
      end

      def create_table
        return unless resource == 'model'

        migration_name = "create_#{name.underscore.pluralize}"
        options = args.join(' ')

        inside Nrb.root do
          run "rake db:new_migration name=#{migration_name} options='#{options}'"
        end
      end

      private

      def target(final = nil)
        File.join(File.expand_path(resource.pluralize), final.to_s)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nrb-1.0.0 lib/nrb/commands/generate.rb