flows 0.1.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (147) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +38 -0
  3. data/.gitignore +9 -1
  4. data/.mdlrc +1 -0
  5. data/.reek.yml +54 -0
  6. data/.rubocop.yml +44 -2
  7. data/.ruby-version +1 -1
  8. data/.yardopts +1 -0
  9. data/CHANGELOG.md +65 -0
  10. data/README.md +186 -256
  11. data/Rakefile +35 -1
  12. data/bin/.rubocop.yml +5 -0
  13. data/bin/all_the_errors +55 -0
  14. data/bin/benchmark +69 -78
  15. data/bin/benchmark_cli/compare.rb +118 -0
  16. data/bin/benchmark_cli/compare/a_plus_b.rb +22 -0
  17. data/bin/benchmark_cli/compare/base.rb +45 -0
  18. data/bin/benchmark_cli/compare/command.rb +47 -0
  19. data/bin/benchmark_cli/compare/ten_steps.rb +22 -0
  20. data/bin/benchmark_cli/examples.rb +23 -0
  21. data/bin/benchmark_cli/examples/.rubocop.yml +19 -0
  22. data/bin/benchmark_cli/examples/a_plus_b/dry_do.rb +23 -0
  23. data/bin/benchmark_cli/examples/a_plus_b/dry_transaction.rb +17 -0
  24. data/bin/benchmark_cli/examples/a_plus_b/flows_do.rb +22 -0
  25. data/bin/benchmark_cli/examples/a_plus_b/flows_railway.rb +13 -0
  26. data/bin/benchmark_cli/examples/a_plus_b/flows_scp.rb +13 -0
  27. data/bin/benchmark_cli/examples/a_plus_b/flows_scp_mut.rb +13 -0
  28. data/bin/benchmark_cli/examples/a_plus_b/flows_scp_oc.rb +21 -0
  29. data/bin/benchmark_cli/examples/a_plus_b/trailblazer.rb +15 -0
  30. data/bin/benchmark_cli/examples/ten_steps/dry_do.rb +70 -0
  31. data/bin/benchmark_cli/examples/ten_steps/dry_transaction.rb +64 -0
  32. data/bin/benchmark_cli/examples/ten_steps/flows_do.rb +69 -0
  33. data/bin/benchmark_cli/examples/ten_steps/flows_railway.rb +58 -0
  34. data/bin/benchmark_cli/examples/ten_steps/flows_scp.rb +58 -0
  35. data/bin/benchmark_cli/examples/ten_steps/flows_scp_mut.rb +58 -0
  36. data/bin/benchmark_cli/examples/ten_steps/flows_scp_oc.rb +66 -0
  37. data/bin/benchmark_cli/examples/ten_steps/trailblazer.rb +60 -0
  38. data/bin/benchmark_cli/helpers.rb +12 -0
  39. data/bin/benchmark_cli/ruby.rb +15 -0
  40. data/bin/benchmark_cli/ruby/command.rb +38 -0
  41. data/bin/benchmark_cli/ruby/method_exec.rb +71 -0
  42. data/bin/benchmark_cli/ruby/self_class.rb +69 -0
  43. data/bin/benchmark_cli/ruby/structs.rb +90 -0
  44. data/bin/console +1 -0
  45. data/bin/docserver +7 -0
  46. data/bin/errors +130 -0
  47. data/bin/errors_cli/contract_error_demo.rb +49 -0
  48. data/bin/errors_cli/di_error_demo.rb +38 -0
  49. data/bin/errors_cli/flow_error_demo.rb +22 -0
  50. data/bin/errors_cli/flows_router_error_demo.rb +15 -0
  51. data/bin/errors_cli/oc_error_demo.rb +40 -0
  52. data/bin/errors_cli/railway_error_demo.rb +10 -0
  53. data/bin/errors_cli/result_error_demo.rb +13 -0
  54. data/bin/errors_cli/scp_error_demo.rb +17 -0
  55. data/docs/.nojekyll +0 -0
  56. data/docs/README.md +13 -0
  57. data/docs/_sidebar.md +2 -0
  58. data/docs/index.html +30 -0
  59. data/flows.gemspec +27 -2
  60. data/forspell.dict +17 -0
  61. data/lefthook.yml +21 -0
  62. data/lib/flows.rb +13 -5
  63. data/lib/flows/contract.rb +402 -0
  64. data/lib/flows/contract/array.rb +55 -0
  65. data/lib/flows/contract/case_eq.rb +43 -0
  66. data/lib/flows/contract/compose.rb +77 -0
  67. data/lib/flows/contract/either.rb +53 -0
  68. data/lib/flows/contract/error.rb +25 -0
  69. data/lib/flows/contract/hash.rb +75 -0
  70. data/lib/flows/contract/hash_of.rb +70 -0
  71. data/lib/flows/contract/helpers.rb +22 -0
  72. data/lib/flows/contract/predicate.rb +34 -0
  73. data/lib/flows/contract/transformer.rb +50 -0
  74. data/lib/flows/contract/tuple.rb +70 -0
  75. data/lib/flows/flow.rb +96 -7
  76. data/lib/flows/flow/errors.rb +29 -0
  77. data/lib/flows/flow/node.rb +132 -0
  78. data/lib/flows/flow/router.rb +29 -0
  79. data/lib/flows/flow/router/custom.rb +59 -0
  80. data/lib/flows/flow/router/errors.rb +11 -0
  81. data/lib/flows/flow/router/simple.rb +25 -0
  82. data/lib/flows/plugin.rb +14 -0
  83. data/lib/flows/plugin/dependency_injector.rb +159 -0
  84. data/lib/flows/plugin/dependency_injector/dependency.rb +24 -0
  85. data/lib/flows/plugin/dependency_injector/dependency_definition.rb +16 -0
  86. data/lib/flows/plugin/dependency_injector/dependency_list.rb +57 -0
  87. data/lib/flows/plugin/dependency_injector/errors.rb +58 -0
  88. data/lib/flows/plugin/implicit_init.rb +45 -0
  89. data/lib/flows/plugin/output_contract.rb +85 -0
  90. data/lib/flows/plugin/output_contract/dsl.rb +48 -0
  91. data/lib/flows/plugin/output_contract/errors.rb +74 -0
  92. data/lib/flows/plugin/output_contract/wrapper.rb +55 -0
  93. data/lib/flows/plugin/profiler.rb +114 -0
  94. data/lib/flows/plugin/profiler/injector.rb +35 -0
  95. data/lib/flows/plugin/profiler/report.rb +48 -0
  96. data/lib/flows/plugin/profiler/report/events.rb +43 -0
  97. data/lib/flows/plugin/profiler/report/flat.rb +41 -0
  98. data/lib/flows/plugin/profiler/report/flat/method_report.rb +81 -0
  99. data/lib/flows/plugin/profiler/report/raw.rb +15 -0
  100. data/lib/flows/plugin/profiler/report/tree.rb +98 -0
  101. data/lib/flows/plugin/profiler/report/tree/calculated_node.rb +116 -0
  102. data/lib/flows/plugin/profiler/report/tree/node.rb +35 -0
  103. data/lib/flows/plugin/profiler/wrapper.rb +53 -0
  104. data/lib/flows/railway.rb +154 -0
  105. data/lib/flows/railway/dsl.rb +18 -0
  106. data/lib/flows/railway/errors.rb +17 -0
  107. data/lib/flows/railway/step.rb +24 -0
  108. data/lib/flows/railway/step_list.rb +38 -0
  109. data/lib/flows/result.rb +189 -2
  110. data/lib/flows/result/do.rb +172 -0
  111. data/lib/flows/result/err.rb +12 -6
  112. data/lib/flows/result/errors.rb +29 -17
  113. data/lib/flows/result/helpers.rb +25 -3
  114. data/lib/flows/result/ok.rb +12 -6
  115. data/lib/flows/shared_context_pipeline.rb +299 -0
  116. data/lib/flows/shared_context_pipeline/dsl.rb +12 -0
  117. data/lib/flows/shared_context_pipeline/dsl/callbacks.rb +38 -0
  118. data/lib/flows/shared_context_pipeline/dsl/tracks.rb +52 -0
  119. data/lib/flows/shared_context_pipeline/errors.rb +17 -0
  120. data/lib/flows/shared_context_pipeline/mutation_step.rb +29 -0
  121. data/lib/flows/shared_context_pipeline/router_definition.rb +21 -0
  122. data/lib/flows/shared_context_pipeline/step.rb +44 -0
  123. data/lib/flows/shared_context_pipeline/track.rb +54 -0
  124. data/lib/flows/shared_context_pipeline/track_list.rb +51 -0
  125. data/lib/flows/shared_context_pipeline/wrap.rb +74 -0
  126. data/lib/flows/util.rb +17 -0
  127. data/lib/flows/util/inheritable_singleton_vars.rb +86 -0
  128. data/lib/flows/util/inheritable_singleton_vars/dup_strategy.rb +98 -0
  129. data/lib/flows/util/inheritable_singleton_vars/isolation_strategy.rb +91 -0
  130. data/lib/flows/util/prepend_to_class.rb +179 -0
  131. data/lib/flows/version.rb +1 -1
  132. metadata +288 -20
  133. data/.travis.yml +0 -8
  134. data/Gemfile.lock +0 -119
  135. data/bin/demo +0 -66
  136. data/bin/examples.rb +0 -159
  137. data/bin/profile_10steps +0 -64
  138. data/bin/ruby_benchmarks +0 -26
  139. data/lib/flows/node.rb +0 -27
  140. data/lib/flows/operation.rb +0 -54
  141. data/lib/flows/operation/builder.rb +0 -130
  142. data/lib/flows/operation/builder/build_router.rb +0 -37
  143. data/lib/flows/operation/dsl.rb +0 -72
  144. data/lib/flows/operation/errors.rb +0 -75
  145. data/lib/flows/operation/executor.rb +0 -78
  146. data/lib/flows/result_router.rb +0 -14
  147. data/lib/flows/router.rb +0 -22
