Class: ZD::Migration

Inherits:
Object
  • Object
show all
Defined in:
lib/zd.rb

Constant Summary collapse

STATES =
[:unrun, :prepared, :migrated, :switched, :completed, :destroyed]
TRANSITIONS =
{
  unrun: :prepare,
  prepared: :migrate,
  migrated: :switch,
  switched: :complete,
  completed: :destroy,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_state) ⇒ Migration

Returns a new instance of Migration.



63
64
65
# File 'lib/zd.rb', line 63

def initialize(initial_state)
  self.state = initial_state
end

Instance Attribute Details

#stateObject

Returns the value of attribute state.



62
63
64
# File 'lib/zd.rb', line 62

def state
  @state
end

Class Method Details

.register!(options = {}) ⇒ Object



46
47
48
49
50
51
# File 'lib/zd.rb', line 46

def self.register!(options={})
  migration_name = (options[:name] || name.split(/::/).last.underscore.to_sym)
  initial_state = (options[:initial_state] || :destroyed)
  initial_state = initial_state.call if initial_state.respond_to?(:call)
  ZD.register_migration(migration_name, new(initial_state), Array(options[:depends_on]))
end

Instance Method Details

#already?(in_state) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/zd.rb', line 90

def already?(in_state)
  STATES.index(@state) >= STATES.index(in_state)
end

#check_state(state) ⇒ Object

Raises:

  • (ArgumentError)


72
73
74
# File 'lib/zd.rb', line 72

def check_state(state)
  raise ArgumentError, "Unknown state #{state}" unless STATES.include?(state)
end

#HANDLE {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (ZD::Migration)

    the object that the method was called on



94
95
96
# File 'lib/zd.rb', line 94

def HANDLE
  yield self
end

#migrate_to(new_state, silent = false) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/zd.rb', line 76

def migrate_to(new_state, silent=false)
  @silent = silent
  check_state(new_state)
  currently = STATES.index(state)
  target = STATES.index(new_state)
  to_run = STATES[(currently)..(target-1)]
  to_run.each do |s|
    action_name = TRANSITIONS[s]
    puts "Transitioning from #{s} via #{action_name}..."
    send(action_name) if action_name && respond_to?(action_name)
    self.state = s
  end
end

#ONCE_COMPLETEDObject



128
129
130
131
132
# File 'lib/zd.rb', line 128

def ONCE_COMPLETED
  if already?(:completed)
    yield
  end
end

#ONCE_DESTROYEDObject



134
135
136
137
138
# File 'lib/zd.rb', line 134

def ONCE_DESTROYED
  if already?(:destroyed)
    yield
  end
end

#ONCE_PREPAREDObject



104
105
106
107
108
# File 'lib/zd.rb', line 104

def ONCE_PREPARED
  if already?(:prepared)
    yield
  end
end

#ONCE_SWITCHEDObject



116
117
118
119
120
# File 'lib/zd.rb', line 116

def ONCE_SWITCHED
  if already?(:switched)
    yield
  end
end


152
153
154
# File 'lib/zd.rb', line 152

def print(v)
  super(v) unless @silent
end

#puts(v = "\n") ⇒ Object



148
149
150
# File 'lib/zd.rb', line 148

def puts(v="\n")
  super(v) unless @silent
end

#TEST_ONCE_SWITCHEDObject



140
141
142
143
144
145
146
# File 'lib/zd.rb', line 140

def TEST_ONCE_SWITCHED
  old_state = state
  migrate_to(:switched, true)
  yield
ensure
  self.state = old_state
end

#UNTIL_COMPLETEDObject



122
123
124
125
126
# File 'lib/zd.rb', line 122

def UNTIL_COMPLETED
  unless already?(:completed)
    yield
  end
end

#UNTIL_PREPAREDObject



98
99
100
101
102
# File 'lib/zd.rb', line 98

def UNTIL_PREPARED
  unless already?(:prepared)
    yield
  end
end

#UNTIL_SWITCHEDObject



110
111
112
113
114
# File 'lib/zd.rb', line 110

def UNTIL_SWITCHED
  unless already?(:switched)
    yield
  end
end