module Shoebox class Storage UnknownResourceType = Class.new(StandardError) attr_reader :resource_types, :data def initialize(*resource_types) @resource_types = resource_types reset end def fetch(resource_type, resource_id, &value) validate_resource_type(resource_type) data[resource_type][resource_id] ||= value.call end def stash(resource_type, resource_id, value) validate_resource_type(resource_type) data[resource_type][resource_id] = value end def get(resource_type, resource_id) validate_resource_type(resource_type) data[resource_type][resource_id] end def reset @data = Hash.new resource_types.each do |resource_type| data[resource_type] = Hash.new end end private def data @data end def validate_resource_type(resource_type) unless resource_types.include?(resource_type) raise UnknownResourceType.new( "#{resource_type} is not a known resource type" ) end end end end