Class: Shaf::Generator::Migration::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/shaf/generator/migration.rb

Direct Known Subclasses

AddColumn, CreateTable, DropColumn, Empty, RenameColumn

Constant Summary collapse

DB_COL_TYPES =
{
  integer:    ['Integer :%s', ':%s, Integer'],
  varchar:    ['String %s', ':%s, String'],
  string:     ['String :%s', ':%s, String'],
  text:       ['String :%s, text: true', ':%s, String, text: true'],
  blob:       ['File :%s', ':%s, File'],
  bigint:     ['Bignum :%s', ':%s, Bignum'],
  double:     ['Float :%s', ':%s, Float'],
  numeric:    ['BigDecimal :%s', ':%s, BigDecimal'],
  date:       ['Date :%s', ':%s, Date'],
  timestamp:  ['DateTime :%s', ':%s, DateTime'],
  time:       ['Time :%s', ':%s, Time'],
  bool:       ['TrueClass :%s', ':%s, TrueClass'],
  boolean:    ['TrueClass :%s', ':%s, TrueClass'],
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.



57
58
59
# File 'lib/shaf/generator/migration.rb', line 57

def initialize(*args)
  @args = args.dup
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



41
42
43
# File 'lib/shaf/generator/migration.rb', line 41

def args
  @args
end

Class Method Details

.identifier(*ids) ⇒ Object



48
49
50
# File 'lib/shaf/generator/migration.rb', line 48

def identifier(*ids)
  @identifiers = ids.flatten.map(&:to_s)
end

.inherited(child) ⇒ Object



44
45
46
# File 'lib/shaf/generator/migration.rb', line 44

def inherited(child)
  Factory.register(child)
end

.usage(str = nil, &block) ⇒ Object



52
53
54
# File 'lib/shaf/generator/migration.rb', line 52

def usage(str = nil, &block)
  @usage = str || block
end

Instance Method Details

#add_change(change) ⇒ Object



70
71
72
73
# File 'lib/shaf/generator/migration.rb', line 70

def add_change(change)
  @changes ||= []
  @changes << change if change
end

#callObject



61
62
63
64
65
66
67
68
# File 'lib/shaf/generator/migration.rb', line 61

def call
  validate_args
  name = compile_migration_name
  compile_changes
  [target(name), render]
rescue StandardError => e
  raise Command::ArgumentError, e.message
end

#column_def(str, create: true) ⇒ Object



80
81
82
83
# File 'lib/shaf/generator/migration.rb', line 80

def column_def(str, create: true)
  name, type = str.split(':')
  format db_type(type)[create ? 0 : 1], name.downcase
end

#db_type(type) ⇒ Object



75
76
77
78
# File 'lib/shaf/generator/migration.rb', line 75

def db_type(type)
  type ||= :string
  DB_COL_TYPES[type.to_sym] or raise "Column type '#{type}' not supported"
end

#target(name) ⇒ Object



85
86
87
88
# File 'lib/shaf/generator/migration.rb', line 85

def target(name)
  raise "Migration filename is nil" unless name
  "db/migrations/#{timestamp}_#{name}.rb"
end