Class: Frasier::Library

Inherits:
Object
  • Object
show all
Defined in:
lib/frasier/library.rb

Constant Summary collapse

LIBRARY_PATH =
"#{ENV['HOME']}/.config/frasier"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLibrary

Returns a new instance of Library.



7
8
9
10
11
# File 'lib/frasier/library.rb', line 7

def initialize
  @books = []
  setup
  @books = find_books!
end

Instance Attribute Details

#booksObject

Returns the value of attribute books.



5
6
7
# File 'lib/frasier/library.rb', line 5

def books
  @books
end

Class Method Details

.library_pathObject



36
37
38
# File 'lib/frasier/library.rb', line 36

def self.library_path
  LIBRARY_PATH
end

Instance Method Details

#book_with_name(name) ⇒ Object



19
20
21
22
23
24
# File 'lib/frasier/library.rb', line 19

def book_with_name(name)
  name = Regexp.new(name)
  books.detect do |book|
    book.title =~ name || book.path =~ name
  end
end

#find_books!Object



30
31
32
33
34
# File 'lib/frasier/library.rb', line 30

def find_books!
  Dir.glob(File.join(self.class.library_path, "*")).map do |path|
    Book.new(path)
  end
end

#random_bookObject



26
27
28
# File 'lib/frasier/library.rb', line 26

def random_book
  books.sample
end

#setupObject



13
14
15
16
17
# File 'lib/frasier/library.rb', line 13

def setup
  return if File.exist?(self.class.library_path)

  FileUtils.mkdir_p(self.class.library_path)
end