Class: PgHaMigrations::LockMode

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/pg_ha_migrations/lock_mode.rb

Constant Summary collapse

MODE_CONFLICTS =
ActiveSupport::OrderedHash.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode) ⇒ LockMode

Returns a new instance of LockMode.



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pg_ha_migrations/lock_mode.rb', line 73

def initialize(mode)
  @mode = mode
    .to_s
    .underscore
    .delete_suffix("_lock")
    .to_sym

  if !MODE_CONFLICTS.keys.include?(@mode)
    raise ArgumentError, "Unrecognized lock mode #{@mode.inspect}. Valid modes: #{MODE_CONFLICTS.keys}"
  end
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



69
70
71
# File 'lib/pg_ha_migrations/lock_mode.rb', line 69

def mode
  @mode
end

Instance Method Details

#<=>(other) ⇒ Object



92
93
94
# File 'lib/pg_ha_migrations/lock_mode.rb', line 92

def <=>(other)
  MODE_CONFLICTS.keys.index(mode) <=> MODE_CONFLICTS.keys.index(other.mode)
end

#==(other) ⇒ Object



100
101
102
# File 'lib/pg_ha_migrations/lock_mode.rb', line 100

def ==(other)
  eql?(other)
end

#conflicts_with?(other) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/pg_ha_migrations/lock_mode.rb', line 108

def conflicts_with?(other)
  MODE_CONFLICTS[mode].include?(other.mode)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/pg_ha_migrations/lock_mode.rb', line 96

def eql?(other)
  other.is_a?(LockMode) && mode == other.mode
end

#hashObject



104
105
106
# File 'lib/pg_ha_migrations/lock_mode.rb', line 104

def hash
  mode.hash
end

#to_sqlObject



85
86
87
88
89
90
# File 'lib/pg_ha_migrations/lock_mode.rb', line 85

def to_sql
  mode
    .to_s
    .upcase
    .gsub("_", " ")
end