Sha256: 632741ec01765ce97af4608fdd4db9d02a5f944c9b18a3a005dd192f0adb92cc

Contents?: true

Size: 1.54 KB

Versions: 6

Compression:

Stored size: 1.54 KB

Contents

require 'mongo-db-utils/models'
require 'yaml'

module MongoDbUtils

  class ConfigLoader
    
    ROOT_FOLDER = "~/.mongo-db-utils"
    CONFIG_LOCATION = "#{ROOT_FOLDER}/config.yml"

    def self.load(load_path = CONFIG_LOCATION)
      full_path = File.expand_path(load_path)
      puts "loading config from #{full_path}"

      if File.exist?(full_path) && YAML.load(File.open(full_path))
        config = YAML.load(File.open(full_path))
        config.writer = self
        config
      else
        self.create_fresh_install_config(full_path)
      end
    end

    def self.flush(path = CONFIG_LOCATION)
      path = File.expand_path(path)
      puts "removing: #{path}"
      FileUtils.rm(path) if File.exist?(path)
      self.initialize_files(path)
    end

    def self.save(config, path = CONFIG_LOCATION)
      raise "config is nil" if config.nil?

      File.open( File.expand_path(path), 'w' ) do |out|
        YAML.dump( config, out )
      end
    end

    private 
    def self.create_fresh_install_config(full_path)
        config = Model::Config.new
        config.writer = self
        config.backup_folder = "#{ROOT_FOLDER}/backups"
        self.initialize_files(full_path)
        File.open( full_path, 'w' ) do |out|
          YAML.dump( config, out )
        end
        config
    end

    def self.get_folder_name(path)
      /(.*)\/.*.yml/.match(path)[1]
    end

    def self.initialize_files(path)
      folder = self.get_folder_name(path)
      FileUtils.mkdir_p(folder) unless File.exist?(folder)
      FileUtils.touch(path)
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mongo-db-utils-0.0.9 lib/mongo-db-utils/config-loader.rb
mongo-db-utils-0.0.8 lib/mongo-db-utils/config-loader.rb
mongo-db-utils-0.0.7 lib/mongo-db-utils/config-loader.rb
mongo-db-utils-0.0.6 lib/mongo-db-utils/config-loader.rb
mongo-db-utils-0.0.5 lib/mongo-db-utils/config-loader.rb
mongo-db-utils-0.0.4 lib/mongo-db-utils/config-loader.rb