Sha256: 38117c564cdb91c17249e1fd43ecfc3ed95a90eb2bcb5767954d54f396edbb24

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

require File.expand_path(File.dirname(__FILE__) + '/store')

module HashPersistent
  class ExtendedStore
    attr_reader :store
    
    def initialize(store)
      @store = store
      yield self if block_given?
    end
    
    def find(key)
      a_singleton = singleton_keys.find do |singleton|
       key.match(singleton[0])
     end

      if a_singleton
        find_or_create(key, a_singleton[1], *a_singleton[2], &a_singleton[3])
      else
        @store.find(key)
      end
    end

    def save(object)
      @store.save(object)
    end

    def delete(object)
      @store.delete(object)
    end

    def create(key, class_to_create, *args, &block)
      @store.create(key, class_to_create, *args, &block)
    end

    def find_or_create(key, class_to_create, *args, &block)
      @store.find_or_create(key, class_to_create, *args, &block)
    end
    
    def singleton_key(pattern, class_to_create, *args, &block)
      "1".match(pattern) # a test to check the duck typing early
      singleton_keys << [pattern, class_to_create, args, block]
    end

    def singleton_keys
      @singleton_keys ||= []
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kissifer-hash-persistent-0.0.0 lib/hash-persistent/extended_store.rb