Class: Nestene::Actor::AutonStorage

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Celluloid::Notifications
Defined in:
lib/nestene/actor/auton_storage.rb

Instance Method Summary collapse

Constructor Details

#initialize(auton_id, storage) ⇒ AutonStorage

Returns a new instance of AutonStorage.



13
14
15
16
# File 'lib/nestene/actor/auton_storage.rb', line 13

def initialize(auton_id, storage)
  @auton_id = auton_id
  @storage = storage
end

Instance Method Details

#create(type) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/nestene/actor/auton_storage.rb', line 23

def create type
  state = AutonState.new
  state.type = type
  instance = Nestene::class_from_string(type).new
  state.serialized = instance.to_structure
  @storage.store(@auton_id,state.to_structure)
  publish('state_update', @auton_id, state)
rescue Exception => e
  abort e
end

#getObject



34
35
36
37
# File 'lib/nestene/actor/auton_storage.rb', line 34

def get
  structure = @storage.load(@auton_id)
  structure ? AutonState.from_structure(structure) : nil
end

#publish_initial_stateObject



18
19
20
21
# File 'lib/nestene/actor/auton_storage.rb', line 18

def publish_initial_state
  state = get
  publish('state_update', @auton_id, state) if state
end

#shutdownObject



39
40
41
42
43
44
# File 'lib/nestene/actor/auton_storage.rb', line 39

def shutdown
  Celluloid::Actor.delete("storage:%s" % @auton_id)
  @storage.delete(@auton_id)
  publish('state_update', @auton_id, nil)
  terminate
end

#update(&block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/nestene/actor/auton_storage.rb', line 46

def update &block
  before = @storage.load(@auton_id)
  state = AutonState.from_structure(@storage.load(@auton_id))
  result = block.call(state)
  after = state.to_structure
  @storage.store(@auton_id,after)
  publish('state_update', @auton_id, state) if before != after
  result
rescue Exception => e
  abort e
end