Class: PGTrunk::Operations::Enums::Change

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, ActiveModel::Model, ActiveModel::Validations
Defined in:
lib/pg_trunk/operations/enums/change.rb

Overview

Definition for the value's change

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(data) ⇒ Object



11
12
13
# File 'lib/pg_trunk/operations/enums/change.rb', line 11

def self.build(data)
  data.is_a?(self) ? data : new(**data)
end

Instance Method Details

#add?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/pg_trunk/operations/enums/change.rb', line 30

def add?
  new_name.blank?
end

#invertObject



42
43
44
# File 'lib/pg_trunk/operations/enums/change.rb', line 42

def invert
  { name: new_name, new_name: name }
end

#optsObject



38
39
40
# File 'lib/pg_trunk/operations/enums/change.rb', line 38

def opts
  to_h.slice(:before, :after).compact
end

#rename?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/pg_trunk/operations/enums/change.rb', line 26

def rename?
  new_name.present?
end

#to_hObject



34
35
36
# File 'lib/pg_trunk/operations/enums/change.rb', line 34

def to_h
  attributes.compact.symbolize_keys
end

#to_sqlObject



46
47
48
49
50
51
52
53
# File 'lib/pg_trunk/operations/enums/change.rb', line 46

def to_sql
  return "RENAME VALUE '#{name}' TO '#{new_name}'" if new_name.present?

  sql = "ADD VALUE IF NOT EXISTS $value$#{name}$value$"
  sql << " BEFORE $value$#{before}$value$" if before.present?
  sql << " AFTER $value$#{after}$value$" if after.present?
  sql
end