Class: WordTree::LibraryLocator

Inherits:
Object
  • Object
show all
Defined in:
lib/wordtree/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)



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

def initialize(id)
  @id = id
end

Instance Attribute Details

#idObject (readonly)

The book ID to locate



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

def id
  @id
end

Class Method Details

.identity(id) ⇒ Object

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



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

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

Instance Method Details

#firstObject



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

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

#lastObject



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

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”



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

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