wordtree 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,9 +4,15 @@ require 'wordtree/book'
4
4
  module WordTree
5
5
  module DB
6
6
  class Librarian
7
- def initialize(rethinkdb_connection)
8
- @rdb = rethinkdb_connection
7
+ # @connection can be either a Hash or a RethinkDB::Connection object
8
+ # If a hash, it uses keys :host, :port, and :db
9
+ def initialize(connection)
9
10
  @r = RethinkDB::RQL.new
11
+ if connection.is_a? Hash
12
+ @rdb = @r.connect(connection)
13
+ else
14
+ @rdb = connection
15
+ end
10
16
  end
11
17
 
12
18
  def find(book_id)
@@ -6,10 +6,18 @@ require 'wordtree/archdown'
6
6
  module WordTree
7
7
  module Disk
8
8
  class Librarian
9
+ include Enumerable
10
+
9
11
  attr_reader :library
10
12
 
13
+ # @library can be either a string (the path of the library) or a
14
+ # WordTree::Disk::Library object
11
15
  def initialize(library)
12
- @library = library
16
+ if library.is_a? String
17
+ @library = WordTree::Disk::Library.new(library)
18
+ else
19
+ @library = library
20
+ end
13
21
  end
14
22
 
15
23
  def find(book_id)
@@ -21,6 +29,13 @@ module WordTree
21
29
  end
22
30
  end
23
31
 
32
+ def each(file_suffix_re=/\.(md|txt)$/, &block)
33
+ library.each(file_suffix_re) do |path|
34
+ retrieved = Preamble.load(path, :external_encoding => "utf-8")
35
+ yield Book.new(retrieved.metadata.merge("content" => retrieved.content))
36
+ end
37
+ end
38
+
24
39
  def save(book)
25
40
  library.mkdir(book.id)
26
41
  Preamble.new(book.metadata, book.content || "").save(library.path_to(book.id))
@@ -1,4 +1,5 @@
1
1
  require 'fileutils'
2
+ require 'find'
2
3
 
3
4
  require 'wordtree/archdown'
4
5
  require 'wordtree/disk/library_locator'
@@ -6,6 +7,7 @@ require 'wordtree/disk/library_locator'
6
7
  module WordTree
7
8
  module Disk
8
9
  class Library
10
+ include Enumerable
9
11
 
10
12
  FILE_TYPES = {
11
13
  :raw => "%s.md"
@@ -38,6 +40,23 @@ module WordTree
38
40
  def mkdir(book_id)
39
41
  FileUtils.mkdir_p(dir_of(book_id))
40
42
  end
43
+
44
+ # Breadth-first search of the directory structure, operating on each book
45
+ def each(file_suffix_re=/\.(md|txt)$/, &block)
46
+ Find.find(@root) do |path|
47
+ if FileTest.directory?(path)
48
+ if File.basename(path)[0] == ?.
49
+ # Don't look any further into this directory.
50
+ Find.prune
51
+ else
52
+ next
53
+ end
54
+ elsif path =~ file_suffix_re
55
+ yield path
56
+ end
57
+ end
58
+ end
59
+
41
60
  end
42
61
  end
43
62
  end
@@ -1,3 +1,3 @@
1
1
  module Wordtree
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  ---
2
+ archive_org_id: book
2
3
  year: 1800
3
4
  ---
4
5
  Book with content
@@ -0,0 +1,5 @@
1
+ ---
2
+ archive_org_id: other
3
+ year: 1801
4
+ ---
5
+ Other book with content
@@ -34,6 +34,13 @@ describe WordTree::Disk::Librarian do
34
34
  end
35
35
  end
36
36
 
37
+ describe "#each" do
38
+ it "iterates through each book" do
39
+ book_sizes = librarian.map{ |book| book.size_bytes }
40
+ expect(book_sizes).to contain_exactly(17, 23)
41
+ end
42
+ end
43
+
37
44
  it "saves to disk (yaml, content)" do
38
45
  tmp_root = Dir.mktmpdir
39
46
  tmp_library = WordTree::Disk::Library.new(tmp_root)
@@ -49,6 +56,7 @@ describe WordTree::Disk::Librarian do
49
56
  updated = Preamble.load(tmp_library.path_to("book"))
50
57
  expect(updated.metadata).to eq(
51
58
  :id => "book",
59
+ :archive_org_id => "book",
52
60
  :year => 1800,
53
61
  :source => "test",
54
62
  :simhash => 14921967289891934128,
@@ -20,4 +20,15 @@ describe WordTree::Disk::Library do
20
20
  library.mkdir(book_id)
21
21
  expect(File.exist?(library.dir_of(book_id))).to be_truthy
22
22
  end
23
+
24
+ context "with fixture library" do
25
+ let(:root) { fixture('library') }
26
+
27
+ describe "#each" do
28
+ it "iterates through each book" do
29
+ books = library.map.to_a
30
+ expect(books.size).to eq 2
31
+ end
32
+ end
33
+ end
23
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wordtree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-07-20 00:00:00.000000000 Z
12
+ date: 2014-09-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: virtus
@@ -260,6 +260,7 @@ files:
260
260
  - lib/wordtree/version.rb
261
261
  - spec/fixtures/cassettes/archive_org_download_book.yml
262
262
  - spec/fixtures/library/bo/ok/book/book.md
263
+ - spec/fixtures/library/ot/er/other/other.md
263
264
  - spec/spec_helper.rb
264
265
  - spec/wordtree/book_spec.rb
265
266
  - spec/wordtree/db/librarian_spec.rb
@@ -295,6 +296,7 @@ summary: Wordtree common library code
295
296
  test_files:
296
297
  - spec/fixtures/cassettes/archive_org_download_book.yml
297
298
  - spec/fixtures/library/bo/ok/book/book.md
299
+ - spec/fixtures/library/ot/er/other/other.md
298
300
  - spec/spec_helper.rb
299
301
  - spec/wordtree/book_spec.rb
300
302
  - spec/wordtree/db/librarian_spec.rb