Sha256: 3d51518d5ce0c426387a72d76176426c288af0491f996b7b357ec2d232447a36

Contents?: true

Size: 1.37 KB

Versions: 13

Compression:

Stored size: 1.37 KB

Contents

require 'yaml'

module Skylight
  class UserConfig

    attr_accessor :disable_dev_warning, :disable_env_warning

    def initialize(config)
      @config = config
      reload
    end

    def file_path
      return @file_path if @file_path

      config_path = @config[:user_config_path] || begin
        require "etc"
        home_dir = ENV['HOME'] || Etc.getpwuid.dir || (ENV["USER"] && File.expand_path("~#{ENV['USER']}"))
        if home_dir
          File.join(home_dir, ".skylight")
        else
          raise ConfigError, "The Skylight `user_config_path` must be defined since the home directory cannot be inferred"
        end
      end

      @file_path = File.expand_path(config_path)
    end

    def disable_dev_warning?
      disable_dev_warning
    end

    def disable_env_warning?
      disable_env_warning
    end

    def reload
      config = File.exist?(file_path) ? YAML.load_file(file_path) : false
      return unless config

      self.disable_dev_warning = !!config['disable_dev_warning']
      self.disable_env_warning = !!config['disable_env_warning']
    end

    def save
      FileUtils.mkdir_p(File.dirname(file_path))
      File.open(file_path, 'w') do |f|
        f.puts YAML.dump(to_hash)
      end
    end

    def to_hash
      {
        'disable_dev_warning' => disable_dev_warning,
        'disable_env_warning' => disable_env_warning
      }
    end

  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
skylight-1.7.2 lib/skylight/user_config.rb
skylight-1.7.1 lib/skylight/user_config.rb
skylight-1.7.0 lib/skylight/user_config.rb
skylight-1.6.1 lib/skylight/user_config.rb
skylight-1.6.0 lib/skylight/user_config.rb
skylight-1.5.1 lib/skylight/user_config.rb
skylight-1.5.0 lib/skylight/user_config.rb
skylight-1.4.4 lib/skylight/user_config.rb
skylight-1.4.3 lib/skylight/user_config.rb
skylight-1.4.2 lib/skylight/user_config.rb
skylight-1.4.1 lib/skylight/user_config.rb
skylight-1.4.0 lib/skylight/user_config.rb
skylight-1.4.0.beta.2 lib/skylight/user_config.rb