-
-
Save kalenp/35d68a7de8c6baaeec82dadce0b22900 to your computer and use it in GitHub Desktop.
Demonstration of adapting ruby methods with kwargs to kotlin interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# cache.rb | |
# Existing ruby cache implementation. Uses kwargs | |
class Cache | |
include RubyCache | |
def set(key, value, ttl:) | |
... | |
end | |
end | |
// RubyCache.kt | |
// Java interface to Ruby cache | |
interface RubyCache { | |
fun set(key: String, value: Object, kwargs: Map<String, Object>) | |
} | |
// KotlinCache.kt | |
// Idiomatic kotlin interface for cache access | |
interface KotlinCache { | |
fun set(key: String, value: Object, ttl: Duration?) | |
} | |
// RubyCacheAdapter.kt | |
// Adapts RubyCache interface to KotlinCache interface | |
class CacheAdapter(private val rubyCache: RubyCache): KotlinCache { | |
override fun set(key: String, value: Object, ttl: Duration?) { | |
rubyCache.set(key, value, mapOf("ttl" to ttl?.seconds)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment