Sha256: 4e6d5d1f4641f0e16ce330a2dc66be2ed36f506db7cb1e2a5c1894957e6db1db

Contents?: true

Size: 1.68 KB

Versions: 11

Compression:

Stored size: 1.68 KB

Contents

module Tw
  class Conf

    def self.default
      {
        'version' => Tw::VERSION,
        'consumer_key' => 'AYhhkOC8H2yTZyelz3uw',
        'consumer_secret' => '28Ba8YyFDSPgoCYAmH5ANqOmT6qVS8gIhKnUiDbIpU',
        'default_user' => nil,
        'users' => {}
      }
    end

    def self.[](key)
      ENV[key] || conf[key]
    end

    def self.[]=(key,value)
      conf[key] = value
    end

    def self.conf_file
      "#{ENV['HOME']}/.tw.yml"
    end

    def self.conf
      @@conf ||= (
                  res = default
                  if File.exists? conf_file
                    begin
                      data = nil
                      open_conf_file do |f|
                        data = YAML::load f.read
                      end
                      if data['version'] < REQUIRE_VERSION
                        puts "This is tw version #{Tw::VERSION}."
                        puts "Your config file is old ("+data['version']+"). Reset tw settings?"
                        puts "[Y/n]"
                        res = data if STDIN.gets =~ /^n/i
                      else
                        res = data
                      end
                    rescue => e
                      STDERR.puts e
                      File.delete conf_file
                    end
                  end
                  res
                  )
    end

    def self.to_yaml
      self.conf.to_yaml
    end

    def self.save
      open_conf_file('w+') do |f|
        f.write conf.to_yaml
      end
    end

    private
    def self.open_conf_file(opt=nil, &block)
      if block_given?
        yield open(self.conf_file, opt)
      else
        return open(self.conf_file, opt)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
tw-0.3.1 lib/tw/conf.rb
tw-0.3.0 lib/tw/conf.rb
tw-0.2.6 lib/tw/conf.rb
tw-0.2.5 lib/tw/conf.rb
tw-0.2.4 lib/tw/conf.rb
tw-0.2.3 lib/tw/conf.rb
tw-0.2.2 lib/tw/conf.rb
tw-0.2.1 lib/tw/conf.rb
tw-0.2.0 lib/tw/conf.rb
tw-0.1.1 lib/tw/conf.rb
tw-0.1.0 lib/tw/conf.rb