dry-effects 0.1.0.alpha

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +15 -0
  3. data/.gitignore +12 -0
  4. data/.rspec +3 -0
  5. data/.rubocop.yml +73 -0
  6. data/.travis.yml +31 -0
  7. data/CHANGELOG.md +3 -0
  8. data/CONTRIBUTING.md +29 -0
  9. data/Gemfile +22 -0
  10. data/LICENSE +21 -0
  11. data/README.md +20 -0
  12. data/Rakefile +8 -0
  13. data/dry-effects.gemspec +48 -0
  14. data/examples/amb.rb +51 -0
  15. data/examples/state.rb +29 -0
  16. data/lib/dry/effects.rb +42 -0
  17. data/lib/dry/effects/all.rb +47 -0
  18. data/lib/dry/effects/constructors.rb +8 -0
  19. data/lib/dry/effects/container.rb +11 -0
  20. data/lib/dry/effects/effect.rb +29 -0
  21. data/lib/dry/effects/effects/amb.rb +23 -0
  22. data/lib/dry/effects/effects/async.rb +22 -0
  23. data/lib/dry/effects/effects/cache.rb +67 -0
  24. data/lib/dry/effects/effects/current_time.rb +27 -0
  25. data/lib/dry/effects/effects/defer.rb +31 -0
  26. data/lib/dry/effects/effects/env.rb +31 -0
  27. data/lib/dry/effects/effects/fork.rb +21 -0
  28. data/lib/dry/effects/effects/implicit.rb +25 -0
  29. data/lib/dry/effects/effects/interrupt.rb +29 -0
  30. data/lib/dry/effects/effects/lock.rb +45 -0
  31. data/lib/dry/effects/effects/parallel.rb +19 -0
  32. data/lib/dry/effects/effects/random.rb +19 -0
  33. data/lib/dry/effects/effects/reader.rb +15 -0
  34. data/lib/dry/effects/effects/resolve.rb +26 -0
  35. data/lib/dry/effects/effects/retry.rb +26 -0
  36. data/lib/dry/effects/effects/state.rb +50 -0
  37. data/lib/dry/effects/errors.rb +68 -0
  38. data/lib/dry/effects/extensions.rb +13 -0
  39. data/lib/dry/effects/extensions/auto_inject.rb +67 -0
  40. data/lib/dry/effects/extensions/system.rb +43 -0
  41. data/lib/dry/effects/halt.rb +29 -0
  42. data/lib/dry/effects/handler.rb +58 -0
  43. data/lib/dry/effects/inflector.rb +9 -0
  44. data/lib/dry/effects/initializer.rb +99 -0
  45. data/lib/dry/effects/instruction.rb +8 -0
  46. data/lib/dry/effects/instructions/execute.rb +25 -0
  47. data/lib/dry/effects/instructions/raise.rb +25 -0
  48. data/lib/dry/effects/provider.rb +29 -0
  49. data/lib/dry/effects/provider/class_interface.rb +61 -0
  50. data/lib/dry/effects/providers/amb.rb +36 -0
  51. data/lib/dry/effects/providers/async.rb +31 -0
  52. data/lib/dry/effects/providers/cache.rb +43 -0
  53. data/lib/dry/effects/providers/current_time.rb +49 -0
  54. data/lib/dry/effects/providers/defer.rb +84 -0
  55. data/lib/dry/effects/providers/env.rb +65 -0
  56. data/lib/dry/effects/providers/fork.rb +23 -0
  57. data/lib/dry/effects/providers/implicit.rb +39 -0
  58. data/lib/dry/effects/providers/interrupt.rb +37 -0
  59. data/lib/dry/effects/providers/lock.rb +125 -0
  60. data/lib/dry/effects/providers/parallel.rb +34 -0
  61. data/lib/dry/effects/providers/random.rb +13 -0
  62. data/lib/dry/effects/providers/reader.rb +61 -0
  63. data/lib/dry/effects/providers/resolve.rb +88 -0
  64. data/lib/dry/effects/providers/retry.rb +59 -0
  65. data/lib/dry/effects/providers/state.rb +30 -0
  66. data/lib/dry/effects/stack.rb +67 -0
  67. data/lib/dry/effects/version.rb +7 -0
  68. metadata +263 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 66aa8408a51ae62ea853de06a0d19617925646c28783044e01338e46d5e75e0d
