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]

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)



20
21
22
# File 'lib/wordtree/disk/library_locator.rb', line 20

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

.identity(id) ⇒ Object

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



41
42
43
# File 'lib/wordtree/disk/library_locator.rb', line 41

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

Instance Method Details

#firstObject



24
25
26
# File 'lib/wordtree/disk/library_locator.rb', line 24

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

#lastObject



28
29
30
# File 'lib/wordtree/disk/library_locator.rb', line 28

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”



36
37
38
# File 'lib/wordtree/disk/library_locator.rb', line 36

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