Sha256: 8db48904f8371c2be3070c4915b87a417920f94520d2cfc5a3c0911ce7bc9320

Contents?: true

Size: 1.68 KB

Versions: 10

Compression:

Stored size: 1.68 KB

Contents

require "yaml"

module Invoker
  module Power
    # Save and Load Invoker::Power config
    class ConfigExists < StandardError; end

    class Config
      def self.has_config?
        File.exist?(config_file)
      end

      def self.create(options = {})
        if has_config?
          raise ConfigExists, "Config file already exists at location #{config_file}"
        end
        config = new(options)
        config.save
      end

      def self.delete
        if File.exist?(config_file)
          File.delete(config_file)
        end
      end

      def self.config_file
        File.join(Invoker.home, ".invoker", "config")
      end

      def self.config_dir
        File.join(Invoker.home, ".invoker")
      end

      def initialize(options = {})
        @config = options
      end

      def self.load_config
        config_hash = File.open(config_file, "r") { |fl| YAML.load(fl) }
        new(config_hash)
      end

      def dns_port=(dns_port)
        @config[:dns_port] = dns_port
      end

      def http_port=(http_port)
        @config[:http_port] = http_port
      end

      def https_port=(https_port)
        @config[:https_port] = https_port
      end

      def ipfw_rule_number=(ipfw_rule_number)
        @config[:ipfw_rule_number] = ipfw_rule_number
      end

      def dns_port; @config[:dns_port]; end
      def http_port; @config[:http_port]; end
      def ipfw_rule_number; @config[:ipfw_rule_number]; end
      def https_port; @config[:https_port]; end

      def tld
        @config[:tld] || Invoker.default_tld
      end

      def save
        File.open(self.class.config_file, "w") do |fl|
          YAML.dump(@config, fl)
        end
        self
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 3 rubygems

Version Path
itrg-invoker-1.6.1 lib/invoker/power/config.rb
itrg-invoker-1.6.0 lib/invoker/power/config.rb
invoker-1.5.8 lib/invoker/power/config.rb
invoker-1.5.7 lib/invoker/power/config.rb
invoker-1.5.6 lib/invoker/power/config.rb
invoker-1.5.5 lib/invoker/power/config.rb
invoker-1.5.4 lib/invoker/power/config.rb
invoker-1.5.3 lib/invoker/power/config.rb
meepo-1.5.2 lib/invoker/power/config.rb
invoker-1.5.1 lib/invoker/power/config.rb