Class: WordTree::Disk::LibraryLocator

Inherits:
Object
  • Object
show all
Defined in:
lib/wordtree/disk/library_locator.rb

Overview

A class that converts from a book ID to a location within the library, e.g.

“firstbooknapole00gruagoog”

becomes

“fi/og/firstbooknapole00gruagoog/”

or, in context of the full path:
/data/library/

“fi/og/firstbooknapole00gruagoog/” [firstbooknapole00gruagoog.md]

Constant Summary collapse

NotPath =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ LibraryLocator

Construct a LibraryLocator from a string (book ID)



22
23
24
# File 'lib/wordtree/disk/library_locator.rb', line 22

def initialize(id)
  @id = id
end

Instance Attribute Details

#idObject (readonly)

The book ID to locate



17
18
19
# File 'lib/wordtree/disk/library_locator.rb', line 17

def id
  @id
end

Class Method Details

.id_from_path(path) ⇒ Object

Raises:



47
48
49
50
# File 'lib/wordtree/disk/library_locator.rb', line 47

def self.id_from_path(path)
  raise NotPath, "not a path" if path.nil? or !path.is_a?(String)
  File.basename(path).sub(/\.[^\.]+$/, '')
end

.identity(id) ⇒ Object

Constructor that is as willing to use a String as it is a LibraryLocator



43
44
45
# File 'lib/wordtree/disk/library_locator.rb', line 43

def self.identity(id)
  id.is_a?(LibraryLocator) ? id : new(id)
end

Instance Method Details

#firstObject



26
27
28
# File 'lib/wordtree/disk/library_locator.rb', line 26

def first
  @id[0..1].downcase
end

#lastObject



30
31
32
# File 'lib/wordtree/disk/library_locator.rb', line 30

def last
  @id[-2..-1].downcase
end

#relpathObject

Returns a “relative” path to be joined to the library root, e.g. if the identifier is “firstbooknapole00gruagoog”, then relpath should return “fi/og/firstbooknapole00gruagoog”, i.e. probably later to become something like “/data/library/fi/og/firstbooknapole00gruagoog”



38
39
40
# File 'lib/wordtree/disk/library_locator.rb', line 38

def relpath
  File.join(first, last, @id)
end