Class: Honyomi::Core
- Inherits:
-
Object
- Object
- Honyomi::Core
- Defined in:
- lib/honyomi/core.rb
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Returns the value of attribute database.
Instance Method Summary collapse
- #add(filename, options = {}) ⇒ Object
- #db_dir ⇒ Object
- #db_path ⇒ Object
- #edit(book_id, options) ⇒ Object
- #image(id, options = {}) ⇒ Object
- #init_database ⇒ Object
-
#initialize(opts = {}) ⇒ Core
constructor
A new instance of Core.
- #list(args = [], options = {}) ⇒ Object
- #load_database ⇒ Object
- #move(old_path, new_path) ⇒ Object
- #remove(book_id) ⇒ Object
- #search(query) ⇒ Object
- #update(book_id, options) ⇒ Object
- #web(options) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Core
Returns a new instance of Core.
10 11 12 |
# File 'lib/honyomi/core.rb', line 10 def initialize(opts = {}) @opts = opts end |
Instance Attribute Details
#database ⇒ Object (readonly)
Returns the value of attribute database.
8 9 10 |
# File 'lib/honyomi/core.rb', line 8 def database @database end |
Instance Method Details
#add(filename, options = {}) ⇒ Object
25 26 27 28 29 |
# File 'lib/honyomi/core.rb', line 25 def add(filename, = {}) book, status = @database.add_from_pdf(filename, ) return book, status end |
#db_dir ⇒ Object
155 156 157 |
# File 'lib/honyomi/core.rb', line 155 def db_dir File.join(home_dir, 'db') end |
#db_path ⇒ Object
159 160 161 |
# File 'lib/honyomi/core.rb', line 159 def db_path File.join(db_dir, 'honyomi.db') end |
#edit(book_id, options) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/honyomi/core.rb', line 34 def edit(book_id, ) opts = {} opts[:title] = [:title] if [:title] opts[:author] = [:author] if [:author] opts[:url] = [:url] if [:url] opts[:path] = [:path] if [:path] opts[:timestamp] = Time.parse([:timestamp]) if [:timestamp] if [:strip] || [:no_strip] filename = @database.books[book_id].path opts[:pages] = Pdf.new(filename).pages opts[:pages] = opts[:pages].map { |page| Util.strip_page(page) } if [:strip] end @database.change_book(book_id, opts) end |
#image(id, options = {}) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/honyomi/core.rb', line 125 def image(id, = {}) output_dir = @database.image_dir(id) unless [:delete] if [:verbose] unless File.exist?(output_dir) puts "Generate images to '#{output_dir}'" else puts "Regenerate images to '#{output_dir}'" end end @database.add_image(id) else if [:verbose] if File.exist?(output_dir) puts "Delete images from '#{output_dir}'" else puts "Already deleted '#{output_dir}'" end end @database.delete_image(id) end end |
#init_database ⇒ Object
14 15 16 17 18 |
# File 'lib/honyomi/core.rb', line 14 def init_database FileUtils.mkdir_p(db_dir) Groonga::Database.create(path: db_path) @database = Database.new(home_dir) end |
#list(args = [], options = {}) ⇒ Object
59 60 61 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 93 94 95 96 97 |
# File 'lib/honyomi/core.rb', line 59 def list(args = [], = {}) books = @database.books books = books.select("title:@#{[:title]}").map {|record| record.key} if [:title] if args.empty? id_length = books.max { |book| book.id.to_s.length } id_length = id_length ? id_length.id.to_s.length : 0 if [:path] books.map do |book| "#{book.id.to_s.rjust(id_length)} #{book.path}" end else books.map do |book| # "#{book.id} #{book.title} (#{book.page_num} pages) #{book.path}" "#{book.id.to_s.rjust(id_length)} #{book.title} (#{book.page_num} pages)" end end else results = [] books.each do |book| if args.include?(book.id) results << <<EOF id: #{book.id.to_s} title: #{book.title} author: #{book.} url: #{book.url} path: #{book.path} pages: #{book.page_num} timestamp: #{book.} EOF end end results end end |
#load_database ⇒ Object
20 21 22 23 |
# File 'lib/honyomi/core.rb', line 20 def load_database Groonga::Database.open(db_path) @database = Database.new(home_dir) end |
#move(old_path, new_path) ⇒ Object
151 152 153 |
# File 'lib/honyomi/core.rb', line 151 def move(old_path, new_path) @database.move(old_path, new_path) end |
#remove(book_id) ⇒ Object
51 52 53 |
# File 'lib/honyomi/core.rb', line 51 def remove(book_id) @database.delete_book(book_id) end |
#search(query) ⇒ Object
55 56 57 |
# File 'lib/honyomi/core.rb', line 55 def search(query) @database.search(Query.new(query), cli: true) end |
#update(book_id, options) ⇒ Object
31 32 |
# File 'lib/honyomi/core.rb', line 31 def update(book_id, ) end |
#web(options) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/honyomi/core.rb', line 99 def web() = { :environment => ENV['RACK_ENV'] || "development", :pid => nil, :Port => [:port], :Host => [:host], :AccessLog => [], :config => "config.ru", # ---------------------------- :server => [:server], } # Move to the location of the server script FileUtils.cd(File.join(File.dirname(__FILE__), 'web')) # Create Rack Server rack_server = Rack::Server.new() # Start Rack rack_server.start do unless [:no_browser] Launchy.open("http://#{[:host]}:#{[:port]}") end end end |