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

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, **options) ⇒ Base

Returns a new instance of Base.



28
29
30
31
# File 'lib/shaf/generator/migration/base.rb', line 28

def initialize(*args, **options)
  @args = args
  @options = options
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



8
9
10
# File 'lib/shaf/generator/migration/base.rb', line 8

def args
  @args
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/shaf/generator/migration/base.rb', line 8

def options
  @options
end

Class Method Details

.identified_byObject



23
24
25
# File 'lib/shaf/generator/migration/base.rb', line 23

def identified_by
  @identifiers
end

.identifier(*ids) ⇒ Object



15
16
17
# File 'lib/shaf/generator/migration/base.rb', line 15

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

.inherited(child) ⇒ Object



11
12
13
# File 'lib/shaf/generator/migration/base.rb', line 11

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

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



19
20
21
# File 'lib/shaf/generator/migration/base.rb', line 19

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

Instance Method Details

#add_change(change) ⇒ Object



46
47
48
49
# File 'lib/shaf/generator/migration/base.rb', line 46

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

#callObject



33
34
35
36
37
38
39
40
# File 'lib/shaf/generator/migration/base.rb', line 33

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



51
52
53
54
55
56
57
58
59
60
# File 'lib/shaf/generator/migration/base.rb', line 51

def column_def(str, create: true)
  _, col_type = str.split(':')
  type = Types.find(col_type)
  return type.build(str, create: create, alter: !create) if type

  raise <<~ERR
    No supported DB column types for: #{col_type}
    Supported types: #{Types.all.map(&:name).join(', ')}
  ERR
end

#table_nameObject



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

def table_name
  (args.first || '').tr('/', '_')
end

#target(name) ⇒ Object



62
63
64
65
# File 'lib/shaf/generator/migration/base.rb', line 62

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