From cd9459319b47a896b1e3d9f98377e91a2211514e Mon Sep 17 00:00:00 2001 From: Ian Clarke Date: Sat, 22 Apr 2017 14:24:40 -0500 Subject: [PATCH] add support for creating views directly from Shoebox --- build.gradle | 2 +- src/main/kotlin/com/github/sanity/shoebox/Shoebox.kt | 10 +++++++++- src/main/kotlin/com/github/sanity/shoebox/Store.kt | 1 - src/main/kotlin/com/github/sanity/shoebox/View.kt | 1 - .../com/github/sanity/shoebox/stores/DirectoryStore.kt | 2 +- 5 files changed, 11 insertions(+), 5 deletions(-) diff --git a/build.gradle b/build.gradle index 21931f6..2b4cf99 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'com.github.sanity' -version '0.2.7' +version '0.2.8' buildscript { ext.kotlin_version = '1.1.1' diff --git a/src/main/kotlin/com/github/sanity/shoebox/Shoebox.kt b/src/main/kotlin/com/github/sanity/shoebox/Shoebox.kt index b6096bb..899d54e 100644 --- a/src/main/kotlin/com/github/sanity/shoebox/Shoebox.kt +++ b/src/main/kotlin/com/github/sanity/shoebox/Shoebox.kt @@ -134,7 +134,15 @@ class Shoebox(val store: Store, private val kc: KClass) { } } - + fun view(name : String, by : (T) -> String) : View { + val store = when (store) { + is MemoryStore -> MemoryStore() + is DirectoryStore -> + DirectoryStore(store.directory.parent.resolve("${store.directory.fileName.toString()}-$name-view")) + else -> throw RuntimeException("Shoebox doesn't currently support creating a view for store type ${store::class.simpleName}") + } + return View(Shoebox(store), this, View.VerifyBehavior.ASYNC_VERIFY, by) + } } /** diff --git a/src/main/kotlin/com/github/sanity/shoebox/Store.kt b/src/main/kotlin/com/github/sanity/shoebox/Store.kt index cd6eec1..4a31a8f 100644 --- a/src/main/kotlin/com/github/sanity/shoebox/Store.kt +++ b/src/main/kotlin/com/github/sanity/shoebox/Store.kt @@ -5,7 +5,6 @@ package com.github.sanity.shoebox */ interface Store { val entries: Iterable> - fun remove(key: String): T? operator fun get(key: String): T? operator fun set(key: String, value: T) : T? diff --git a/src/main/kotlin/com/github/sanity/shoebox/View.kt b/src/main/kotlin/com/github/sanity/shoebox/View.kt index 1b7cbb1..1090122 100644 --- a/src/main/kotlin/com/github/sanity/shoebox/View.kt +++ b/src/main/kotlin/com/github/sanity/shoebox/View.kt @@ -1,6 +1,5 @@ package com.github.sanity.shoebox -import java.nio.file.Path import java.util.* import java.util.concurrent.ConcurrentHashMap import kotlin.concurrent.thread diff --git a/src/main/kotlin/com/github/sanity/shoebox/stores/DirectoryStore.kt b/src/main/kotlin/com/github/sanity/shoebox/stores/DirectoryStore.kt index b5538e9..b9b7877 100644 --- a/src/main/kotlin/com/github/sanity/shoebox/stores/DirectoryStore.kt +++ b/src/main/kotlin/com/github/sanity/shoebox/stores/DirectoryStore.kt @@ -17,7 +17,7 @@ import kotlin.reflect.KClass inline fun DirectoryStore(directory : Path) = DirectoryStore(directory, T::class) -class DirectoryStore(private val directory : Path, private val kc : KClass) : Store { +class DirectoryStore(val directory : Path, private val kc : KClass) : Store { companion object { const private val LOCK_FILENAME = "shoebox.lock" const private val LOCK_TOUCH_TIME_MS = 2000.toLong()