Skip to content

Instantly share code, notes, and snippets.

@jmatsu
Last active August 24, 2016 07:35
Show Gist options
  • Save jmatsu/9f2ad7c9e009086d2ffa77751a3de56a to your computer and use it in GitHub Desktop.
Save jmatsu/9f2ad7c9e009086d2ffa77751a3de56a to your computer and use it in GitHub Desktop.
object BookmarkTranslator {
  fun toPresentation(model: Bookmark): BookmarkViewModel {
    return BookmarkViewModel().build(model.id.value) {
      name = model.name
      // ommit
    }
  }
  
  fun toDomainId(viewModel: BookmarkViewModel): Bookmark.Id = Bookmark.Id(viewModel.id.key)
}
class BookmarkViewModel(val id: Id, val name: String, ...) {
  companion object {
    fun build(key: Int, applier: (BookmarkViewModel.Builder) -> Unit): BookmarkViewModel {
      return BookmarkViewModel.Builder(key).apply(applier).build()
    }
  }
  
  data class Builder(key: Int) {
    private val id: BookmarkViewModel.Id = BookmarkViewModel.Id(key)
    lateinit var name: String
    
    // ...
    
    fun build(): BookmarkViewModel = BookmarkViewModel(id, name, ...)
  }

  data class Id(val key: Int, val cratedAt: Date = Date())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment