Skip to content

Instantly share code, notes, and snippets.

@nickstenning
Created July 24, 2008 22:04
Show Gist options
  • Save nickstenning/2319 to your computer and use it in GitHub Desktop.
Save nickstenning/2319 to your computer and use it in GitHub Desktop.
class Manager < ActiveRecord::Base
belongs_to :demo
serialize :state
serialize :cache
def after_initialize
@observers = []
end
def kind
read_attribute(:kind) or write_attribute(:kind, "example")
end
def before_update
logger.info "setting state to #{state.to_yaml}"
write_attribute(:state, state)
logger.info "setting state to #{cache.to_yaml}"
write_attribute(:cache, cache)
end
def object
@object ||= new_object
end
def state
object.state
end
def cache
object.cache
end
def completed?(command)
case command
when :rollout
object.position == object.tasks.length
when :rollback
object.position == 0
end
end
def rollout
object.rollout!
end
def rollback
object.rollback!
end
def add_observer( obj )
@observers << obj
object.add_observer( obj )
end
def observed_by?( obj )
@observers.include? obj
end
def task_count
object.tasks.length
end
def method_missing(meth, *args)
if object.respond_to?(meth)
object.send(meth, *args)
else
super(meth, *args)
end
end
private
def new_object
require File.join(RAILS_ROOT, 'lib', 'outback', "#{kind}.rb")
@object = Outback.const_get(kind.classify).create
if s = read_attribute(:state)
logger.info "Going to update state with #{s.to_yaml}"
@object.restore_state(s)
end
if c = read_attribute(:cache)
logger.info "Going to update cache with #{c.to_yaml}"
@object.cache = c
end
@object
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment