Sha256: eb2c68b695f8867445c94a42efe6973009452e30c340a74cb212f759de50149d

Contents?: true

Size: 1.22 KB

Versions: 4

Compression:

Stored size: 1.22 KB

Contents

require "yaml"

module GemSuit
  class CLI < Thor
    module Config

      class Hash
        def initialize(file, default = {})
          @filename = file
          @hash ||= ::Thor::CoreExt::HashWithIndifferentAccess.new File.exists?(@filename) ? YAML.load_file(@filename) : default
        end

        def dump
          YAML.dump hash.inject({}){|h, (k, v)| h[k.to_sym] = v; h}
        end

        def to_str
          hash.collect do |key, value|
            val = case value.class.name
                  when "Array"
                    value.join ","
                  else
                    value
                  end
            "#{key}=#{val}"
          end.join "\n"
        end

        def [](key)
          hash[key]
        end

        def []=(key, value)
          hash[key] = value
          dump_file
        end

      private

        def method_missing(method, *args)
          if hash.respond_to?(method) || method.to_s =~ /^(\w+)\?$/
            hash.send method, *args
          else
            super
          end
        end

        def hash
          @hash
        end

        def dump_file
          File.open @filename, "w" do |file|
            file << dump
          end
        end
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gem_suit-0.1.3 lib/gem_suit/cli/config/hash.rb
gem_suit-0.1.2 lib/gem_suit/cli/config/hash.rb
gem_suit-0.1.1 lib/gem_suit/cli/config/hash.rb
gem_suit-0.1.0 lib/gem_suit/cli/config/hash.rb