Class: Habari2md::Exporter
- Inherits:
-
Object
- Object
- Habari2md::Exporter
- Includes:
- Celluloid, Celluloid::Logger
- Defined in:
- lib/habari2md.rb
Overview
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
Instance Method Summary collapse
- #export_posts(directory) ⇒ Object
-
#initialize(opts = {}) ⇒ Exporter
constructor
A new instance of Exporter.
-
#post_exported(post_id) ⇒ Object
Called by PostExport when an export operation has finished.
- #posts ⇒ Object
- #user(id) ⇒ Hash
Constructor Details
#initialize(opts = {}) ⇒ Exporter
Returns a new instance of Exporter.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/habari2md.rb', line 58 def initialize(opts = {}) @db = Sequel.connect(db_uri opts) @counter = 0 @halfway = 0 # Cache users @users = @db[:users].all.inject({}) do |cache, user| cache.merge!([user[:id]] => user) end end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
54 55 56 |
# File 'lib/habari2md.rb', line 54 def db @db end |
Instance Method Details
#export_posts(directory) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/habari2md.rb', line 78 def export_posts(directory) FileUtils.mkdir_p(directory) unless File.directory?(directory) @counter = posts.count @halfway = @counter / 2 info "Exporting #{@counter} posts..." pool = Habari2md::PostExporter.pool(args: [directory, current_actor]) posts.each { |post| pool.async.export(post) } wait(:done) info "We're done." end |
#post_exported(post_id) ⇒ Object
Called by PostExport when an export operation has finished.
94 95 96 97 98 |
# File 'lib/habari2md.rb', line 94 def post_exported(post_id) @counter -= 1 info "50% to go" if @counter == @halfway signal(:done) if @counter == 0 end |
#posts ⇒ Object
69 70 71 |
# File 'lib/habari2md.rb', line 69 def posts db[:posts].order(:modified) end |
#user(id) ⇒ Hash
74 75 76 |
# File 'lib/habari2md.rb', line 74 def user(id) @users.fetch(id, {}) end |