Class: CoopAl::Library

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/coop_al/library.rb

Overview

Library

Instance Method Summary collapse

Constructor Details

#initializeLibrary

Returns a new instance of Library.



10
11
12
# File 'lib/coop_al/library.rb', line 10

def initialize
  @adventures = {}
end

Instance Method Details

#add_adventure(adventure) ⇒ Object

Raises:



18
19
20
21
# File 'lib/coop_al/library.rb', line 18

def add_adventure(adventure)
  raise Exception, 'Duplicate adventure' if @adventures.key?(adventure.name)
  @adventures[adventure.name] = adventure
end

#adventure(name) ⇒ Object



27
28
29
# File 'lib/coop_al/library.rb', line 27

def adventure(name)
  @adventures[name]
end

#adventure?(name) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/coop_al/library.rb', line 23

def adventure?(name)
  @adventures.key?(name)
end

#all_entriesObject



42
43
44
# File 'lib/coop_al/library.rb', line 42

def all_entries
  @adventures.values.inject([]) { |a, e| a + e.all_entries }
end

#available_paths_from(path) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/coop_al/library.rb', line 46

def available_paths_from(path)
  return all_entries if path.root?
  current_chapter = resolve(path)
  paths = current_chapter.links
  paths << Path.root if current_chapter.links_to_downtime?
  paths
end

#empty?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/coop_al/library.rb', line 14

def empty?
  @adventures.empty?
end

#path?(path) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



31
32
33
34
35
# File 'lib/coop_al/library.rb', line 31

def path?(path)
  raise Exception, "Cannot resolve relative path (#{path})" if path.relative?
  return false unless @adventures.key?(path.adventure)
  @adventures[path.adventure].chapter?(path.chapter)
end

#resolve(path) ⇒ Object

Raises:



37
38
39
40
# File 'lib/coop_al/library.rb', line 37

def resolve(path)
  raise Exception, "Adventure (#{path.adventure}) not found" unless @adventures.key?(path.adventure)
  @adventures[path.adventure].chapter(path.chapter)
end