Sha256: 11c3ff3aa242a1ddbedb22dbfd594bffb82db6ca46e24d65e30dff994adb1485

Contents?: true

Size: 1.25 KB

Versions: 9

Compression:

Stored size: 1.25 KB

Contents

require 'celluloid'

module Nestene
  module Actor
    class AutonStorage
      include Celluloid
      include Celluloid::Notifications

      exclusive :create, :get, :update

      execute_block_on_receiver :update

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

      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

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

      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

    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
nestene-0.1.8 lib/nestene/actor/auton_storage.rb
nestene-0.1.7 lib/nestene/actor/auton_storage.rb
nestene-0.1.6 lib/nestene/actor/auton_storage.rb
nestene-0.1.5 lib/nestene/actor/auton_storage.rb
nestene-0.1.4 lib/nestene/actor/auton_storage.rb
nestene-0.1.3 lib/nestene/actor/auton_storage.rb
nestene-0.1.2 lib/nestene/actor/auton_storage.rb
nestene-0.1.1 lib/nestene/actor/auton_storage.rb
nestene-0.1.0 lib/nestene/actor/auton_storage.rb