Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make runIf more general #4

Merged
merged 5 commits into from
Feb 15, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -4,3 +4,64 @@
Churros
====
This is a humble library dedicated to making the life of developers easier.

Installation
====
Be sure to have JCenter in the base gradle file.
```groovy
allprojects {
repositories {
jcenter()
}
}
```

Then it's a matter of adding the dependency.
```groovy
dependencies {
// Churros
implementation "com.asapp.churros:churros:0.1.0"
}
```

Things we have
====
* runIf
```kotlin
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(interceptor)
.runIf(isVerbose) {
val loggingInterceptor =
HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)

addNetworkInterceptor(loggingInterceptor)
}
.build()
```
* exhaustive

This code will work happily.
```kotlin
enum class Snack {
HUMUS, CHIPS, GUACAMOLE
}
val today = GUACAMOLE

when (today) {
HUMUS -> getPita()
CHIPS -> buyGuacamole()
}
```
This code will complain that you missed thinking about Guacamole
```kotlin
enum class Snack {
HUMUS, CHIPS, GUACAMOLE
}
val today = GUACAMOLE

when (today) {
HUMUS -> getPita()
CHIPS -> buyGuacamole()
}
.exhaustive()
```
16 changes: 15 additions & 1 deletion app/src/main/java/com/asapp/churros/AnyExtensions.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
package com.asapp.churros

fun Any.exhaustive() {}
/**
* exhaustive is meant to be used in combination with `when` to ensure all options are considered.
*/
fun Any.exhaustive() {}

/**
* runIf preserves the chain in fluent APIs such as RxJava or the Builder pattern.
*/
fun <T> T.runIf(condition: Boolean, block: T.() -> T): T {
return if (condition) {
block()
} else {
this
}
}
1 change: 1 addition & 0 deletions app/src/main/java/com/asapp/churros/rx/Extensions.kt
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ fun Disposable.disposeWith(disposableContainer: DisposableContainer) {
disposableContainer.add(this)
}

@Deprecated("Use the simpler com.asapp.churros.runIf. Will be removed in the next mayor version.")
fun <T> Single<T>.runIf(condition: Boolean, block: Single<T>.() -> Single<T>): Single<T> {
return if (condition) {
block()
23 changes: 23 additions & 0 deletions app/src/test/java/com/asapp/churros/RunIfUnitTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.asapp.churros

import io.reactivex.Single
import org.junit.Test

class RunIfUnitTest {
@Test
fun testRunIf() {
Single.just(1)
.runIf(condition = true) {
map { 2 * it }
}
.test()
.assertValue(2)

Single.just(1)
.runIf(condition = false) {
map { 2 * it }
}
.test()
.assertValue(1)
}
}