Class: Shaf::Generator::Migration::Type

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, create_template:, alter_template:, validator: nil) ⇒ Type

Returns a new instance of Type.



7
8
9
10
11
12
13
# File 'lib/shaf/generator/migration/type.rb', line 7

def initialize(str, create_template:, alter_template:, validator: nil)
  @name = str.downcase.to_sym
  @create_template = create_template.freeze
  @alter_template = alter_template.freeze
  @validator = validator.freeze
  freeze
end

Instance Attribute Details

#alter_templateObject (readonly)

Returns the value of attribute alter_template.



5
6
7
# File 'lib/shaf/generator/migration/type.rb', line 5

def alter_template
  @alter_template
end

#create_templateObject (readonly)

Returns the value of attribute create_template.



5
6
7
# File 'lib/shaf/generator/migration/type.rb', line 5

def create_template
  @create_template
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/shaf/generator/migration/type.rb', line 5

def name
  @name
end

#validatorObject (readonly)

Returns the value of attribute validator.



5
6
7
# File 'lib/shaf/generator/migration/type.rb', line 5

def validator
  @validator
end

Instance Method Details

#==(other) ⇒ Object



60
61
62
# File 'lib/shaf/generator/migration/type.rb', line 60

def ==(other)
  name == other.name
end

#build(str, create: false, alter: false) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/shaf/generator/migration/type.rb', line 15

def build(str, create: false, alter: false)
  args = parse_args(str)
  validate!(*args)

  if create && !alter
    build_create_string(*args)
  elsif alter && !create
    build_alter_string(*args)
  else
    [
      build_create_string(*args),
      build_alter_string(*args)
    ]
  end
end

#build_alter_string(*args) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/shaf/generator/migration/type.rb', line 45

def build_alter_string(*args)
  format alter_template, *args
rescue ArgumentError
  raise Command::ArgumentError,
    "Wrong number of arguments for type #{name} with string " \
    "template '#{alter_template}. Given: #{args}"
end

#build_create_string(*args) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/shaf/generator/migration/type.rb', line 37

def build_create_string(*args)
  format create_template, *args
rescue ArgumentError
  raise Command::ArgumentError,
    "Wrong number of arguments for type #{name} with string " \
    "template '#{create_template}. Given: #{args}"
end

#parse_args(str) ⇒ Object



31
32
33
34
35
# File 'lib/shaf/generator/migration/type.rb', line 31

def parse_args(str)
  name, col_type  = str.to_s.downcase.split(':')
  _, *args = col_type&.split(',')
  args.unshift name
end

#validate!(*args) ⇒ Object



53
54
55
56
57
58
# File 'lib/shaf/generator/migration/type.rb', line 53

def validate!(*args)
  errors = Array(validator&.call(name, *args))
  return if errors.empty?

  raise "Failed to process '#{name}': #{errors&.join(', ')}"
end