@@ -0,0 +1,10 @@
1
+ module RailwayErrorDemo
2
+ class MyRailway < ::Flows::Railway
3
+ end
4
+
5
+ class << self
6
+ def call
7
+ MyRailway.new
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,13 @@
1
+ module ResultErrorDemo
2
+ class << self
3
+ include Flows::Result::Helpers
4
+
5
+ def success_access_error
6
+ ok(some: :data).error
7
+ end
8
+
9
+ def failure_access_error
10
+ err(some: :data).unwrap
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ module SCPErrorDemo
2
+ class MySCP < ::Flows::SharedContextPipeline; end
3
+
4
+ class NoImplSCP < ::Flows::SharedContextPipeline
5
+ step :hello
6
+ end
7
+
8
+ class << self
9
+ def no_steps
10
+ MySCP.new
11
+ end
12
+
13
+ def no_step_impl
14
+ NoImplSCP.new
15
+ end
16
+ end
17
+ end
File without changes
@@ -0,0 +1,13 @@
1
+ # Flows
2
+
3
+ [![Build Status](https://github.com/ffloyd/flows/workflows/Test/badge.svg)](https://github.com/ffloyd/flows/actions)
4
+ [![codecov](https://codecov.io/gh/ffloyd/flows/branch/master/graph/badge.svg)](https://codecov.io/gh/ffloyd/flows)
5
+ [![Gem Version](https://badge.fury.io/rb/flows.svg)](https://badge.fury.io/rb/flows)
6
+
7
+ Small and fast ruby framework for implementing railway-like operations.
8
+ By design it is close to [Trailblazer::Operation](http://trailblazer.to/gems/operation/2.0/) and [Dry::Transaction](https://dry-rb.org/gems/dry-transaction/),
9
+ but has simpler and flexible DSLs for defining operations and matching results. Also `flows` is faster.
10
+
11
+ `flows` has no production dependencies so it can be used with any framework and cannot bring dependency incompatibilities.
12
+
13
+ Rest of the documentation will be here when v1.0.0 be released.
@@ -0,0 +1,2 @@
1
+ * Overview
2
+ * [Getting Started](README.md)
@@ -0,0 +1,30 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>Flows - framework for your Business Logic Layer</title>
6
+ <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
7
+ <meta name="description" content="Flows documentation">
8
+ <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
9
+ <link rel="stylesheet" href="//unpkg.com/docsify/lib/themes/vue.css">
10
+ </head>
11
+ <body>
12
+ <div id="app">Please wait...</div>
13
+
14
+ <script>
15
+ window.$docsify = {
16
+ name: 'Flows',
17
+ repo: 'ffloyd/flows',
18
+ auto2top: true,
19
+ noEmoji: true,
20
+ loadSidebar: true,
21
+ subMaxLevel: 2
22
+ }
23
+ </script>
24
+
25
+ <script src="//unpkg.com/docsify/lib/docsify.min.js"></script>
26
+ <script src="//unpkg.com/prismjs/components/prism-bash.min.js"></script>
27
+ <script src="//unpkg.com/prismjs/components/prism-ruby.min.js"></script>
28
+ <script src="//unpkg.com/docsify-plantuml/dist/docsify-plantuml.min.js"></script>
29
+ </body>
30
+ </html>
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength
8
8
  spec.authors = ['Roman Kolesnev']
9
9
  spec.email = ['rvkolesnev@gmail.com']
10
10
 
11
- spec.summary = 'Ruby framework for building FSM-like data flows.'
11
+ spec.summary = 'Ruby framework for building your Business Logic Layer inside Rails and other frameworks.'
12
12
  spec.homepage = 'https://github.com/ffloyd/flows'
13
13
  spec.license = 'MIT'
14
14
 
@@ -21,25 +21,50 @@ Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ['lib']
23
23
 
24
+ # This library has no production dependencies.
25
+ # So, it will not block you from updating any dependencies in your project.
26
+ # So, don't add production dependencies.
27
+
28
+ # things that should be part of a standard library
24
29
  spec.add_development_dependency 'bundler', '~> 2.0'
25
- spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rake', '~> 13.0'
26
31
  spec.add_development_dependency 'rspec', '~> 3.0'
27
32
 
33
+ # Documentation is the key!
34
+ spec.add_development_dependency 'yard'
35
+
36
+ # linters to make code and documentation awesome
37
+ spec.add_development_dependency 'forspell', '~> 0.0.8'
38
+ spec.add_development_dependency 'inch'
39
+ spec.add_development_dependency 'mdl'
40
+ spec.add_development_dependency 'reek'
28
41
  spec.add_development_dependency 'rubocop'
42
+ spec.add_development_dependency 'rubocop-md'
29
43
  spec.add_development_dependency 'rubocop-performance'
30
44
  spec.add_development_dependency 'rubocop-rspec'
31
45
 
46
+ # let's make dubugging confortable
47
+ spec.add_development_dependency 'awesome_print'
32
48
  spec.add_development_dependency 'pry'
33
49
 
50
+ # 100% coverage does not mean that you cover everything,
51
+ # but 50% coverage means that you haven't covered half of the project.
34
52
  spec.add_development_dependency 'codecov'
35
53
  spec.add_development_dependency 'simplecov'
36
54
 
37
55
  # benchmarking tools
38
56
  spec.add_development_dependency 'benchmark-ips'
57
+ spec.add_development_dependency 'kalibera'
39
58
  spec.add_development_dependency 'ruby-prof'
40
59
  spec.add_development_dependency 'stackprof'
41
60
 
61
+ # make benchmark scripts a convinient CLI tool
62
+ spec.add_development_dependency 'gli'
63
+ spec.add_development_dependency 'rainbow'
64
+ spec.add_development_dependency 'warning' # to suppress some unhandable Ruby warnings during CLI execution
65
+
42
66
  # alternatives for comparison in benchmarking
67
+ spec.add_development_dependency 'dry-monads', '~> 1.3'
43
68
  spec.add_development_dependency 'dry-transaction'
44
69
  spec.add_development_dependency 'trailblazer-operation'
45
70
  end
@@ -0,0 +1,17 @@
1
+ # Format: one word per line. Empty lines and #-comments are supported too.
2
+ # If you want to add word with its forms, you can write 'word: example' (without quotes) on the line,
3
+ # where 'example' is existing word with the same possible forms (endings) as your word.
4
+ # Example: deduplicate: duplicate
5
+ linter
6
+ linters
7
+ matchers
8
+ superset
9
+ lefthook
10
+ preprocessor
11
+ postprocessor
12
+ upcase
13
+ fixpoint
14
+ Megatron
15
+ homebrew
16
+ MDL
17
+ forspell
@@ -0,0 +1,21 @@
1
+ pre-commit:
2
+ parallel: true
3
+ commands:
4
+ rubocop:
5
+ glob: "{*.rb,*.md,*.gemspec,Gemfile,Rakefile}"
6
+ run: bundle exec rubocop {staged_files}
7
+ reek:
8
+ glob: "*.rb"
9
+ exclude: "^spec|^bin"
10
+ run: bundle exec reek {staged_files}
11
+ markdownlinter:
12
+ glob: "*.md"
13
+ run: bundle exec mdl {staged_files}
14
+ forspell:
15
+ glob: "{*.md,*.rb}"
16
+ run: bundle exec forspell {staged_files}
17
+
18
+ pre-push:
19
+ commands:
20
+ test:
21
+ run: bundle exec rake
@@ -1,13 +1,21 @@
1
+ # Namespace for all the classes and modules of the library.
2
+ #
3
+ # @since 0.4.0
1
4
  module Flows
5
+ # Base class for all the library's errors.
6
+ #
7
+ # @since 0.4.0
2
8
  class Error < StandardError; end
3
9
  end
4
10
 
5
11
  require 'flows/version'
6
12
 
7
- require 'flows/router'
8
- require 'flows/result_router'
9
- require 'flows/node'
10
- require 'flows/flow'
13
+ require 'flows/util'
14
+ require 'flows/plugin'
11
15
 
12
16
  require 'flows/result'
13
- require 'flows/operation'
17
+ require 'flows/contract'
18
+ require 'flows/flow'
19
+
20
+ require 'flows/railway'
21
+ require 'flows/shared_context_pipeline'
@@ -0,0 +1,402 @@
1
+ require_relative 'contract/error'
2
+
3
+ require_relative 'contract/case_eq'
4
+ require_relative 'contract/predicate'
5
+
6
+ require_relative 'contract/transformer'
7
+ require_relative 'contract/compose'
8
+ require_relative 'contract/either'
9
+
10
+ require_relative 'contract/hash'
11
+ require_relative 'contract/hash_of'
12
+ require_relative 'contract/array'
13
+ require_relative 'contract/tuple'
14
+
15
+ require_relative 'contract/helpers'
16
+
17
+ module Flows
18
+ # @abstract
19
+ #
20
+ # A type contract based on Ruby's case equality.
21
+ #
22
+ # ## Motivation
23
+ #
24
+ # In ruby we have limited ability to express type contracts.
25
+ # Because of the dynamic nature of the language we cannot provide type specs or signatures for methods.
26
+ # We can provide type specs in a form of YARD documentation, but in this way we have no real type checking.
27
+ # Nothing will stop execution if type contract is violated.
28
+ #
29
+ # Flows Contracts are designed to provide runtime type checks for critical places in your code.
30
+ # Let's review options we have except Flows Contracts and then define what is Flows Contract more strictly.
31
+ #
32
+ # Recently in the Ruby community, static/runtime type checking tools started to evolve.
33
+ # The most advanced solution right now is [Sorbet](https://sorbet.org/).
34
+ # But Sorbet solves a different problem: it provides static type checking for the whole codebase.
35
+ # Each method will be checked. Moreover, Sorbet is a tool like bundler or rake,
36
+ # not just a library.
37
+ #
38
+ # In contrast, Flows Contracts are designed to be used in critical places only.
39
+ # For example to declare input and output contracts for your service objects.
40
+ # Or to express contracts between application layers
41
+ # (between Data Access Layer and Business Logic Layer for example).
42
+ #
43
+ # As an optional feature Sorbet provides [runtime checks](https://sorbet.org/docs/runtime).
44
+ # And if you already using Sorbet you may use it to express type contracts also.
45
+ # The main differences between Sorbet Runtime and Flows Contracts are:
46
+ #
47
+ # * Contracts relies on Ruby's case equality and set of helper Contract classes for the most common cases.
48
+ # Sorbet provides it's own type system and you have to learn it.
49
+ # * It may be overkill to use Sorbet for expressing contracts only.
50
+ # In contrast, Flows Contracts are not designed to provide contract for each method in your codebase.
51
+ # * Sorbet Runtime checks should be a bit faster then Contracts checks (because of transformations).
52
+ # * The main advantage of Flows Contracts is _transformations_.
53
+ # It allows you to slightly transform data using Contract
54
+ # which adds some degree of flexibility to your entities.
55
+ # See Tranformations section of this documentation for details.
56
+ #
57
+ # Let's check what we have for runtime type checking in pure Ruby.
58
+ # To make some runtime type checks we have at least two ways:
59
+ #
60
+ # * methods like `#is_a?`, `#kind_of?` and `#class` can check if subject is an instance of a particular class
61
+ # * case equality (`===`) in combination with `case` can check different things depends on concrete class.
62
+ # Check [this article](https://blog.arkency.com/the-equals-equals-equals-case-equality-operator-in-ruby/)
63
+ # for details.
64
+ #
65
+ # As you may see - case equality is already a contract check. We don't need additional checkers to test
66
+ # if something is a `String` because `String === x` will do the job.
67
+ # Also lambdas is like predicates with case equality.
68
+ # Ranges check if subject in a range and regular expressions check for string match.
69
+ # The problem is that `===` does not provide any error messages.
70
+ # Second problem - `===` is not an object - it's just a method.
71
+ # Contract should be an object, it opens more ways of composition.
72
+ #
73
+ # _So, Flows Contract is a case equality check wrapped into Contract class instance
74
+ # with assigned error message and optional transformation logic._
75
+ #
76
+ # ## Implementation
77
+ #
78
+ # {Contract} is an abstract class which requires {#check!} method
79
+ # to be implemented. It provides {#===}, {#check}, {#to_proc}, {#transform} and {#transform!} methods for usage in
80
+ # different scenarios. More details in the methods' documentation.
81
+ #
82
+ # {#transform!} must be overriden for types with defined transforming behaviour.
83
+ # By default no transformation defined - input will be equal to output.
84
+ # See Transformations and Transformation Laws sections of this documentation for details.
85
+ #
86
+ # ## Transformations
87
+ #
88
+ # Contract can be used in two ways:
89
+ #
90
+ # * to check if data matches a contract ({#check}, {#check!}, {#===}, {#to_proc})
91
+ # * to check & slightly transform data ({#transform}, {#transform!})
92
+ #
93
+ # Transformation is a way to slightly adjust input value before usage.
94
+ # Good example is when your method accepts both String and Symbol as a name for something,
95
+ # but internally name should always be a Symbol.
96
+ # So, contract for this case can be expressed in the following way:
97
+ #
98
+ # > Accept either String or Symbol, convert valid value to Symbol
99
+ #
100
+ # In this way we still can use both String and Symbol instances as argument,
101
+ # but in the method's implementation we can be sure that we always get Symbol.
102
+ #
103
+ # In the situation when you have to transform one or two arguments
104
+ # it's easier to merely rely on Ruby's methods like `#to_sym`, `#to_s`, etc.
105
+ # But in the cases when we talking about 3-6 arguments or nested arguments -
106
+ # contracts will be more convenient way to express transformations.
107
+ #
108
+ # ## Transformation Laws
109
+ #
110
+ # When you writing transformations for your contract you MUST implement it
111
+ # with respect to the following laws:
112
+ #
113
+ # # let `c` be an any contract
114
+ # # let `x` be an any value valid for `c`
115
+ # # the following statements MUST be true
116
+ #
117
+ # # 1. transformed value MUST match the contract:
118
+ # c.check!(c.transform!(x)) == true
119
+ #
120
+ # # 2. tranformation of transformed value MUST has no effect:
121
+ # c.transform(x) == c.transform(c.transform(x))
122
+ #
123
+ # If you violate these laws - you'll get undefined behaviour of contracts.
124
+ #
125
+ # The meaning of these laws can be explained through [Equivalence Relation](https://en.wikipedia.org/wiki/Equivalence_relation).
126
+ # Let's use the following contract as example:
127
+ #
128
+ # > Accepts natural numbers except zero in form of String or Integer, transforms to Integer
129
+ #
130
+ # We can define a type using a set of all possible type values. For our contract such set can be
131
+ # described like `[1, '1', 2, '2', ...]`.
132
+ #
133
+ # First law says that transformation result must not leave a type.
134
+ # In other words: transformation is a function from contract type to contract type.
135
+ #
136
+ # Second law does two things:
137
+ #
138
+ # * split values of type into [equivalence classes](https://en.wikipedia.org/wiki/Equivalence_class)
139
+ # * for each equivalence class defines one and only one value which should be a transform result for
140
+ # any value inside the equivalent class. You may call it a tranformation [fixpoint](https://en.wikipedia.org/wiki/Fixed_point_(mathematics)).
141
+ #
142
+ # In our example partition will look like this: `[[1, '1'], [2, '2'], ...]`.
143
+ # Each equivalence class consists of Integer and String form of the same natural number.
144
+ # And Integer form is a fixpoint.
145
+ #
146
+ # Let's review another example:
147
+ #
148
+ # > Accepts String, transform is `String#strip`
149
+ #
150
+ # In this example each equivalent class is a set of stripped string and all the possible non-stripped variations.
151
+ # Fixpoint is a stripped string.
152
+ #
153
+ # You may think about transformations as transformers (form cinema and animation).
154
+ # When transformer transforms - it's still the same guy, but in different form (first law).
155
+ # And fixpoint is transformer main form. We remember Megatron mostly as robot, not as truck. (second law)
156
+ #
157
+ # If you find contract transformation too complex abstraction - you can merely not use it.
158
+ # Flows Contracts without transforms become just type contracts.
159
+ #
160
+ # **You MUST be extra careful with transformations and {Compose}.
161
+ # You cannot just compose any set of types and get a correct result.
162
+ # See {Compose} documentation for details**
163
+ #
164
+ # ## Low-level contracts
165
+ #
166
+ # Flows provides some low-level contract classes.
167
+ # In almost all the cases you don't need to implement your own Contract class
168
+ # and you only need to compose your contract from this helper classes.
169
+ #
170
+ # Wrappers for Ruby objects:
171
+ #
172
+ # * {CaseEq} - to wrap Ruby's case equality with error message.
173
+ # Automatically applied if you pass some Ruby object instead of
174
+ # {Contract} to some contract initializer.
175
+ # Please preserve such behaviour in custom contracts.
176
+ # * {Predicate} - to wrap lambda-check with error message
177
+ #
178
+ # Composition and modification of contracts:
179
+ #
180
+ # * {Transformer} - to wrap existing contract with some transformation
181
+ # * {Compose} - to merge two or more contracts
182
+ # * {Either} - to make "or"-contract from two or more provided contracts. (String or Symbol, for example)
183
+ #
184
+ # Contracts for common Ruby collection types:
185
+ #
186
+ # * {Hash} - restrict keys by some contract and values by another contract
187
+ # * {HashOf} - restrict values under particular keys by particular contracts
188
+ # * {Array} - restrict array elements with some contract
189
+ # * {Tuple} - restrict fixed-size array elements with contracts
190
+ #
191
+ # Using these classes as is can be too verbose and ugly when building complex contracts.
192
+ # To address this issue Contract class has singleton methods as shortcuts and {.make} class method as DSL:
193
+ #
194
+ # # Accepts any string, transforms into stripped variant
195
+ # strip_str = Flows::Contract.transformer(String, &:strip)
196
+ #
197
+ # strip_str === 111
198
+ # # => false
199
+ #
200
+ # strip_str.transform!(' AAA ')
201
+ # # => 'AAA'
202
+ #
203
+ # # Accepts positive integers
204
+ # pos_int = Flows::Contract.compose(
205
+ # Integer,
206
+ # Flows::Contract.predicate('must be positive', &:positive?)
207
+ # )
208
+ #
209
+ # pos_int === 10
210
+ # # => true
211
+ #
212
+ # pos_int === -10
213
+ # # => false
214
+ #
215
+ # # Accepts numbers in String format
216
+ # str_num = Flows::Contract.make do
217
+ # compose(
218
+ # String,
219
+ # case_eq(/\A\d+\z/, 'must be a number')
220
+ # )
221
+ # end
222
+ #
223
+ # # Accepts integer or number as string, transforms to integer
224
+ # pos_int_from_str = transformer(either(Integer, str_num), &:to_i)
225
+ #
226
+ # pos_int_from_str === 10
227
+ # # => true
228
+ #
229
+ # pos_int_from_str === '-10'
230
+ # # => false
231
+ #
232
+ # pos_int_from_str.transform!('10')
233
+ # # => 10
234
+ #
235
+ # pos_int_from_str.transform!(10)
236
+ # # => 10
237
+ #
238
+ # # Example of a complex contract
239
+ # user_contract = Flows::Contract.make do
240
+ # hash_of(
241
+ # name: strip_str,
242
+ # email: strip_str,
243
+ # password_hash: String,
244
+ # age: pos_int_from_str,
245
+ # addresses: array(hash_of(
246
+ # country: strip_str,
247
+ # street: strip_str
248
+ # ))
249
+ # )
250
+ # end
251
+ #
252
+ # result = user_contract.transform!(
253
+ # name: ' Roman ',
254
+ # email: 'bla@blabla.com',
255
+ # password_hash: '01234567890ABCDEF',
256
+ # age: '10',
257
+ # addresses: [],
258
+ # blabla: 'blablabla' # extra field will be removed by HashOf#transform
259
+ # )
260
+ #
261
+ # result == {
262
+ # name: 'Roman',
263
+ # email: 'bla@blabla.com',
264
+ # password_hash: '01234567890ABCDEF',
265
+ # age: 10,
266
+ # addresses: []
267
+ # }
268
+ #
269
+ # All the shortcuts (without {.make}) are available as a separate module: {Helpers}.
270
+ #
271
+ # It's up to lead developer how to integrate contracts into app.
272
+ # You may put contract into constant and use it in the first line of your method.
273
+ # Or you can write some DSL.
274
+ # But you should avoid constructing static contracts at runtime -
275
+ # it's better to instantiate them during loading time (by putting it into constant, for example).
276
+ #
277
+ # {Flows::Plugin::OutputContract} in combination with {SharedContextPipeline} will add DSL for contracts.
278
+ # So, you don't need to invent anything to use output contracts with shared context pipelines.
279
+ #
280
+ # ## Private helper methods
281
+ #
282
+ # Some private utility methods are defined to simplify new contract implementations:
283
+ #
284
+ # `to_contract(value) => Flows::Contract` - if value is a Contract does nothing.
285
+ # Otherwise wraps value with {CaseEq}. Useful in initializers.
286
+ #
287
+ # `merge_nested_errors(description, nested_error) => String` - to make an accurate
288
+ # multiline error messages with indentation.
289
+ #
290
+ # @!method check!( other )
291
+ # @abstract
292
+ # Checks for type match.
293
+ # @return [true] `true` if check succesful
294
+ # @raise [Flows::Contract::Error] if check failed
295
+ class Contract
296
+ # Case equality check.
297
+ #
298
+ # Based on {#check!}
299
+ #
300
+ # @example Contracts and Ruby's case
301
+ #
302
+ # case value
303
+ # when contract1 then blablabla
304
+ # when contract2 then blablabla2
305
+ # end
306
+ #
307
+ # @return [Boolean] check result
308
+ def ===(other)
309
+ check!(other)
310
+ true
311
+ rescue Flows::Contract::Error
312
+ false
313
+ end
314
+
315
+ # Checks `other` for type match.
316
+ #
317
+ # Based on {#check!}.
318
+ #
319
+ # @param other [Object] object to check
320
+ # @return [Flows::Result::Ok<true>] if check successful
321
+ # @return [Flows::Result::Err<String>] if check failed
322
+ def check(other)
323
+ check!(other)
324
+ Result::Ok.new(true)
325
+ rescue ::Flows::Contract::Error => err
326
+ Result::Err.new(err.value_error)
327
+ end
328
+
329
+ # Check and transform value.
330
+ #
331
+ # Override this method to implement type transform behaviour.
332
+ #
333
+ # If contract is built from other contracts -
334
+ # all internal contracts must be called via {#transform}.
335
+ #
336
+ # You must obey Transformation Laws (see {Contract} class documentation).
337
+ #
338
+ # @return [Object] successful result with value after transformation
339
+ # @raise [Flows::Contract::Error] if check failed
340
+ def transform!(other)
341
+ check!(other)
342
+ other
343
+ end
344
+
345
+ # Check and transform value.
346
+ #
347
+ # Based on {#transform!}.
348
+ #
349
+ # @return [Flows::Result::Ok<Object>] successful result with value after type transform
350
+ # @return [Flows::Result::Err<String>] failure result with error message
351
+ def transform(other)
352
+ Result::Ok.new(transform!(other))
353
+ rescue ::Flows::Contract::Error => err
354
+ Result::Err.new(err.value_error)
355
+ end
356
+
357
+ # Allows to use contract as proc.
358
+ #
359
+ # Based on {#===}.
360
+ #
361
+ # @example Check all elements in an array
362
+ # pos_num = Flows::Contract::Predicate.new 'must be positive' do |x|
363
+ # x > 0
364
+ # end
365
+ #
366
+ # [1, 2, 3].all?(&pos_num)
367
+ # # => true
368
+ def to_proc
369
+ proc do |obj|
370
+ self === obj # rubocop:disable Style/CaseEquality
371
+ end
372
+ end
373
+
374
+ class << self
375
+ include Helpers
376
+
377
+ # @example
378
+ # Flows::Contract.make { transformer(either(Symbol, String), &:to_sym) }
379
+ #
380
+ # Flows::Contract.make { String }
381
+ def make(&block)
382
+ result = instance_exec(&block)
383
+
384
+ result.is_a?(Contract) ? result : CaseEq.new(result)
385
+ end
386
+ end
387
+
388
+ private
389
+
390
+ # :reek:UtilityFunction
391
+ def to_contract(value)
392
+ value.is_a?(::Flows::Contract) ? value : CaseEq.new(value)
393
+ end
394
+
395
+ # :reek:UtilityFunction
396
+ def merge_nested_errors(description, nested_errors)
397
+ shifted = nested_errors.split("\n").map { |str| ' ' + str }.join("\n")
398
+
399
+ description + "\n" + shifted
400
+ end
401
+ end
402
+ end