Class: Frasier::Library
- Inherits:
-
Object
- Object
- Frasier::Library
- Defined in:
- lib/frasier/library.rb
Constant Summary collapse
- LIBRARY_PATH =
"#{ENV['HOME']}/.config/frasier"
Instance Attribute Summary collapse
-
#books ⇒ Object
Returns the value of attribute books.
Class Method Summary collapse
Instance Method Summary collapse
- #book_with_name(name) ⇒ Object
- #find_books! ⇒ Object
-
#initialize ⇒ Library
constructor
A new instance of Library.
- #random_book ⇒ Object
- #setup ⇒ Object
Constructor Details
#initialize ⇒ Library
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
#books ⇒ Object
Returns the value of attribute books.
5 6 7 |
# File 'lib/frasier/library.rb', line 5 def books @books end |
Class Method Details
.library_path ⇒ Object
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_book ⇒ Object
26 27 28 |
# File 'lib/frasier/library.rb', line 26 def random_book books.sample end |
#setup ⇒ Object
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 |