Class: WordTree::BookList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/wordtree/book_list.rb

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ BookList

can be initialized from the following sources:

  • a WordTree::Disk::Library object

  • an open File object (containing a list of files or paths to books)

  • a String directory (presumed to be the library on disk)

  • a String file (containing a list of files or paths to books)



10
11
12
13
# File 'lib/wordtree/book_list.rb', line 10

def initialize(source)
  @source = source
  @iterable = iterable_from_source(source)
end

Instance Method Details

#each(&block) ⇒ Object



34
35
36
# File 'lib/wordtree/book_list.rb', line 34

def each(&block)
  @iterable.each(&block)
end

#iterable_from_source(source) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wordtree/book_list.rb', line 15

def iterable_from_source(source)
  case source
  when WordTree::Disk::Library then
    source
  when File then
    source.read.split("\n").tap do |file|
      file.close
    end
  when String then
    if File.directory?(source)
      WordTree::Disk::Library.new(source)
    elsif File.exist?(source)
      IO.read(source).split("\n")
    else
      raise Errno::ENOENT, "Unable to find source for BookList, #{source.inspect}"
    end
  end
end