Class: Honyomi::Core

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#databaseObject (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, options = {})
  book, status = @database.add_from_pdf(filename, home_dir, options)

  return book, status
end

#db_dirObject



134
135
136
# File 'lib/honyomi/core.rb', line 134

def db_dir
  File.join(home_dir, 'db')
end

#db_pathObject



138
139
140
# File 'lib/honyomi/core.rb', line 138

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, options)
  opts = {}
  opts[:title]     = options[:title]                 if options[:title]
  opts[:author]    = options[:author]                if options[:author]
  opts[:url]       = options[:url]                   if options[:url]
  opts[:path]      = options[:path]                  if options[:path]
  opts[:timestamp] = Time.parse(options[:timestamp]) if options[:timestamp]

  if options[:strip] || options[: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 options[:strip]
  end

  @database.change_book(book_id, opts)
end

#image(id, options = {}) ⇒ Object



125
126
127
128
# File 'lib/honyomi/core.rb', line 125

def image(id, options = {})
  output_dir = @database.add_image(id, home_dir)
  puts "Generated images to '#{output_dir}'" if options[:verbose]
end

#init_databaseObject



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
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 = [], options = {})
  books = @database.books
  books = books.select("title:@#{options[:title]}").map {|record| record.key} if options[: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 options[: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.author}
url:       #{book.url}
path:      #{book.path}
pages:     #{book.page_num}
timestamp: #{book.timestamp}

EOF
      end
    end

    results
  end
end

#load_databaseObject



20
21
22
23
# File 'lib/honyomi/core.rb', line 20

def load_database
  Groonga::Database.open(db_path)
  @database = Database.new
end

#move(old_path, new_path) ⇒ Object



130
131
132
# File 'lib/honyomi/core.rb', line 130

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, options)
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(options)
  rack_options = {
    :environment => ENV['RACK_ENV'] || "development",
    :pid         => nil,
    :Port        => options[:port],
    :Host        => options[:host],
    :AccessLog   => [],
    :config      => "config.ru",
    # ----------------------------
    :server      => options[: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(rack_options)

  # Start Rack
  rack_server.start do
    unless options[:no_browser]
      Launchy.open("http://#{options[:host]}:#{options[:port]}")
    end
  end
end