class Thoth::Importer

Public Class Methods

after_import(&block) click to toggle source
# File lib/thoth/importer.rb, line 35
def after_import(&block)    trait(:after    => block); end
before_import(&block) click to toggle source
# File lib/thoth/importer.rb, line 36
def before_import(&block)   trait(:before   => block); end
import_comments(&block) click to toggle source
# File lib/thoth/importer.rb, line 37
def import_comments(&block) trait(:comments => block); end
import_media(&block) click to toggle source
# File lib/thoth/importer.rb, line 38
def import_media(&block)    trait(:media    => block); end
import_pages(&block) click to toggle source
# File lib/thoth/importer.rb, line 39
def import_pages(&block)    trait(:pages    => block); end
import_posts(&block) click to toggle source
# File lib/thoth/importer.rb, line 40
def import_posts(&block)    trait(:posts    => block); end
import_tags(&block) click to toggle source
# File lib/thoth/importer.rb, line 41
def import_tags(&block)     trait(:tags     => block); end
load_importer(name) click to toggle source
# File lib/thoth/importer.rb, line 43
def load_importer(name)
  importer = name.to_s.downcase.strip.gsub(/importer$/, '')
  files    = Dir["{#{File.join(HOME_DIR, 'importer')},#{File.join(LIB_DIR, 'importer')},#{$:.join(',')}}/#{importer}.rb"]

  unless (files.any? && require(files.first)) || require(importer)
    raise LoadError, "Importer #{name} not found"
  end

  Kernel.const_get("#{importer.capitalize}Importer")
end
run() click to toggle source
# File lib/thoth/importer.rb, line 54
def run
  # Bootstrap.
  Ramaze::Log.loggers = []

  begin
    Thoth.init_thoth
  rescue => e
    abort("Error: #{e}")
  end

  # Disable model hooks.
  [Comment, Media, Page, Post].each do |klass|
    klass.class_eval('def before_create; end')
    klass.class_eval('def before_save; end')
  end

  # Confirm that the user really wants to blow away their database.
  puts "WARNING: Your existing Thoth database will be completely erased to make way"
  puts "for the imported content. Are you sure you want to continue? (y/n) "
  print "> "

  exit unless STDIN.gets.strip =~ /^y(?:es)?/i
  puts

  trait[:before].call if trait[:before]

  if trait[:pages]
    puts 'Importing pages...'

    Thoth.db.transaction do
      Page.delete
      trait[:pages].call
    end
  end

  if trait[:posts]
    puts 'Importing blog posts...'

    Thoth.db.transaction do
      Post.delete
      trait[:posts].call
    end
  end

  if trait[:tags]
    puts 'Importing tags...'

    Thoth.db.transaction do
      Tag.delete
      TagsPostsMap.delete
      trait[:tags].call
    end
  end

  if trait[:comments]
    puts 'Importing comments...'

    Thoth.db.transaction do
      Comment.delete
      trait[:comments].call
    end
  end

  if trait[:media]
    puts 'Importing media...'

    Thoth.db.transaction do
      Media.delete
      trait[:media].call
    end
  end

  trait[:after].call if trait[:after]
end