Class: Sourced::Projector::StateStored

Inherits:
Sourced::Projector show all
Defined in:
lib/sourced/projector.rb

Overview

A StateStored projector fetches initial state from storage somewhere (DB, files, API) And then after reacting to events and updating state, it can save it back to the same or different storage.

Examples:


class CartListings < Sourced::Projector::StateStored
  # Fetch listing record from DB, or new one.
  def init_state(id)
    CartListing.find_or_initialize(id)
  end

  # Evolve listing record from events
  evolve Carts::ItemAdded do |listing, event|
    listing.total += event.payload.price
  end

  # Sync listing record back to DB
  sync do |listing, _, _|
    listing.save!
  end
end

Constant Summary

Constants included from Sync

Sync::CallableInterface

Constants included from Evolve

Evolve::NOOP_HANDLER, Evolve::PREFIX

Instance Attribute Summary

Attributes inherited from Sourced::Projector

#id, #seq, #state

Class Method Summary collapse

Methods inherited from Sourced::Projector

#handle_events, handled_events, #initialize, #inspect

Methods included from Consumer

#consumer, #consumer_info

Methods included from Sync

included, #run_sync_blocks

Methods included from Evolve

#evolve, included

Constructor Details

This class inherits a constructor from Sourced::Projector

Class Method Details

.handle_events(events) ⇒ Object



73
74
75
76
# File 'lib/sourced/projector.rb', line 73

def handle_events(events)
  instance = new(events.first.stream_id)
  instance.handle_events(events)
end