Class: CoopAl::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/coop_al/path.rb

Overview

Path

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adventure, chapter) ⇒ Path

Returns a new instance of Path.



8
9
10
11
# File 'lib/coop_al/path.rb', line 8

def initialize(adventure, chapter)
  @adventure = adventure
  @chapter = chapter
end

Instance Attribute Details

#adventureObject (readonly)

Returns the value of attribute adventure.



6
7
8
# File 'lib/coop_al/path.rb', line 6

def adventure
  @adventure
end

#chapterObject (readonly)

Returns the value of attribute chapter.



6
7
8
# File 'lib/coop_al/path.rb', line 6

def chapter
  @chapter
end

Class Method Details

.absolute(adventure, chapter) ⇒ Object



25
26
27
# File 'lib/coop_al/path.rb', line 25

def self.absolute(adventure, chapter)
  Path.new(adventure, chapter)
end

.parse(path) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/coop_al/path.rb', line 37

def self.parse(path)
  tokens = path.split('/').map(&:to_sym)
  raise "Invalid path #{path}" if tokens.count > 2
  return absolute(tokens[0], tokens[1]) if tokens.count == 2
  return root if tokens[0] == :downtime
  relative(tokens[0])
end

.relative(chapter) ⇒ Object



29
30
31
# File 'lib/coop_al/path.rb', line 29

def self.relative(chapter)
  Path.new(nil, chapter)
end

.rootObject



33
34
35
# File 'lib/coop_al/path.rb', line 33

def self.root
  Path.new(nil, nil)
end

Instance Method Details

#+(other) ⇒ Object



49
50
51
52
53
54
# File 'lib/coop_al/path.rb', line 49

def +(other)
  local_path = other.is_a?(Path) ? other : Path.parse(other)
  return local_path if local_path.absolute?
  raise 'Cannot add two relative paths' if relative?
  Path.absolute(@adventure, other.chapter)
end

#==(other) ⇒ Object



45
46
47
# File 'lib/coop_al/path.rb', line 45

def ==(other)
  to_s == other.to_s
end

#absolute?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/coop_al/path.rb', line 21

def absolute?
  !relative?
end

#adventure_sObject



60
61
62
63
# File 'lib/coop_al/path.rb', line 60

def adventure_s
  return @adventure.to_s + '/' if absolute?
  ''
end

#relative?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/coop_al/path.rb', line 17

def relative?
  @adventure.nil? && !@chapter.nil?
end

#root?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/coop_al/path.rb', line 13

def root?
  @adventure.nil? && @chapter.nil?
end

#to_sObject



56
57
58
# File 'lib/coop_al/path.rb', line 56

def to_s
  adventure_s + @chapter.to_s
end