Sha256: a8c93aa77b178223468be923aa4db023341c12c99c6197a95742d8117ddf6639

Contents?: true

Size: 853 Bytes

Versions: 3

Compression:

Stored size: 853 Bytes

Contents

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

    def create(record)
      @counter = @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 |id, 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

3 entries across 3 versions & 1 rubygems

Version Path
vedeu-0.0.18 lib/vedeu/repository/storage.rb
vedeu-0.0.17 lib/vedeu/repository/storage.rb
vedeu-0.0.16 lib/vedeu/repository/storage.rb