Sha256: 7fa9453fbdb5ba8df35b83081a859f7859c4eafb02096fff2c3b3dc01cef97f2

Contents?: true

Size: 1.1 KB

Versions: 5

Compression:

Stored size: 1.1 KB

Contents

module Xray

  def self.config
    @@config ||= Config.new
  end

  class Config
    attr_accessor :editor

    CONFIG_FILE = ".xrayconfig"

    def default_editor
      ENV['GEM_EDITOR'] ||
        ENV['VISUAL'] ||
        ENV['EDITOR'] ||
        '/usr/local/bin/subl'
    end

    def editor
      load_config[:editor]
    end

    def editor=(new_editor)
      if new_editor && new_editor != editor
        write_config(editor: new_editor)
        true
      else
        false
      end
    end

    def to_yaml
      {editor: editor}.to_yaml
    end

    def config_file
      if File.exists?("#{Dir.pwd}/#{CONFIG_FILE}")
        "#{Dir.pwd}/#{CONFIG_FILE}"
      else
        "#{Dir.home}/#{CONFIG_FILE}"
      end
    end

    private

    def write_config(new_config)
      config = load_config.merge(new_config)
      File.open(config_file, 'w') { |f| f.write(config.to_yaml) }
    end

    def load_config
      default_config.merge(local_config)
    end

    def local_config
      YAML.load_file(config_file)
    rescue
      {}
    end

    def default_config
      { editor: default_editor }
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
xray-rails-0.3.1 lib/xray/config.rb
xray-rails-0.3.0 lib/xray/config.rb
xray-rails-0.2.0 lib/xray/config.rb
xray-rails-0.1.23 lib/xray/config.rb
xray-rails-0.1.22 lib/xray/config.rb