Sha256: 26c69edc68f65cdd76107d48781450690b5a7639658bcd171c535b185dcdb6f0

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

require 'yaml/store'

module Pair
  HOST = ENV['HOST'] || "api.pairmill.com"

  def self.config(options = nil)
    config = Config.new(HOST, STDIN, STDOUT)
    config.update(options) if options
    config
  end

  class Config
    attr_accessor :host, :input, :output

    def initialize(host = HOST, input = STDIN, output = STDOUT)
      self.host   = host
      self.input  = input
      self.output = output
    end

    def method_missing(method, *args, &block)
      method = method.to_s

      if method =~ /=$/ # setter
        self[method.gsub(/=$/,'').to_sym] = [*args].first
      else
        self[method.to_sym] || super
      end
    end

    def api_token
      self[:api_token]
    end

    def ssh_enabled?
      self[:ssh_enabled]
    end

    def growl_enabled?
      self[:growl_enabled]
    end

    # FIXME: Does this belong in config?
    def ssh_on?
      if Pair::OS.os_x?
        `systemsetup -getremotelogin`.match("Remote Login: On")
      elsif Pair::OS.linux?
        `ps aux | grep sshd | grep -v grep` != ""
      end
    end

    def update(options = {})
      if options.is_a? Hash
        options.each do |key, value|
          self[key] = value
        end
      end
    end

    protected

    def [](key)
      config.transaction do
        host_config[key]
      end
    end

    def []=(key, value)
      config.transaction do
        host_config[key] = value
      end
    end

    # Must be called within a config transaction
    def host_config
      config[host] ||= {}
    end

    def config
      @config ||= YAML::Store.new(config_file)
    end

    def config_file
      File.join(ENV['HOME'], ".pair.yml")
    end

    def print(*args)
      output.print(*args)
    end

    def gets(*args)
      input.gets(*args)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pair-0.0.4 lib/pair/config.rb