Class: WordTree::Disk::Library
- Inherits:
-
Object
- Object
- WordTree::Disk::Library
- Includes:
- Enumerable
- Defined in:
- lib/wordtree/disk/library.rb
Constant Summary collapse
- FILE_TYPES =
{ :raw => "%{id}.md", :ngrams => "%{id}.%{n}grams.json" }
Instance Attribute Summary collapse
-
#root ⇒ Object
readonly
The file path to the root of the library directory, e.g.
Instance Method Summary collapse
-
#dir_of(book_id) ⇒ Object
returns the full path of a book’s subdirectory within the library Accepts either a String or a LibraryLocator object.
-
#each(file_suffix_re = /\.(md|txt)$/, &block) ⇒ Object
Breadth-first search of the directory structure, operating on each book.
- #file_type(book_id, type = :raw, opts = {}) ⇒ Object
-
#initialize(root) ⇒ Library
constructor
A new instance of Library.
-
#mkdir(book_id) ⇒ Object
Create all subdirs up to the location where a book is stored Accepts either a String or a LibraryLocator object.
- #path_to(book_id, type = :raw, opts = {}) ⇒ Object
Constructor Details
Instance Attribute Details
#root ⇒ Object (readonly)
The file path to the root of the library directory, e.g. /data/library
18 19 20 |
# File 'lib/wordtree/disk/library.rb', line 18 def root @root end |
Instance Method Details
#dir_of(book_id) ⇒ Object
returns the full path of a book’s subdirectory within the library Accepts either a String or a LibraryLocator object
26 27 28 |
# File 'lib/wordtree/disk/library.rb', line 26 def dir_of(book_id) File.(LibraryLocator.identity(book_id).relpath, root) end |
#each(file_suffix_re = /\.(md|txt)$/, &block) ⇒ Object
Breadth-first search of the directory structure, operating on each book
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/wordtree/disk/library.rb', line 48 def each(file_suffix_re=/\.(md|txt)$/, &block) Find.find(@root) do |path| if FileTest.directory?(path) if File.basename(path)[0] == ?. # Don't look any further into this directory. Find.prune else next end elsif path =~ file_suffix_re yield path, LibraryLocator.id_from_path(path) end end end |
#file_type(book_id, type = :raw, opts = {}) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/wordtree/disk/library.rb', line 34 def file_type(book_id, type=:raw, opts={}) locator = LibraryLocator.identity(book_id) template = FILE_TYPES[type] raise ArgumentError, "unable to find file type template #{type.inspect}" if template.nil? template % {:id => locator.id}.merge(opts) end |
#mkdir(book_id) ⇒ Object
Create all subdirs up to the location where a book is stored Accepts either a String or a LibraryLocator object
43 44 45 |
# File 'lib/wordtree/disk/library.rb', line 43 def mkdir(book_id) FileUtils.mkdir_p(dir_of(book_id)) end |
#path_to(book_id, type = :raw, opts = {}) ⇒ Object
30 31 32 |
# File 'lib/wordtree/disk/library.rb', line 30 def path_to(book_id, type=:raw, opts={}) File.join(dir_of(book_id), file_type(book_id, type, opts)) end |