Sha256: c20df6af4e68731ba0eb665f6fcbbac1e092de6d659f572553c597f91c11ac91

Contents?: true

Size: 1.84 KB

Versions: 6

Compression:

Stored size: 1.84 KB

Contents

#          Copyright (c) 2006 Michael Fellinger m.fellinger@gmail.com
# All files in this distribution are subject to the terms of the Ruby license.

require 'yaml/store'

module Ramaze
  module Store

    # A simple wrapper around YAML::Store

    class Default
      include Enumerable

      attr_accessor :db

      # create a new store with a filename

      def initialize filename = 'db.yaml'
        FileUtils.touch(filename)
        @db = YAML::Store.new(filename)
      end

      # yield a block in a transaction, identical to #db.transaction{}

      def transaction
        @db.transaction do
          yield
        end
      end

      # pass on all methods inside a transaction

      def method_missing(meth, *args, &block)
        @db.transaction do
          @db.send(meth, *args, &block)
        end
      end

      # the actual content of the store in YAML format

      def to_yaml
        dump(:x)
      end

      # loads the #to_yaml

      def original
        YAML.load(to_yaml)
      end

      # available keys of the store

      def keys
        (original || {}).keys
      end

      # is the Store empty? (no keys)

      def empty?
        keys.empty?
      end

      # nondestructive merge on #original

      def merge hash = {}
        original.merge(hash)
      end

      # destructive #merge

      def merge! hash = {}
        hash.each do |key, value|
          transaction do
            @db[key] = value
          end
        end

        original
      end

      # delete all entries

      def clear
        keys.each do |key|
          delete key
        end
      end

      # number of #keys

      def size
        keys.size
      end

      # Iterate over #original and pass the key and value to a block.

      def each
        original.each do |key, value|
          yield key, value
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ramaze-0.0.9 lib/ramaze/store/default.rb
ramaze-0.0.8 lib/ramaze/store/default.rb
ramaze-0.1.1 lib/ramaze/store/default.rb
ramaze-0.1.2 lib/ramaze/store/default.rb
ramaze-0.1.3 lib/ramaze/store/default.rb
ramaze-0.1.0 lib/ramaze/store/default.rb