Skip to content

Commit

Permalink
Improved documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
gcotelli committed May 9, 2019
1 parent 6d01b2e commit b23c0a7
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions docs/Conditions.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
## Conditions
## Extensions to the base library

This package include some extensions to base classes with the intention of improving readability.

- `Boolean>>#then:` is the equivalent to `ifTrue:`, except that `nil` is not returned if the condition is false.
- `Boolean>>#then:otherwise:` is the equivalent of `ifTrue:ifFalse:`
- `BlockClosure>>#unless:` evaluates the block unless the condition is met. It's used to emphasize the block action as the usual case. For example:
```
[ html
attributeAt: 'x:num'
put: (self xnumAmountFormatter format: amount)]
unless: amount isUndefined
```
- `BlockClosure>>#unless:inWhichCase:` is used when you want to emphasize a positive action, and in the exceptional case that the condition is met, evaluate an exceptional action.
For example:
```
[self createLabelFor: aParameter] unless: aParameter isOptional
inWhichCase: [self createOptionalWidgetsFor: aParameter].
```

## Conditions

A condition is used to test a value against it.

## Arithmetic Conditions

Arithmetic conditions are used to test magnitudes against a provided value.
Arithmetic conditions are used to test magnitudes against a provided value.

```smalltalk
| condition |
| condition |
condition := ArithmeticCondition toBeEqualTo: 1.
condition isSatisfiedBy: 1. "returns true"
condition isSatisfiedBy: 0. "returns false"
Expand All @@ -18,7 +38,7 @@ condition isSatisfiedBy: 0. "returns false"
You can easily build the opposite of a condition sending #negated to it.

```smalltalk
| condition |
| condition |
condition := (ArithmeticCondition toBeEqualTo: 1) negated.
condition isSatisfiedBy: 1. "returns false"
condition isSatisfiedBy: 0. "returns true"
Expand All @@ -30,7 +50,7 @@ You can compose conditions using AND or OR as a logic operator. A simple AND com

```smalltalk
| condition |
condition := (ArithmeticCondition toBeGreaterThan: 0)
condition := (ArithmeticCondition toBeGreaterThan: 0)
and: (ArithmeticCondition toBeLessThan: 2).
condition isSatisfiedBy: 0. "returns false"
condition isSatisfiedBy: 1. "returns true"
Expand All @@ -40,7 +60,7 @@ A simple OR composition.

```smalltalk
| condition |
condition :=(ArithmeticCondition toBeEqualTo: 1)
condition :=(ArithmeticCondition toBeEqualTo: 1)
or: (ArithmeticCondition toBeEqualTo: 2).
condition isSatisfiedBy: 1. "returns true"
condition isSatisfiedBy: 2. "returns true"
Expand Down

0 comments on commit b23c0a7

Please sign in to comment.