Sha256: ff4ccc5642cb650a7782d1ddd827e9140fe92e3996c5ded0e195acc85b265dc9

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

require 'yaml'
require 'hashie/mash'

module BackupRepos
  class Config
    attr_reader :options

    def initialize(options = Hashie::Mash.new({}))
      @options = options
    end

    # === OPTIONS

    def debug
      options.debug || config['debug']
    end

    def backup_root
      return if backup_root_dir.blank?
      File.expand_path(backup_root_dir)
    end

    def github_access_token
      config_token = config['github']['access_token'] if config['github']
      options.github_access_token || config_token
    end

    # ===

    def method_missing(name, *_args)
      options.send(name) || config[name.to_s] || super
    end

    def respond_to_missing?(name, include_private = false)
      options.respond_to?(name) || config.key?(name.to_s) || super
    end

    def config_file
      File.join(Dir.home, '.backup-repos')
    end

    private

    def config
      @config ||= Hashie::Mash.new(file_config)
    end

    def file_config
      return {} unless File.exist?(config_file)
      @file_config ||= (YAML.load_file(config_file) || {})
    end

    def backup_root_dir
      options.backup_root || config['backup_root']
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
backup_repos-0.3.0 lib/backup_repos/config.rb
backup_repos-0.2.1 lib/backup_repos/config.rb