Sha256: d6f2d9eb50b1125a16a56c16a6e06ff8831c615f6d9a300116e0afa39fdcb268

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

#!/usr/bin/env ruby

@root = Pathname.new(File.dirname(__FILE__)).parent.parent.parent.expand_path
require @root.join('lib/visage/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={})
        unless 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={})
        unless ::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

3 entries across 3 versions & 1 rubygems

Version Path
visage-app-0.3.2 lib/visage/config/init.rb
visage-app-0.3.1 lib/visage/config/init.rb
visage-app-0.3.0 lib/visage/config/init.rb