geode.thor
# Throws exit code 1 on errors
Throws exit code 1 on errors
Throws exit code 1 on errors
def generate(type, *args)
    case type
    when 'crystal'
      raise Error, 'ERROR: Option --singleton should not be given when generating a crystal' if options[:singleton]
      raise Error, 'ERROR: Option --with-up-down should not be given when generating a crystal' if options[:with_up_down]
      if options[:without_events] && options[:without_commands]
        raise Error, 'ERROR: Only one of --without-events, --without-commands can be given'
      end
      args.each do |crystal_name|
        generator = Generators::CrystalGenerator.new(
            crystal_name,
            without_commands: options[:without_commands],
            without_events: options[:without_events],
            without_models: options[:without_models]
        )
        generator.generate_in(options[:main] ? 'app/main' : 'app/dev')
      end
    when 'model'
      raise Error, 'ERROR: Option -m, --main should not be given when generating a model' if options[:main]
      raise Error, 'ERROR: Option --without-commands should not be given when generating a model' if options[:without_commands]
      raise Error, 'ERROR: Option --without-events should not be given when generating a model' if options[:without_events]
      raise Error, 'ERROR: Option --without-models should not be given when generating a model' if options[:without_models]
      raise Error, 'ERROR: Option --with-up-down should not be given when generating a model' if options[:with_up_down]
      name = args[0]
      fields = args[1..-1]
      raise Error, 'ERROR: Model name must be given' unless name
      if fields
        fields.map! do |field_str|
          unless (field_name, field_type = field_str.split(':')).size == 2
            raise Error, "ERROR: #{field_str} is not in the correct format of name:type"
          end
          unless Generators::ModelGenerator::VALID_FIELD_TYPES.include?(field_type)
            raise Error, "ERROR: #{field_str} has an invalid type"
          end
          if field_name == 'id' && field_type != 'primary_key'
            raise Error, 'ERROR: Field id can only be primary key'
          end
          [field_name, field_type]
        end
      else
        fields = []
      end
      generator = Generators::ModelGenerator.new(name, fields, singleton: options[:singleton])
      generator.generate_in 'app/models', 'db/migrations'
    when 'migration'
      raise Error, 'ERROR: Option -m, --main should not be given when generating a migration' if options[:main]
      raise Error, 'ERROR: Option --without-commands should not be given when generating a migration' if options[:without_commands]
      raise Error, 'ERROR: Option --without-events should not be given when generating a migration' if options[:without_events]
      raise Error, 'ERROR: Option --without-models should not be given when generating a migration' if options[:without_models]
      raise Error, 'ERROR: Option --singleton should not be given when generating a migration' if options[:singleton]
      raise Error, 'ERROR: Migration name must be given' if args.size < 1
      raise Error, 'ERROR: Only one migration name can be given' if args.size > 1
      generator = Generators::MigrationGenerator.new(args[0], with_up_down: options[:with_up_down])
      generator.generate_in 'db/migrations'
    else raise Error, 'ERROR: Type must be crystal, model or migration'
    end
  end
def generate ( type , * args ) case type when 'crystal' raise Error , 'ERROR: Option --singleton should not be given when generating a crystal' if options [ :singleton ] raise Error , 'ERROR: Option --with-up-down should not be given when generating a crystal' if options [ :with_up_down ] if options [ :without_events ] && options [ :without_commands ] raise Error , 'ERROR: Only one of --without-events, --without-commands can be given' end args . each do | crystal_name | generator = Generators :: CrystalGenerator . new ( crystal_name , without_commands : options [ :without_commands ] , without_events : options [ :without_events ] , without_models : options [ :without_models ] ) generator . generate_in ( options [ :main ] ? 'app/main' : 'app/dev' ) end when 'model' raise Error , 'ERROR: Option -m, --main should not be given when generating a model' if options [ :main ] raise Error , 'ERROR: Option --without-commands should not be given when generating a model' if options [ :without_commands ] raise Error , 'ERROR: Option --without-events should not be given when generating a model' if options [ :without_events ] raise Error , 'ERROR: Option --without-models should not be given when generating a model' if options [ :without_models ] raise Error , 'ERROR: Option --with-up-down should not be given when generating a model' if options [ :with_up_down ] name = args [ 0 ] fields = args [ 1 .. - 1 ] raise Error , 'ERROR: Model name must be given' unless name if fields fields . map! do | field_str | unless ( field_name , field_type = field_str . split ( ':' ) ) . size == 2 raise Error , "ERROR: #{field_str} is not in the correct format of name:type" end unless Generators :: ModelGenerator :: VALID_FIELD_TYPES . include? ( field_type ) raise Error , "ERROR: #{field_str} has an invalid type" end if field_name == 'id' && field_type != 'primary_key' raise Error , 'ERROR: Field id can only be primary key' end [ field_name , field_type ] end else fields = [ ] end generator = Generators :: ModelGenerator . new ( name , fields , singleton : options [ :singleton ] ) generator . generate_in 'app/models' , 'db/migrations' when 'migration' raise Error , 'ERROR: Option -m, --main should not be given when generating a migration' if options [ :main ] raise Error , 'ERROR: Option --without-commands should not be given when generating a migration' if options [ :without_commands ] raise Error , 'ERROR: Option --without-events should not be given when generating a migration' if options [ :without_events ] raise Error , 'ERROR: Option --without-models should not be given when generating a migration' if options [ :without_models ] raise Error , 'ERROR: Option --singleton should not be given when generating a migration' if options [ :singleton ] raise Error , 'ERROR: Migration name must be given' if args . size < 1 raise Error , 'ERROR: Only one migration name can be given' if args . size > 1 generator = Generators :: MigrationGenerator . new ( args [ 0 ] , with_up_down : options [ :with_up_down ] ) generator . generate_in 'db/migrations' else raise Error , 'ERROR: Type must be crystal, model or migration' end end
Throws exit code 1 on errors
Throws exit code 1 on errors
# Cases generation type # Validates that no invalid options are given when a crystal is being generated # Validates that both of --without-events and --without-commands are not given # Iterates through the given names and generates crystals for each # Validates that no invalid option is given when generating a model # Validates that a name is given # If fields were given, validates that they have the correct format, the type is valid and if an id field is # given, it is the primary key; if so, maps the array to the correct format for the generator # If fields were not given, sets fields equal to an empty array # Generates model (either standard or singleton) # Validates that no invalid option is given when generating a migration # Validates that exactly one argument (the migration name) is given # Generates migration
type
<not_specific>
args
<not_specific>