Sha256: 5cda2cdb95729704df5c1f25e7f6d4c67aa86204ea9318f92adaf7d97080165b

Contents?: true

Size: 1.5 KB

Versions: 7

Compression:

Stored size: 1.5 KB

Contents

module Reap

  class Project

    # Configuration is an open hash.
    #
    # TODO: Settings needs improvement -- it's rather sloppy at this point.

    class Settings < Hash

      DEFAULT_FILE = File.join(File.dirname(__FILE__), 'default.yaml')
      REAP_FILE = '{.reap,_reap,reapfile}{.yaml,.yml,}'

      def self.read(location, metadata)
        if File.file?(location)
          file = location
        else
          file = Dir.glob(File.join(location, REAP_FILE), File::FNM_CASEFOLD).first
        end

        if file
          data = YAML::load(File.open(file)) || {}
        else
          data = {}
        end

        new(data, metadata)
      end


      attr :metadata

      # FIXME: when using the settings, I think nil should be considered a none entry and
      # so false would be required to actually mean "off". This means assigning each key value par one a time?

      def initialize(data, metadata)
        super()
        @metadata = metadata
        defaults = File.read(DEFAULT_FILE)
        defaults = instance_eval("<<-XXXXXXXXXXXXX\n#{defaults}\nXXXXXXXXXXXXX")
        defaults = YAML::load(defaults)
        settings = defaults.dup
        data.each do |key, value|
          settings[key] ||= {}
          settings[key].update(value) if value
        end
        update(settings)
      end

      # open hash

      def method_missing(s, *a)
        if s =~ /=$/
          self[s] = a[0]
        elsif a.empty?
          self[s]
        else
          super
        end
      end

    end

  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
reap-9.2.0 lib/reap/settings.rb
reap-9.2.1 lib/reap/settings.rb
reap-9.3.0 lib/reap/settings.rb
reap-9.3.3 lib/reap/settings.rb
reap-9.3.4 lib/reap/settings.rb
reap-9.3.5 lib/reap/settings.rb
reap-9.3.1 lib/reap/settings.rb