Sha256: c546d191bf842ac59500b3cd534dce90bf801e7abe9af3e4308a64924151adf6

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

require 'directory_watcher'

module LedgerWeb
  class Watcher
    def self.run!
      directory = LedgerWeb::Config.instance.get :watch_directory
      glob = "*"

      if directory.nil?
        directory = File.dirname(LedgerWeb::Config.instance.get :ledger_file)
        glob = File.basename(LedgerWeb::Config.instance.get :ledger_file)
      end

      @@dw = DirectoryWatcher.new directory, :glob => glob
      @@dw.interval = LedgerWeb::Config.instance.get :watch_interval
      @@dw.stable = LedgerWeb::Config.instance.get :watch_stable_count

      @@dw.add_observer do |*args|
        args.each do |event|
          if event[0] == :stable
            puts "Loading database"
            LedgerWeb::Database.connect
            LedgerWeb::Database.run_migrations
            file = LedgerWeb::Database.dump_ledger_to_csv
            count = LedgerWeb::Database.load_database(file)
            puts "Loaded #{count} records"
          end
        end
      end

      @@dw.start

    end

    def self.stop!
      @@dw.stop
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ledger_web-1.4.3 lib/ledger_web/watcher.rb