Sha256: bc6874f7c6466b5d66d6d12300b32539792351fb92258ef57510ff76b698d1b1

Contents?: true

Size: 863 Bytes

Versions: 3

Compression:

Stored size: 863 Bytes

Contents

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

    def create(record)
      map_for(record)[record.name] = record
    end

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

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

    def find(entity, name)
      map_for_class(entity).fetch(name, nil)
    end

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

    def query(entity, attribute, value)
      return false if value.nil? || value.empty?

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

      nil
    end

    private

    attr_reader :map

    def map_for_class(entity)
      map[entity.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.33 lib/vedeu/repository/storage.rb
vedeu-0.0.32 lib/vedeu/repository/storage.rb
vedeu-0.0.31 lib/vedeu/repository/storage.rb