Sha256: e5e0dc03cf61cff1ad446a0f7524f51b245f96015460af6fa670646d27161441

Contents?: true

Size: 1.57 KB

Versions: 8

Compression:

Stored size: 1.57 KB

Contents

#!/usr/bin/env ruby

@root = Pathname.new(File.dirname(__FILE__)).parent.parent.parent.expand_path
require @root.join('lib/visage-app/config')
require 'yaml'

module Visage
  class Config
    class File
      @@config_directories = []
      @@config_directories << Pathname.new(::File.dirname(__FILE__)).expand_path
      @@config_directories << Pathname.new(ENV["CONFIG_PATH"]).expand_path if ENV["CONFIG_PATH"]
      @@config_directories.reverse!

      def self.find(filename, opts={})
        range = opts[:ignore_bundled] ? (0..-2) : (0..-1)
        potential_filenames = @@config_directories[range].map {|d| d.join(filename)}
        potential_filenames.find { |f| ::File.exists?(f) }
      end

      def self.load(filename, opts={})
        if not path = self.find(filename, opts)
          if opts[:create]
            path = @@config_directories.first.join(filename)
            begin
              FileUtils.touch(path)
            rescue Errno::EACCES => e
              raise Errno::EACCES, "Couldn't write #{path}. Do you have CONFIG_PATH set?"
            end
          end
        end

        YAML::load_file(path)
      end

      def self.open(filename, &block)
        path = self.find(filename)
        ::File.open(path, 'r+') do |f|
          block.call(f)
        end
      end

      def initialize(filename, opts={})
        if not ::File.exists?(filename)
          path = @@config_directories.first.join(filename)
          FileUtils.touch(path)
        end
        @file = ::File.open(filename, 'r+')
      end

      def to_s
        @file.path
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
visage-app-2.1.0 lib/visage-app/config/file.rb
visage-app-2.0.5 lib/visage-app/config/file.rb
visage-app-2.0.4 lib/visage-app/config/file.rb
visage-app-2.0.2 lib/visage-app/config/file.rb
visage-app-2.0.0 lib/visage-app/config/file.rb
visage-app-1.0.0 lib/visage-app/config/file.rb
visage-app-0.9.6 lib/visage-app/config/file.rb
visage-app-0.9.5 lib/visage-app/config/file.rb