4
+ data.tar.gz: f226f1cbb69164f77db634870e139439290927f58075aa0a2fa83b37bb438c46
5
+ SHA512:
6
+ metadata.gz: bebd714592097d5dde1852ad38bc5162e1d275c7c92d98a36a9f9445dead76dde4325a6b4e58758ca3fc40ae7ba554a2484c1ae64f59b344273f34da9e4d5305
7
+ data.tar.gz: '06600796cf9707b1e55141d3fec4cae8584a8525b3ed3155eb36be05095db85148e2cdc75f2d9aad662096cb454f5a8ab4070989022d6cc16ce98c3af87b3261'
data/.codeclimate.yml ADDED
@@ -0,0 +1,15 @@
1
+ version: "2"
2
+
3
+ prepare:
4
+ fetch:
5
+ - url: "https://raw.githubusercontent.com/dry-rb/devtools/master/.rubocop.yml"
6
+ path: ".rubocop.yml"
7
+
8
+ exclude_patterns:
9
+ - "benchmarks/"
10
+ - "examples/"
11
+ - "spec/"
12
+
13
+ plugins:
14
+ rubocop:
15
+ enabled: true
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,73 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ Style/EachWithObject:
5
+ Enabled: false
6
+
7
+ Style/StringLiterals:
8
+ Enabled: true
9
+ EnforcedStyle: single_quotes
10
+
11
+ Style/Alias:
12
+ Enabled: false
13
+
14
+ Style/LambdaCall:
15
+ Enabled: false
16
+
17
+ Style/StabbyLambdaParentheses:
18
+ Enabled: false
19
+
20
+ Style/FormatString:
21
+ Enabled: false
22
+
23
+ Layout/SpaceInLambdaLiteral:
24
+ Enabled: false
25
+
26
+ Layout/MultilineMethodCallIndentation:
27
+ Enabled: true
28
+ EnforcedStyle: indented
29
+
30
+ Metrics/LineLength:
31
+ Max: 100
32
+
33
+ Metrics/MethodLength:
34
+ Max: 22
35
+
36
+ Metrics/ClassLength:
37
+ Max: 150
38
+
39
+ Metrics/AbcSize:
40
+ Max: 20
41
+
42
+ Metrics/BlockLength:
43
+ Enabled: true
44
+ Exclude:
45
+ - 'spec/**/*_spec.rb'
46
+
47
+ Metrics/CyclomaticComplexity:
48
+ Enabled: true
49
+ Max: 10
50
+
51
+ Lint/BooleanSymbol:
52
+ Enabled: false
53
+
54
+ Style/AccessModifierDeclarations:
55
+ Enabled: false
56
+
57
+ Style/BlockDelimiters:
58
+ Enabled: false
59
+
60
+ Layout/IndentFirstArrayElement:
61
+ EnforcedStyle: consistent
62
+
63
+ Style/ClassAndModuleChildren:
64
+ Exclude:
65
+ - 'spec/**/*_spec.rb'
66
+
67
+ Lint/HandleExceptions:
68
+ Exclude:
69
+ - 'spec/spec_helper.rb'
70
+
71
+ Naming/FileName:
72
+ Exclude:
73
+ - 'lib/dry-*.rb'
data/.travis.yml ADDED
@@ -0,0 +1,31 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ bundler_args: --without benchmarks tools
5
+ before_script:
6
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
7
+ - chmod +x ./cc-test-reporter
8
+ - ./cc-test-reporter before-build
9
+ rvm:
10
+ - 2.5.5
11
+ - 2.6.3
12
+ - truffleruby
13
+ after_script:
14
+ - "[ -d coverage ] && ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
15
+ env:
16
+ global:
17
+ - COVERAGE=true
18
+ matrix:
19
+ allow_failures:
20
+ - rvm: truffleruby
21
+ include:
22
+ - rvm: jruby-9.2.7.0
23
+ jdk: openjdk8
24
+ notifications:
25
+ email: false
26
+ webhooks:
27
+ urls:
28
+ - https://webhooks.gitter.im/e/19098b4253a72c9796db
29
+ on_success: change # options: [always|never|change] default: always
30
+ on_failure: always # options: [always|never|change] default: always
31
+ on_start: false # default: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # v0.1.0
2
+
3
+ TBD
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,29 @@
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](https://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).
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/dry-rb/#{repo_name}" }
6
+
7
+ gemspec
8
+
9
+ group :test do
10
+ gem 'dry-auto_inject', require: false
11
+ gem 'dry-system', github: 'dry-system', branch: 'master', require: false
12
+ gem 'simplecov', require: false, platform: :mri
13
+ gem 'warning'
14
+ end
15
+
16
+ group :tools do
17
+ gem 'pry-byebug', platform: :mri
18
+ gem 'rubocop'
19
+ end
20
+
21
+ gem 'dry-struct'
22
+ gem 'dry-monads'
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Nikita Shilnikov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,20 @@
1
+ [travis]: https://travis-ci.com/dry-rb/dry-effects
2
+ [codeclimate]: https://codeclimate.com/github/dry-rb/dry-effects/maintainability
3
+ [coverage]: https://codeclimate.com/github/dry-rb/dry-effects/test_coverage
4
+ [chat]: https://dry-rb.zulipchat.com
5
+
6
+ # dry-effects [![Join the chat at https://dry-rb.zulipchat.com](https://img.shields.io/badge/dry--rb-join%20chat-%23346b7a.svg)][chat]
7
+
8
+ [![Build Status](https://travis-ci.com/dry-rb/dry-effects.svg?branch=master)][travis]
9
+ [![Maintainability](https://api.codeclimate.com/v1/badges/83bdab1ec9e1097c2284/maintainability)][codeclimate]
10
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/83bdab1ec9e1097c2284/test_coverage)][coverage]
11
+
12
+ ## Development
13
+
14
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
15
+
16
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
17
+
18
+ ## Contributing
19
+
20
+ See CONTRIBUTING.md.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'dry/effects/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'dry-effects'
9
+ spec.version = Dry::Effects::VERSION
10
+ spec.authors = ['Nikita Shilnikov']
11
+ spec.email = ['fg@flashgordon.ru']
12
+
13
+ spec.summary = 'Algebraic effects.'
14
+ spec.description = spec.summary
15
+ spec.homepage = 'https://github.com/dry-rb/dry-effects'
16
+ spec.license = 'MIT'
17
+
18
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
20
+ if spec.respond_to?(:metadata)
21
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
22
+ spec.metadata['homepage_uri'] = spec.homepage
23
+ spec.metadata['source_code_uri'] = 'https://github.com/dry-rb/dry-effects'
24
+ spec.metadata['bug_tracker_uri'] = 'https://github.com/dry-rb/dry-effects/issues'
25
+ else
26
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
27
+ 'public gem pushes.'
28
+ end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|bin)/}) }
34
+ end
35
+ spec.bindir = 'exe'
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ['lib']
38
+
39
+ spec.add_runtime_dependency 'concurrent-ruby', '~> 1.0'
40
+ spec.add_runtime_dependency 'dry-container', '~> 0.7', '>= 0.7.2'
41
+ spec.add_runtime_dependency 'dry-core', '~> 0.4', '>= 0.4.7'
42
+ spec.add_runtime_dependency 'dry-equalizer', '~> 0.2', '>= 0.2.2'
43
+ spec.add_runtime_dependency 'dry-inflector', '~> 0.1', '>= 0.1.2'
44
+ spec.add_runtime_dependency 'dry-initializer', '~> 3.0'
45
+ spec.add_development_dependency 'bundler'
46
+ spec.add_development_dependency 'rake', '~> 10.0'
47
+ spec.add_development_dependency 'rspec', '~> 3.0'
48
+ end
data/examples/amb.rb ADDED
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Operation
4
+ include Dry::Effects.State(:counter)
5
+ include Dry::Effects.Amb(:feature_enabled)
6
+
7
+ def call
8
+ if feature_enabled?
9
+ self.counter += 10
10
+ :with_feature
11
+ else
12
+ self.counter += 1
13
+ :without_feature
14
+ end
15
+ end
16
+ end
17
+
18
+ module Handler
19
+ include Dry::Effects::Handler.State(:counter, as: :with_counter)
20
+ include Dry::Effects::Handler.Amb(:feature_enabled, as: :test_feature)
21
+ end
22
+
23
+ class AmbState
24
+ include Handler
25
+
26
+ def initialize
27
+ @operation = Operation.new
28
+ end
29
+
30
+ def call
31
+ test_feature { with_counter(0) { @operation.call } }
32
+ end
33
+ end
34
+
35
+ class StateAmb
36
+ include Handler
37
+
38
+ def initialize
39
+ @operation = Operation.new
40
+ end
41
+
42
+ def call
43
+ with_counter(0) { test_feature { @operation.call } }
44
+ end
45
+ end
46
+
47
+ amb_then_state = AmbState.new
48
+ state_then_amb = StateAmb.new
49
+
50
+ amb_then_state.() # => [[1, :without_feature], [10, :with_feature]]
51
+ state_then_amb.() # => [11, [:without_feature, :with_feature]]
data/examples/state.rb ADDED
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Operation
4
+ include Dry::Effects.State(:counter)
5
+
6
+ def call
7
+ 3.times do
8
+ self.counter += 1
9
+ end
10
+
11
+ :done
12
+ end
13
+ end
14
+
15
+ class Wrapper
16
+ include Dry::Effects::Handler.State(:counter)
17
+
18
+ def initialize
19
+ @operation = Operation.new
20
+ end
21
+
22
+ def call
23
+ with_counter(0) { @operation.call }
24
+ end
25
+ end
26
+
27
+ __END__
28
+
29
+ Wrapper.new.call # => [3, :done]
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/core/constants'
4
+ require 'dry/effects/version'
5
+ require 'dry/effects/container'
6
+ require 'dry/effects/errors'
7
+ require 'dry/effects/instructions/raise'
8
+
9
+ module Dry
10
+ module Effects
11
+ include Core::Constants
12
+
13
+ class Error < StandardError; end
14
+
15
+ @effects = Container.new
16
+ @providers = Container.new
17
+
18
+ class << self
19
+ attr_reader :effects, :providers
20
+
21
+ def yield(effect)
22
+ result = ::Fiber.yield(effect)
23
+
24
+ if result.is_a?(Instruction)
25
+ result.()
26
+ else
27
+ result
28
+ end
29
+ rescue FiberError => e
30
+ if block_given?
31
+ yield(effect, e)
32
+ else
33
+ raise Errors::UnhandledEffectError, effect
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ require 'dry/effects/handler'
41
+ require 'dry/effects/all'
42
+ require 'dry/effects/extensions'
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'concurrent/map'
4
+ require 'dry/effects'
5
+ require 'dry/effects/inflector'
6
+
7
+ module Dry
8
+ module Effects
9
+ default = %i[
10
+ cache current_time random resolve defer
11
+ state interrupt amb retry fork parallel
12
+ async implicit env lock reader
13
+ ]
14
+
15
+ effect_modules = ::Concurrent::Map.new
16
+
17
+ default.each do |key|
18
+ class_name = Inflector.camelize(key)
19
+
20
+ if ::File.exist?("#{__dir__}/effects/#{key}.rb")
21
+ effects.register(key, memoize: true) do
22
+ require "dry/effects/effects/#{key}"
23
+ Effects.const_get(Inflector.camelize(key))
24
+ end
25
+
26
+ singleton_class.class_eval do
27
+ define_method(class_name) do |*args|
28
+ effect_modules.fetch_or_store([key, args]) do
29
+ ::Dry::Effects.effects[key].new(*args).freeze
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ if ::File.exists?("#{__dir__}/providers/#{key}.rb")
36
+ providers.register(key, memoize: true) do
37
+ require "dry/effects/providers/#{key}"
38
+ Providers.const_get(Inflector.camelize(key))
39
+ end
40
+
41
+ Handler.singleton_class.define_method(class_name) do |*args|
42
+ ::Dry::Effects.providers[key].mixin(*args)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end