Class: ZD::Migration
- Inherits:
-
Object
- Object
- ZD::Migration
- 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
-
#state ⇒ Object
Returns the value of attribute state.
Class Method Summary collapse
Instance Method Summary collapse
- #already?(in_state) ⇒ Boolean
- #check_state(state) ⇒ Object
- #HANDLE {|_self| ... } ⇒ Object
-
#initialize(initial_state) ⇒ Migration
constructor
A new instance of Migration.
- #migrate_to(new_state, silent = false) ⇒ Object
- #ONCE_COMPLETED ⇒ Object
- #ONCE_DESTROYED ⇒ Object
- #ONCE_PREPARED ⇒ Object
- #ONCE_SWITCHED ⇒ Object
- #print(v) ⇒ Object
- #puts(v = "\n") ⇒ Object
- #TEST_ONCE_SWITCHED ⇒ Object
- #UNTIL_COMPLETED ⇒ Object
- #UNTIL_PREPARED ⇒ Object
- #UNTIL_SWITCHED ⇒ Object
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
#state ⇒ Object
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!(={}) migration_name = ([:name] || name.split(/::/).last.underscore.to_sym) initial_state = ([:initial_state] || :destroyed) initial_state = initial_state.call if initial_state.respond_to?(:call) ZD.register_migration(migration_name, new(initial_state), Array([:depends_on])) end |
Instance Method Details
#already?(in_state) ⇒ 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
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
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_COMPLETED ⇒ Object
128 129 130 131 132 |
# File 'lib/zd.rb', line 128 def ONCE_COMPLETED if already?(:completed) yield end end |
#ONCE_DESTROYED ⇒ Object
134 135 136 137 138 |
# File 'lib/zd.rb', line 134 def ONCE_DESTROYED if already?(:destroyed) yield end end |
#ONCE_PREPARED ⇒ Object
104 105 106 107 108 |
# File 'lib/zd.rb', line 104 def ONCE_PREPARED if already?(:prepared) yield end end |
#ONCE_SWITCHED ⇒ Object
116 117 118 119 120 |
# File 'lib/zd.rb', line 116 def ONCE_SWITCHED if already?(:switched) yield end end |
#print(v) ⇒ Object
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_SWITCHED ⇒ Object
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_COMPLETED ⇒ Object
122 123 124 125 126 |
# File 'lib/zd.rb', line 122 def UNTIL_COMPLETED unless already?(:completed) yield end end |
#UNTIL_PREPARED ⇒ Object
98 99 100 101 102 |
# File 'lib/zd.rb', line 98 def UNTIL_PREPARED unless already?(:prepared) yield end end |
#UNTIL_SWITCHED ⇒ Object
110 111 112 113 114 |
# File 'lib/zd.rb', line 110 def UNTIL_SWITCHED unless already?(:switched) yield end end |