Class: Phronomy::Persistence::InMemory::Agents
- Inherits:
-
Object
- Object
- Phronomy::Persistence::InMemory::Agents
- Defined in:
- lib/phronomy/persistence/in_memory.rb
Instance Method Summary collapse
- #create(root) ⇒ Object
- #delete(agent_id) ⇒ Object
-
#initialize(owner) ⇒ Agents
constructor
A new instance of Agents.
- #load(agent_id) ⇒ Object
- #save(agent_id, expected_revision:, root:) ⇒ Object
Constructor Details
#initialize(owner) ⇒ Agents
Returns a new instance of Agents.
49 |
# File 'lib/phronomy/persistence/in_memory.rb', line 49 def initialize(owner) = @owner = owner |
Instance Method Details
#create(root) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/phronomy/persistence/in_memory.rb', line 51 def create(root) @owner.synchronize do key = root.agent_id.to_s raise ConflictError, "agent_id must not be empty" if key.empty? raise ConflictError, "agent already exists: #{key}" if @owner.state[:agents].key?(key) @owner.state[:agents][key] = root end root end |
#delete(agent_id) ⇒ Object
87 88 89 |
# File 'lib/phronomy/persistence/in_memory.rb', line 87 def delete(agent_id) @owner.synchronize { @owner.state[:agents].delete(agent_id.to_s) } end |
#load(agent_id) ⇒ Object
61 62 63 64 65 |
# File 'lib/phronomy/persistence/in_memory.rb', line 61 def load(agent_id) @owner.synchronize do @owner.state[:agents].fetch(agent_id.to_s) { raise NotFoundError, "agent not found: #{agent_id}" } end end |
#save(agent_id, expected_revision:, root:) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/phronomy/persistence/in_memory.rb', line 67 def save(agent_id, expected_revision:, root:) @owner.synchronize do current = load(agent_id) unless current.agent_revision == expected_revision raise ConflictError, "agent revision conflict: expected #{expected_revision}, actual #{current.agent_revision}" end unless root.agent_id.to_s == agent_id.to_s raise ConflictError, "Agent root identity mismatch: #{root.agent_id} != #{agent_id}" end unless root.agent_revision == expected_revision + 1 raise ConflictError, "agent save must advance revision exactly once: " \ "expected #{expected_revision + 1}, got #{root.agent_revision}" end @owner.state[:agents][agent_id.to_s] = root end root end |