Sha256: 3a612e7be89c797d525aa2e73d953e09f68b488251b8ae6f5ee351474cac52d4

Contents?: true

Size: 846 Bytes

Versions: 2

Compression:

Stored size: 846 Bytes

Contents

module Vedeu
  class Storage
    def initialize
      @counter = 0
      @map     = {}
    end

    def create(record)
      @counter += 1
      record.id ||= @counter
      map_for(record)[record.id] = record
    end

    def delete(record)
      map_for(record).delete(record.id)
    end

    def reset(klass)
      all(klass).map { |record| delete(record) }
    end

    def find(klass, id)
      map_for_class(klass).fetch(id, nil)
    end

    def all(klass)
      map_for_class(klass).values
    end

    def query(klass, attribute, value)
      map_for_class(klass).select do |_, result|
        return result if result.send(attribute) == value
      end

      nil
    end

    private

    def map_for_class(klass)
      @map[klass.to_s.to_sym] ||= {}
    end

    def map_for(record)
      map_for_class(record.class)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vedeu-0.0.20 lib/vedeu/repository/storage.rb
vedeu-0.0.19 lib/vedeu/repository/storage.rb