Class: Habari2md::Exporter

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Celluloid::Logger
Defined in:
lib/habari2md.rb

Overview

Examples:

Export stuff

worker = Habari2md::Exporter.new(db: 'foo', user: 'root')
worker.export_posts("./out")

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dbObject (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

#postsObject



69
70
71
# File 'lib/habari2md.rb', line 69

def posts
  db[:posts].order(:modified)
end

#user(id) ⇒ Hash

Returns:

  • (Hash)


74
75
76
# File 'lib/habari2md.rb', line 74

def user(id)
  @users.fetch(id, {})
end