Sha256: 662ddb238ae36661c725348258b39c4e91e6612f5bed6eba447eeb2b97e7b69b

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

unless defined? $__rq_configfile__
  module RQ 
#{{{
    LIBDIR = File::dirname(File::expand_path(__FILE__)) + File::SEPARATOR unless
      defined? LIBDIR

    require 'yaml'

    class ConfigFile
#{{{
      DEFAULT_CONFIG = LIBDIR + 'defaultconfig.txt'

      class << self
        def gen_template(arg = nil)
#{{{
          @data ||= IO::read(DEFAULT_CONFIG)
          case arg 
            when IO 
              arg.write @data
            when String
              open(arg, 'w'){|f| f.write @data}
            else
              STDOUT.write @data 
          end
          self
#}}}
        end
        def load_default
#{{{
          @data ||= IO::read(DEFAULT_CONFIG)
          @default ||= YAML::load(munge(@data)) || {}
#}}}
        end
        def any(basename, *dirnames)
#{{{
          config = nil
          dirnames.each do |dirname|
            path = File::join dirname, basename 
            if test ?e, path
              config = self::new(path)
              break
            end
          end
          config || self::new('default') 
#}}}
        end
        def munge buf
#{{{
          buf.gsub(%r/\t/o,'  ')
#}}}
        end
      end
      attr :path
      def initialize path
#{{{
        @path = nil 
        yaml = nil
        if path.nil? or path and path =~ /^\s*default/io
          yaml = self.class.load_default 
          @path = 'DEFAULT' 
        else path
          yaml = YAML::load(self.class.munge(open(path).read))
          @path = path
        end
        self.update yaml
#}}}
      end
      def to_hash
#{{{
        {}.update self
#}}}
      end
#}}}
    end # class ConfigFile
#}}}
  end # module RQ
$__rq_configfile__ = __FILE__ 
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rq-0.1.7 lib/rq-0.1.7/configfile.rb