class GroongaClientModel::Generators::MigrationGenerator

Public Class Methods

next_migration_number(dirname) click to toggle source
# File lib/rails/generators/groonga_client_model/migration_generator.rb, line 30
def next_migration_number(dirname)
  next_migration_number = current_migration_number(dirname) + 1
  Migrator.next_migration_number(next_migration_number)
end

Public Instance Methods

create_migration_file() click to toggle source
# File lib/rails/generators/groonga_client_model/migration_generator.rb, line 53
def create_migration_file
  IllegalMigrationNameError.validate(file_name)
  decide_template(file_name)
  migration_template(@migration_template,
                     File.join(Migrator.default_search_path,
                               "#{file_name}.rb"))
end

Private Instance Methods

create_table_options(indent_size=0) click to toggle source
# File lib/rails/generators/groonga_client_model/migration_generator.rb, line 100
def create_table_options(indent_size=0)
  prefix = ",\n" + (" " * indent_size)
  table_type = @options[:table_type]
  table_type ||= "hash_table" if @key_type
  table_propose = @options[:table_propose]
  options_text = ""
  if table_type
    options_text << "#{prefix}type: :#{table_type}"
    options_text << "#{prefix}key_type: :#{@key_type}" if @key_type
  end
  options_text << "#{prefix}propose: :#{table_propose}" if table_propose
  options_text
end
decide_template(output_file_name) click to toggle source
# File lib/rails/generators/groonga_client_model/migration_generator.rb, line 62
def decide_template(output_file_name)
  @migration_template = "migration.rb"
  @migration_action = nil
  @key_type = nil
  case output_file_name
  when /\Aadd_.*_to_(.*)\z/
    @migration_action = :add
    @table_name = normalize_table_name($1)
  when /\Aremove_.*_from_(.*)\z/
    @migration_action = :remove
    @table_name = normalize_table_name($1)
  when /\Acreate_(.+)\z/
    @table_name = normalize_table_name($1)
    @migration_template = "create_table_migration.rb"
    attributes.each do |attribute|
      if attribute.name == "_key"
        @key_type = attribute.type
        break
      end
    end
  when /\Aset_config_(.*)\z/
    @migration_template = "set_config_migration.rb"
    @config_key = normalize_config_key($1)
    if attributes.empty?
      @config_value = "new value"
    else
      @config_value = attributes.first.name
    end
  when /\Adelete_config_(.*)\z/
    @migration_template = "delete_config_migration.rb"
    @config_key = normalize_config_key($1)
  end
end
normalize_attribute_type(type) click to toggle source
# File lib/rails/generators/groonga_client_model/migration_generator.rb, line 124
def normalize_attribute_type(type)
  case type
  when :string
    :short_text
  else
    type
  end
end
normalize_config_key(key) click to toggle source
# File lib/rails/generators/groonga_client_model/migration_generator.rb, line 120
def normalize_config_key(key)
  key.gsub(/_/, ".")
end
normalize_table_name(name) click to toggle source
# File lib/rails/generators/groonga_client_model/migration_generator.rb, line 96
def normalize_table_name(name)
  name.pluralize
end
target_attributes() click to toggle source
# File lib/rails/generators/groonga_client_model/migration_generator.rb, line 114
def target_attributes
  attributes.reject do |attribute|
    attribute.name == "_key"
  end
end