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
17
18
# File 'lib/nestene/actor/auton_storage.rb', line 13

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

Instance Method Details

#create(type) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/nestene/actor/auton_storage.rb', line 20

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



31
32
33
34
# File 'lib/nestene/actor/auton_storage.rb', line 31

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

#update(&block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/nestene/actor/auton_storage.rb', line 36

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