Class: Honyomi::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/honyomi/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDatabase

Returns a new instance of Database.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/honyomi/database.rb', line 13

def initialize
  @books = GrnMini::Array.new("Books")
  @pages = GrnMini::Hash.new("Pages")
  @bookmarks = GrnMini::Hash.new("Bookmarks")

  @books.setup_columns(path:     "",
                       title:    "",
                       author:   "",
                       url:      "",
                       page_num: 0,
                       timestamp: Time.new,
                       )
  @pages.setup_columns(book:    @books,
                       text:    "",
                       page_no: 0,
                       bookmark: @bookmarks,
                       )
  @bookmarks.setup_columns(page:    @pages,
                           comment: "",
                           timestamp: Time.new,
                           )
end

Instance Attribute Details

#bookmarksObject (readonly)

Returns the value of attribute bookmarks.



11
12
13
# File 'lib/honyomi/database.rb', line 11

def bookmarks
  @bookmarks
end

#booksObject (readonly)

Returns the value of attribute books.



9
10
11
# File 'lib/honyomi/database.rb', line 9

def books
  @books
end

#pagesObject (readonly)

Returns the value of attribute pages.



10
11
12
# File 'lib/honyomi/database.rb', line 10

def pages
  @pages
end

Instance Method Details

#add_book(path, pages, options = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/honyomi/database.rb', line 62

def add_book(path, pages, options = {})
  book = book_from_path(path)

  if book
    # Already exist
    opts = options.dup
    opts[:pages] = pages
    change_book(book.id, opts)
    return book, :update

  else
    # New book
    path = Util.filename_to_utf8(path)
    title = options[:title] || File.basename(path, File.extname(path))
    timestamp = options[:timestamp] || Time.now

    book = @books.add(path: path,
                      title: title,
                      author: "",
                      url:    "",
                      page_num: pages.size,
                      timestamp: timestamp,
                      )

    pages.each_with_index do |page, index|
      @pages["#{book.id}:#{index+1}"] = { book: book, text: page, page_no: index+1 }
    end

    return book, :add
  end
end

#add_bookmark(page) ⇒ Object



164
165
166
# File 'lib/honyomi/database.rb', line 164

def add_bookmark(page)
  @bookmarks["#{page.book.id}:#{page.page_no}"] = { page: page, timestamp: Time.now }
end

#add_from_pdf(filename, home_dir, options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/honyomi/database.rb', line 36

def add_from_pdf(filename, home_dir, options = {})
  if File.exist?(filename)
    filename = File.expand_path(filename)
    options = options.dup
    pages = Pdf.new(filename).pages
    pages = pages.map { |page| Util.strip_page(page) } if options[:strip]
    options[:timestamp] = File.stat(filename).mtime
    book, status = add_book(filename, pages, options)

    add_image(book.id, home_dir) if Util.exist_command?('pdftoppm')

    return book, status
  else
    nil
  end
end

#add_image(id, home_dir) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/honyomi/database.rb', line 53

def add_image(id, home_dir)
  output_dir = File.join(home_dir, "image", id.to_s)

  pdf = Pdf.new(books[id].path)
  pdf.generate_images(output_dir)

  output_dir
end

#book_from_path(path) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/honyomi/database.rb', line 154

def book_from_path(path)
  r = @books.select("path:\"#{path}\"").first

  if r
    r.key
  else
    nil
  end
end

#book_pages(book_id) ⇒ Object



150
151
152
# File 'lib/honyomi/database.rb', line 150

def book_pages(book_id)
  @pages.select("book._id:\"#{book_id}\"").sort(["page_no"])
end

#bookmark?(page) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/honyomi/database.rb', line 179

def bookmark?(page)
  @bookmarks["#{page.book.id}:#{page.page_no}"]
end

#bookmark_from_page(page) ⇒ Object



183
184
185
# File 'lib/honyomi/database.rb', line 183

def bookmark_from_page(page)
  @bookmarks["#{page.book.id}:#{page.page_no}"]
end

#books_bookmark(book) ⇒ Object



187
188
189
# File 'lib/honyomi/database.rb', line 187

def books_bookmark(book)
  @bookmarks.select { |record| record.page.book == book }
end

#change_book(book_id, options = {}) ⇒ Object

Raises:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/honyomi/database.rb', line 94

def change_book(book_id, options = {})
  book = @books[book_id]
  raise HonyomiError, "Invalid book id: #{book_id}" unless book.valid_id?
  
  book.title = options[:title] if options[:title]
  book.author = options[:author] if options[:author]
  book.url = options[:url] if options[:url]
  book.path  = options[:path]  if options[:path]
  book.timestamp = options[:timestamp] if options[:timestamp]

  if options[:pages]
    pages = options[:pages]
    book.page_num = pages.size

    @pages.delete do |page|
      page.book == book
    end
    
    pages.each_with_index do |page, index|
      @pages["#{book.id}:#{index+1}"] = { book: book, text: page, page_no: index+1 }
    end
  end
end

#delete_book(book_id) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/honyomi/database.rb', line 118

def delete_book(book_id)
  book = @books[book_id]
  
  @pages.delete do |page|
    page.book == book
  end

  book.delete
end

#delete_bookmark(page) ⇒ Object



168
169
170
# File 'lib/honyomi/database.rb', line 168

def delete_bookmark(page)
  @bookmarks.delete { |bookmark| bookmark.page == page }
end

#search(query, options = {}) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/honyomi/database.rb', line 128

def search(query, options = {})
  match_pages = @pages.select(query.page_query, default_column: "text")

  if options[:cli]
    snippet = match_pages.expression.snippet([ ['<<',
                                                '>>'] ],
                                             {normalize: true})
  else
    snippet = match_pages.expression.snippet([["<span class=\"highlight\">", "</span>"]], {html_escape: true, normalize: true, max_results: 5})
  end

  match_bookmarks = @bookmarks.select do |record|
    record.match(query.bookmark_query) do |target|
      target.comment * 10
    end
  end

  group_by_page = match_bookmarks.group("page")

  return group_by_page.union!(match_pages), snippet
end

#update_bookmark_comment(id, page_no, comment) ⇒ Object



172
173
174
175
176
177
# File 'lib/honyomi/database.rb', line 172

def update_bookmark_comment(id, page_no, comment)
  bm = @bookmarks["#{id}:#{page_no}"]
  bm.comment = comment
  bm.timestamp = Time.now
  bm
end