Class: Alchemist::WorldHistory

Inherits:
Object
  • Object
show all
Defined in:
lib/alchemist-server/world_history.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event, prior_history) ⇒ WorldHistory

Returns a new instance of WorldHistory.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/alchemist-server/world_history.rb', line 5

def initialize(event, prior_history)
  @event = event
  @prior_history = prior_history

  @world =
    if @prior_history.nil?
      World.genesis
    else
      outcome = @event.happen @prior_history
      outcome.try(:new_world) || prior_world
    end
end

Instance Attribute Details

#worldObject (readonly)

Returns the value of attribute world.



3
4
5
# File 'lib/alchemist-server/world_history.rb', line 3

def world
  @world
end

Class Method Details

.genesisObject



40
41
42
# File 'lib/alchemist-server/world_history.rb', line 40

def self.genesis
  new Event.genesis, nil
end

.parse(string) ⇒ Object



30
31
32
33
34
# File 'lib/alchemist-server/world_history.rb', line 30

def self.parse(string)
  split_into_chronological_events(string).reduce(nil) do |history, event_string|
    new Event.parse(event_string), history
  end
end

.split_into_chronological_events(string) ⇒ Object



36
37
38
# File 'lib/alchemist-server/world_history.rb', line 36

def self.split_into_chronological_events(string)
  string.split("\n\n").reject { |l| l =~ /^\s*$/ }
end

Instance Method Details

#prior_worldObject



18
19
20
# File 'lib/alchemist-server/world_history.rb', line 18

def prior_world
  @prior_history.try :world
end

#to_sObject



22
23
24
25
26
27
28
# File 'lib/alchemist-server/world_history.rb', line 22

def to_s
  <<-str.strip
#{@prior_history}

#{@event}
  str
end