dry-effects 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +17 -9
- data/dry-effects.gemspec +27 -35
- data/lib/dry/effects/extensions.rb +8 -0
- data/lib/dry/effects/extensions/active_support/tagged_logging.rb +13 -0
- data/lib/dry/effects/version.rb +1 -1
- metadata +18 -47
- data/.codeclimate.yml +0 -12
- data/.github/ISSUE_TEMPLATE/----please-don-t-ask-for-support-via-issues.md +0 -10
- data/.github/ISSUE_TEMPLATE/---bug-report.md +0 -30
- data/.github/ISSUE_TEMPLATE/---feature-request.md +0 -18
- data/.github/workflows/ci.yml +0 -52
- data/.github/workflows/docsite.yml +0 -34
- data/.github/workflows/sync_configs.yml +0 -56
- data/.gitignore +0 -12
- data/.rspec +0 -4
- data/.rubocop.yml +0 -101
- data/CODE_OF_CONDUCT.md +0 -13
- data/CONTRIBUTING.md +0 -29
- data/Gemfile +0 -20
- data/Gemfile.devtools +0 -14
- data/Rakefile +0 -8
- data/docsite/source/effects.html.md +0 -29
- data/docsite/source/effects/cache.html.md +0 -84
- data/docsite/source/effects/current_time.html.md +0 -186
- data/docsite/source/effects/defer.html.md +0 -130
- data/docsite/source/effects/env.html.md +0 -144
- data/docsite/source/effects/interrupt.html.md +0 -109
- data/docsite/source/effects/parallel.html.md +0 -25
- data/docsite/source/effects/reader.html.md +0 -126
- data/docsite/source/effects/resolve.html.md +0 -188
- data/docsite/source/effects/state.html.md +0 -178
- data/docsite/source/effects/timeout.html.md +0 -44
- data/docsite/source/index.html.md +0 -212
- data/examples/cmp.rb +0 -51
- data/examples/state.rb +0 -29
- data/project.yml +0 -2
@@ -1,34 +0,0 @@
|
|
1
|
-
# this file is managed by dry-rb/devtools project
|
2
|
-
|
3
|
-
name: docsite
|
4
|
-
|
5
|
-
on:
|
6
|
-
push:
|
7
|
-
paths:
|
8
|
-
- docsite/**
|
9
|
-
- .github/workflows/docsite.yml
|
10
|
-
branches:
|
11
|
-
- master
|
12
|
-
- release-**
|
13
|
-
tags:
|
14
|
-
|
15
|
-
jobs:
|
16
|
-
update-docs:
|
17
|
-
runs-on: ubuntu-latest
|
18
|
-
steps:
|
19
|
-
- uses: actions/checkout@v1
|
20
|
-
- name: Set up Ruby
|
21
|
-
uses: actions/setup-ruby@v1
|
22
|
-
with:
|
23
|
-
ruby-version: "2.6.x"
|
24
|
-
- name: Install dependencies
|
25
|
-
run: |
|
26
|
-
gem install bundler
|
27
|
-
bundle install --jobs 4 --retry 3 --without benchmarks sql
|
28
|
-
- name: Symlink ossy
|
29
|
-
run: mkdir -p bin && ln -sf "$(bundle show ossy)/bin/ossy" bin/ossy
|
30
|
-
- name: Trigger dry-rb.org deploy
|
31
|
-
env:
|
32
|
-
GITHUB_LOGIN: dry-bot
|
33
|
-
GITHUB_TOKEN: ${{ secrets.GH_PAT }}
|
34
|
-
run: bin/ossy github workflow dry-rb/dry-rb.org ci
|
@@ -1,56 +0,0 @@
|
|
1
|
-
# this file is managed by dry-rb/devtools project
|
2
|
-
|
3
|
-
name: sync_configs
|
4
|
-
|
5
|
-
on:
|
6
|
-
repository_dispatch:
|
7
|
-
|
8
|
-
jobs:
|
9
|
-
sync-configs:
|
10
|
-
runs-on: ubuntu-latest
|
11
|
-
if: github.event.action == 'sync_configs'
|
12
|
-
steps:
|
13
|
-
- uses: actions/checkout@v1
|
14
|
-
- name: Set up Ruby
|
15
|
-
uses: eregon/use-ruby-action@master
|
16
|
-
with:
|
17
|
-
ruby-version: 2.6
|
18
|
-
- name: Install latest bundler
|
19
|
-
run: |
|
20
|
-
gem install bundler
|
21
|
-
bundle config set without 'tools benchmarks docs'
|
22
|
-
- name: Install dependencies
|
23
|
-
run: bundle install --jobs 4 --retry 3 --without tools test benchmarks docs
|
24
|
-
- name: Symlink ossy
|
25
|
-
run: ln -sf "$(bundle info ossy --path)/bin/ossy" bin/ossy
|
26
|
-
- name: Clone devtools
|
27
|
-
run: git clone https://github.com/dry-rb/devtools.git tmp/devtools
|
28
|
-
- name: Compile file templates
|
29
|
-
env:
|
30
|
-
GITHUB_LOGIN: dry-bot
|
31
|
-
GITHUB_TOKEN: ${{ secrets.GH_PAT }}
|
32
|
-
run: |
|
33
|
-
if [ -f "project.yml" ]; then
|
34
|
-
for t in `ls tmp/devtools/templates`
|
35
|
-
do
|
36
|
-
bin/ossy t c tmp/devtools/templates/$t $(basename $t .erb) project.yml
|
37
|
-
done
|
38
|
-
fi
|
39
|
-
- name: Update configuration files from devtools
|
40
|
-
run: |
|
41
|
-
if [ -f ".github/workflows/custom_ci.yml" ]; then
|
42
|
-
rsync -arv --exclude '.github/workflows/ci.yml' tmp/devtools/shared/ . ;
|
43
|
-
else
|
44
|
-
rsync -arv tmp/devtools/shared/ . ;
|
45
|
-
fi
|
46
|
-
- name: Commit changes
|
47
|
-
run: |
|
48
|
-
rm bin/ossy
|
49
|
-
git config --local user.email "dry-bot@dry-rb.org"
|
50
|
-
git config --local user.name "dry-bot"
|
51
|
-
git add -A
|
52
|
-
git commit -m "[devtools] sync configs" || echo "nothing changed"
|
53
|
-
- name: Push changes
|
54
|
-
uses: ad-m/github-push-action@master
|
55
|
-
with:
|
56
|
-
github_token: ${{ secrets.GH_PAT }}
|
data/.gitignore
DELETED
data/.rspec
DELETED
data/.rubocop.yml
DELETED
@@ -1,101 +0,0 @@
|
|
1
|
-
# this file is managed by dry-rb/devtools project
|
2
|
-
|
3
|
-
AllCops:
|
4
|
-
TargetRubyVersion: 2.4
|
5
|
-
|
6
|
-
Style/EachWithObject:
|
7
|
-
Enabled: false
|
8
|
-
|
9
|
-
Style/StringLiterals:
|
10
|
-
Enabled: true
|
11
|
-
EnforcedStyle: single_quotes
|
12
|
-
|
13
|
-
Style/ParallelAssignment:
|
14
|
-
Enabled: false
|
15
|
-
|
16
|
-
Style/Alias:
|
17
|
-
Enabled: false
|
18
|
-
|
19
|
-
Style/LambdaCall:
|
20
|
-
Enabled: false
|
21
|
-
|
22
|
-
Style/StabbyLambdaParentheses:
|
23
|
-
Enabled: false
|
24
|
-
|
25
|
-
Style/FormatString:
|
26
|
-
Enabled: false
|
27
|
-
|
28
|
-
Style/Documentation:
|
29
|
-
Enabled: false
|
30
|
-
|
31
|
-
Layout/SpaceInLambdaLiteral:
|
32
|
-
Enabled: false
|
33
|
-
|
34
|
-
Layout/MultilineMethodCallIndentation:
|
35
|
-
Enabled: true
|
36
|
-
EnforcedStyle: indented
|
37
|
-
|
38
|
-
Metrics/LineLength:
|
39
|
-
Max: 100
|
40
|
-
|
41
|
-
Metrics/MethodLength:
|
42
|
-
Enabled: false
|
43
|
-
|
44
|
-
Metrics/ClassLength:
|
45
|
-
Enabled: false
|
46
|
-
|
47
|
-
Metrics/BlockLength:
|
48
|
-
Enabled: false
|
49
|
-
|
50
|
-
Metrics/AbcSize:
|
51
|
-
Max: 20
|
52
|
-
|
53
|
-
Metrics/CyclomaticComplexity:
|
54
|
-
Enabled: true
|
55
|
-
Max: 10
|
56
|
-
|
57
|
-
Lint/BooleanSymbol:
|
58
|
-
Enabled: false
|
59
|
-
|
60
|
-
Style/AccessModifierDeclarations:
|
61
|
-
Enabled: false
|
62
|
-
|
63
|
-
Style/BlockDelimiters:
|
64
|
-
Enabled: false
|
65
|
-
|
66
|
-
Layout/IndentFirstArrayElement:
|
67
|
-
EnforcedStyle: consistent
|
68
|
-
|
69
|
-
Style/ClassAndModuleChildren:
|
70
|
-
Exclude:
|
71
|
-
- "spec/**/*_spec.rb"
|
72
|
-
|
73
|
-
Lint/HandleExceptions:
|
74
|
-
Exclude:
|
75
|
-
- "spec/spec_helper.rb"
|
76
|
-
|
77
|
-
Naming/PredicateName:
|
78
|
-
Enabled: false
|
79
|
-
|
80
|
-
Naming/FileName:
|
81
|
-
Exclude:
|
82
|
-
- "lib/dry-*.rb"
|
83
|
-
|
84
|
-
Style/SymbolArray:
|
85
|
-
Exclude:
|
86
|
-
- "spec/**/*_spec.rb"
|
87
|
-
|
88
|
-
Style/ConditionalAssignment:
|
89
|
-
Enabled: false
|
90
|
-
|
91
|
-
Naming/MethodName:
|
92
|
-
Enabled: false
|
93
|
-
|
94
|
-
Style/AsciiComments:
|
95
|
-
Enabled: false
|
96
|
-
|
97
|
-
Style/DateTime:
|
98
|
-
Enabled: false
|
99
|
-
|
100
|
-
Style/IfUnlessModifier:
|
101
|
-
Enabled: false
|
data/CODE_OF_CONDUCT.md
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
# Contributor Code of Conduct
|
2
|
-
|
3
|
-
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
-
|
5
|
-
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
-
|
7
|
-
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
-
|
9
|
-
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
-
|
11
|
-
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
-
|
13
|
-
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.4.0, available at [https://www.contributor-covenant.org/version/1/4/code-of-conduct](https://www.contributor-covenant.org/version/1/4/code-of-conduct)
|
data/CONTRIBUTING.md
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
# Issue Guidelines
|
2
|
-
|
3
|
-
## Reporting bugs
|
4
|
-
|
5
|
-
If you found a bug, report an issue and describe what's the expected behavior versus what actually happens. If the bug causes a crash, attach a full backtrace. If possible, a reproduction script showing the problem is highly appreciated.
|
6
|
-
|
7
|
-
## Reporting feature requests
|
8
|
-
|
9
|
-
Report a feature request **only after discussing it first on [discourse.dry-rb.org](https://discourse.dry-rb.org)** where it was accepted. Please provide a concise description of the feature, don't link to a discussion thread, and instead summarize what was discussed.
|
10
|
-
|
11
|
-
## Reporting questions, support requests, ideas, concerns etc.
|
12
|
-
|
13
|
-
**PLEASE DON'T** - use [discourse.dry-rb.org](http://discourse.dry-rb.org) instead.
|
14
|
-
|
15
|
-
# Pull Request Guidelines
|
16
|
-
|
17
|
-
A Pull Request will only be accepted if it addresses a specific issue that was reported previously, or fixes typos, mistakes in documentation etc.
|
18
|
-
|
19
|
-
Other requirements:
|
20
|
-
|
21
|
-
1) Do not open a pull request if you can't provide tests along with it. If you have problems writing tests, ask for help in the related issue.
|
22
|
-
2) Follow the style conventions of the surrounding code. In most cases, this is standard ruby style.
|
23
|
-
3) Add API documentation if it's a new feature
|
24
|
-
4) Update API documentation if it changes an existing feature
|
25
|
-
5) Bonus points for sending a PR to [github.com/dry-rb/dry-rb.org](github.com/dry-rb/dry-rb.org) which updates user documentation and guides
|
26
|
-
|
27
|
-
# Asking for help
|
28
|
-
|
29
|
-
If these guidelines aren't helpful, and you're stuck, please post a message on [discourse.dry-rb.org](https://discourse.dry-rb.org) or join [our chat](https://dry-rb.zulipchat.com).
|
data/Gemfile
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
source 'https://rubygems.org'
|
4
|
-
|
5
|
-
eval_gemfile 'Gemfile.devtools'
|
6
|
-
|
7
|
-
gemspec
|
8
|
-
|
9
|
-
group :test do
|
10
|
-
gem 'dry-auto_inject', require: false
|
11
|
-
gem 'dry-system', github: 'dry-rb/dry-system', branch: 'master', require: false
|
12
|
-
gem 'warning'
|
13
|
-
end
|
14
|
-
|
15
|
-
group :tools do
|
16
|
-
gem 'pry-byebug', platform: :mri
|
17
|
-
end
|
18
|
-
|
19
|
-
gem 'dry-struct'
|
20
|
-
gem 'dry-monads'
|
data/Gemfile.devtools
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
2
|
-
|
3
|
-
gem "ossy", github: "solnic/ossy", branch: "master", platforms: :ruby
|
4
|
-
|
5
|
-
group :test do
|
6
|
-
gem 'codacy-coverage', require: false, platforms: :ruby
|
7
|
-
gem 'simplecov', require: false, platforms: :ruby
|
8
|
-
gem 'warning'
|
9
|
-
end
|
10
|
-
|
11
|
-
group :tools do
|
12
|
-
# this is the same version that we use on codacy
|
13
|
-
gem 'rubocop', '0.71.0'
|
14
|
-
end
|
data/Rakefile
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title: Effects
|
3
|
-
layout: gem-single
|
4
|
-
name: dry-effects
|
5
|
-
sections:
|
6
|
-
- reader
|
7
|
-
- state
|
8
|
-
- resolve
|
9
|
-
- cache
|
10
|
-
- current_time
|
11
|
-
- env
|
12
|
-
- interrupt
|
13
|
-
- defer
|
14
|
-
- timeout
|
15
|
-
- parallel
|
16
|
-
---
|
17
|
-
|
18
|
-
`dry-effects` has a bunch of built-in effects and providers:
|
19
|
-
|
20
|
-
- [Reader](/gems/dry-effects/0.1/effects/reader)
|
21
|
-
- [State](/gems/dry-effects/0.1/effects/state)
|
22
|
-
- [Resolve (Dependency Injection)](/gems/dry-effects/0.1/effects/resolve)
|
23
|
-
- [Cache](/gems/dry-effects/0.1/effects/cache)
|
24
|
-
- [Current Time](/gems/dry-effects/0.1/effects/current_time)
|
25
|
-
- [Environment](/gems/dry-effects/0.1/effects/env)
|
26
|
-
- [Interrupt](/gems/dry-effects/0.1/effects/interrupt)
|
27
|
-
- [Deferred Execution](/gems/dry-effects/0.1/effects/defer)
|
28
|
-
- [Timeout](/gems/dry-effects/0.1/effects/timeout)
|
29
|
-
- [Parallel execution](/gems/dry-effects/0.1/effects/parallel)
|
@@ -1,84 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title: Cache
|
3
|
-
layout: gem-single
|
4
|
-
name: dry-effects
|
5
|
-
---
|
6
|
-
|
7
|
-
Cache provides two interfaces for caching. Set up a handler:
|
8
|
-
|
9
|
-
```ruby
|
10
|
-
require 'dry/effects'
|
11
|
-
|
12
|
-
class CacheMiddleware
|
13
|
-
# Providing scope is required
|
14
|
-
# All cache values will be scoped with this key
|
15
|
-
include Dry::Effects::Handler.Cache(:blog)
|
16
|
-
|
17
|
-
def initialize(app)
|
18
|
-
@app = app
|
19
|
-
end
|
20
|
-
|
21
|
-
def call(env)
|
22
|
-
with_cache { @app.env }
|
23
|
-
end
|
24
|
-
end
|
25
|
-
```
|
26
|
-
|
27
|
-
Using `prepend`:
|
28
|
-
|
29
|
-
```ruby
|
30
|
-
require 'dry/effects'
|
31
|
-
|
32
|
-
class ShowUsers
|
33
|
-
include Dry::Effects.Resolve(:user_repo)
|
34
|
-
# It will cache .find_user calls
|
35
|
-
# Users with the same id won't be searched twice
|
36
|
-
# Effectively (no pun intended),
|
37
|
-
# it's `memoize` scoped with the call in CacheMiddleware
|
38
|
-
prepend Dry::Effects.Cache(blog: :find_user)
|
39
|
-
|
40
|
-
def call(user_ids)
|
41
|
-
users = user_ids.map { find_user(id) }
|
42
|
-
# ...
|
43
|
-
end
|
44
|
-
|
45
|
-
def find_user(id)
|
46
|
-
user_repo.find(id)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
```
|
50
|
-
|
51
|
-
Or using `include`:
|
52
|
-
|
53
|
-
```ruby
|
54
|
-
require 'dry/effects'
|
55
|
-
|
56
|
-
class ShowUsers
|
57
|
-
include Dry::Effects.Resolve(:user_repo)
|
58
|
-
# When included, adds #cache method
|
59
|
-
include Dry::Effects.Cache(:blog)
|
60
|
-
|
61
|
-
def call(user_ids)
|
62
|
-
users = user_ids.map { cache(:user, id) { user_repo.find(id) } }
|
63
|
-
# ...
|
64
|
-
end
|
65
|
-
end
|
66
|
-
```
|
67
|
-
|
68
|
-
### Cache longevity
|
69
|
-
|
70
|
-
The default cache handler doesn't (yet) support long-lived storage. Cache values are discarded once `with_cache` returns.
|
71
|
-
|
72
|
-
### Using in tests
|
73
|
-
|
74
|
-
It's usually OK to have a global handler for cache effects:
|
75
|
-
|
76
|
-
```ruby
|
77
|
-
require 'dry/effects'
|
78
|
-
|
79
|
-
with_cache = Object.new.extend(Dry::Effects::Handler.Cache(:my_app, as: :call))
|
80
|
-
|
81
|
-
RSpec.configure do |config|
|
82
|
-
config.around(:each) { with_cache.(&ex) }
|
83
|
-
end
|
84
|
-
```
|
@@ -1,186 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title: Current Time
|
3
|
-
layout: gem-single
|
4
|
-
name: dry-effects
|
5
|
-
---
|
6
|
-
|
7
|
-
Obtaining the current time with `Time.now` is a classic example of a side effect. Code relying on accessing system time is harder to test. One possible solution is passing time around explicitly, but using effects can save you some typing depending on the case.
|
8
|
-
|
9
|
-
Providing and obtaining the current time is straightforward:
|
10
|
-
|
11
|
-
```ruby
|
12
|
-
require 'dry/effects'
|
13
|
-
|
14
|
-
class CurrentTimeMiddleware
|
15
|
-
include Dry::Effects::Handler.CurrentTime
|
16
|
-
|
17
|
-
def initialize(app)
|
18
|
-
@app = app
|
19
|
-
end
|
20
|
-
|
21
|
-
def call(env)
|
22
|
-
# It will use Time.now internally once and set it fixed
|
23
|
-
with_current_time do
|
24
|
-
@app.(env)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
###
|
30
|
-
|
31
|
-
class CreateSubscription
|
32
|
-
include Dry::Effects.Resolve(:subscription_repo)
|
33
|
-
include Dry::Effects.CurrentTime
|
34
|
-
|
35
|
-
def call(values)
|
36
|
-
subscription_repo.create(
|
37
|
-
values.merge(start_at: current_time)
|
38
|
-
)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
```
|
42
|
-
|
43
|
-
### Providing time in tests
|
44
|
-
|
45
|
-
A typical usage would be:
|
46
|
-
|
47
|
-
```ruby
|
48
|
-
require 'dry/effects'
|
49
|
-
|
50
|
-
RSpec.configure do |config|
|
51
|
-
config.include Dry::Effects::Handler.CurrentTime
|
52
|
-
config.include Dry::Effects.CurrentTime
|
53
|
-
|
54
|
-
config.around { |ex| with_current_time(&ex) }
|
55
|
-
end
|
56
|
-
```
|
57
|
-
|
58
|
-
Then anywhere in tests, you can use it:
|
59
|
-
|
60
|
-
```ruby
|
61
|
-
it 'uses current time as a start' do
|
62
|
-
subscription = create_subscription(...)
|
63
|
-
expect(subscription.start_at).to eql(current_time)
|
64
|
-
end
|
65
|
-
```
|
66
|
-
|
67
|
-
To change the time, call `with_current_time` with a proc:
|
68
|
-
|
69
|
-
```ruby
|
70
|
-
it 'closes a subscription with current time' do
|
71
|
-
future = current_time + 86_400
|
72
|
-
closed_subscription = with_current_time(proc { future }) { close_subscription(subscription) }
|
73
|
-
expect(closed_subscription.closed_at).to eql(future)
|
74
|
-
end
|
75
|
-
```
|
76
|
-
|
77
|
-
Wrapping time with a proc is required, read about generators below.
|
78
|
-
|
79
|
-
### Time rounding
|
80
|
-
|
81
|
-
`current_time` accepts an argument for rounding time values. It can be passed statically to the module builder or dynamically to the effect constructor:
|
82
|
-
|
83
|
-
```ruby
|
84
|
-
class CreateSubscription
|
85
|
-
include Dry::Effects.CurrentTime(round: 3)
|
86
|
-
|
87
|
-
def call(...)
|
88
|
-
# value will be rounded to milliseconds
|
89
|
-
current_time
|
90
|
-
# value will be rounded to microseconds
|
91
|
-
current_time(round: 6)
|
92
|
-
end
|
93
|
-
end
|
94
|
-
```
|
95
|
-
|
96
|
-
### Time is fixed
|
97
|
-
|
98
|
-
By default, calling `with_current_time` even without arguments will freeze the current time. This means `current_time` will return the same value during request processing etc.
|
99
|
-
|
100
|
-
You can "unfix" time with passing `fixed: false` to the handler builder:
|
101
|
-
|
102
|
-
```ruby
|
103
|
-
include Dry::Effects::Handler.CurrentTime(fixed: false)
|
104
|
-
```
|
105
|
-
|
106
|
-
However, this is not recommended because it will make the behavior of `current_time` different in tests (where you pass a fixed value) and in a production environment.
|
107
|
-
|
108
|
-
### Using a custom generator
|
109
|
-
|
110
|
-
The default time provider accepts a custom generator which is a simple callable object. This way you can pass a proc with fixed time:
|
111
|
-
|
112
|
-
```ruby
|
113
|
-
frozen = Time.now
|
114
|
-
with_fixed_time(proc { frozen }) do
|
115
|
-
# ...
|
116
|
-
end
|
117
|
-
```
|
118
|
-
|
119
|
-
Or you can change time on every call:
|
120
|
-
|
121
|
-
```ruby
|
122
|
-
start = Time.now
|
123
|
-
with_fixed_time(proc { start += 0.1 }) do
|
124
|
-
# ...
|
125
|
-
end
|
126
|
-
```
|
127
|
-
|
128
|
-
### Discrete time shifts
|
129
|
-
|
130
|
-
If you pass `step: x` to the handler, it will shift the current time on every access by `x`:
|
131
|
-
|
132
|
-
```ruby
|
133
|
-
with_fixed_time(step: 0.1) do
|
134
|
-
current_time # => ... 18:00:00.000
|
135
|
-
current_time # => ... 18:00:00.100
|
136
|
-
current_time # => ... 18:00:00.200
|
137
|
-
end
|
138
|
-
```
|
139
|
-
|
140
|
-
You can also pass initial time:
|
141
|
-
|
142
|
-
```ruby
|
143
|
-
initial = Time.new(1970)
|
144
|
-
with_fixed_time(initial: initial, step: 60) do
|
145
|
-
current_time # => 1970-01-01 00:00:00 +0000
|
146
|
-
current_time # => 1970-01-01 00:01:00 +0000
|
147
|
-
current_time # => 1970-01-01 00:02:00 +0000
|
148
|
-
end
|
149
|
-
```
|
150
|
-
|
151
|
-
### Overriding handlers
|
152
|
-
|
153
|
-
Handlers of current time can be overridden by an outer handler if you pass `overridable: true`:
|
154
|
-
|
155
|
-
```ruby
|
156
|
-
require 'dry/effects'
|
157
|
-
|
158
|
-
class CurrentTimeMiddleware
|
159
|
-
include Dry::Effects::Handler.CurrentTime
|
160
|
-
|
161
|
-
def initialize(app)
|
162
|
-
@app = app
|
163
|
-
end
|
164
|
-
|
165
|
-
def call(env)
|
166
|
-
with_current_time(overridable: ENV['RACK_ENV'].eql?('test')) do
|
167
|
-
@app.(env)
|
168
|
-
end
|
169
|
-
end
|
170
|
-
end
|
171
|
-
```
|
172
|
-
|
173
|
-
It's usually done in tests:
|
174
|
-
|
175
|
-
```ruby
|
176
|
-
# Using global time
|
177
|
-
frozen_time = Time.now
|
178
|
-
|
179
|
-
puts "Running with time #{frozen_time.iso8601}" if ENV['CI']
|
180
|
-
|
181
|
-
RSpec.configure do |config|
|
182
|
-
config.include Dry::Effects::Handler.CurrentTime
|
183
|
-
config.include(Module.new { define_method(:current_time) { frozen_time } })
|
184
|
-
config.around { |ex| with_current_time(proc { frozen_time }, &ex) }
|
185
|
-
end
|
186
|
-
```
|