dry-facts 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +70 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/ROADMAP.md +110 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/dry-facts.gemspec +45 -0
- data/lib/dry/facts.rb +11 -0
- data/lib/dry/facts/aggregate.rb +79 -0
- data/lib/dry/facts/command.rb +76 -0
- data/lib/dry/facts/event.rb +82 -0
- data/lib/dry/facts/event_store.rb +54 -0
- data/lib/dry/facts/version.rb +5 -0
- data/lib/dry/facts/workflow.rb +9 -0
- metadata +202 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 716f50956a3fbb10524b482e39e76df2daaad30ad67261cdb817d7376f1cb4d8
|
4
|
+
data.tar.gz: deb420fac8620518e6b6ad44d57d5604c72652a9c762aee09c6c36afac104b79
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3d7c5dfa75ff3ff0268aae2b98334331c5863d32b7c8e5a45b95853ade084c1dbb13e7b45421f6661cd8501411add01f8af3c6c3f9510018467592f66ad1a3ad
|
7
|
+
data.tar.gz: e5856d530722f40c4f5f175b163f225ce3848cb7de923d3a4f63a1b47aa6d9d02406b9462a0bac25ba41980b55932a422bc22e0f80dfecc8a35ba024abc413f3
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
dry-facts (0.1.0)
|
5
|
+
dry-equalizer (>= 0.0.11)
|
6
|
+
dry-events (>= 0.1.0)
|
7
|
+
dry-struct
|
8
|
+
dry-types (>= 0.12.1)
|
9
|
+
dry-validation (>= 0.11.1)
|
10
|
+
graphql (>= 1.7.12)
|
11
|
+
require_all
|
12
|
+
|
13
|
+
GEM
|
14
|
+
remote: https://rubygems.org/
|
15
|
+
specs:
|
16
|
+
concurrent-ruby (1.0.5)
|
17
|
+
dry-configurable (0.7.0)
|
18
|
+
concurrent-ruby (~> 1.0)
|
19
|
+
dry-container (0.6.0)
|
20
|
+
concurrent-ruby (~> 1.0)
|
21
|
+
dry-configurable (~> 0.1, >= 0.1.3)
|
22
|
+
dry-core (0.4.4)
|
23
|
+
concurrent-ruby (~> 1.0)
|
24
|
+
dry-equalizer (0.2.0)
|
25
|
+
dry-events (0.1.0)
|
26
|
+
concurrent-ruby (~> 1.0)
|
27
|
+
dry-core (~> 0.4)
|
28
|
+
dry-equalizer (~> 0.2)
|
29
|
+
dry-logic (0.4.2)
|
30
|
+
dry-container (~> 0.2, >= 0.2.6)
|
31
|
+
dry-core (~> 0.2)
|
32
|
+
dry-equalizer (~> 0.2)
|
33
|
+
dry-struct (0.4.0)
|
34
|
+
dry-core (~> 0.4, >= 0.4.1)
|
35
|
+
dry-equalizer (~> 0.2)
|
36
|
+
dry-types (~> 0.12, >= 0.12.2)
|
37
|
+
ice_nine (~> 0.11)
|
38
|
+
dry-types (0.12.2)
|
39
|
+
concurrent-ruby (~> 1.0)
|
40
|
+
dry-configurable (~> 0.1)
|
41
|
+
dry-container (~> 0.3)
|
42
|
+
dry-core (~> 0.2, >= 0.2.1)
|
43
|
+
dry-equalizer (~> 0.2)
|
44
|
+
dry-logic (~> 0.4, >= 0.4.2)
|
45
|
+
inflecto (~> 0.0.0, >= 0.0.2)
|
46
|
+
dry-validation (0.11.1)
|
47
|
+
concurrent-ruby (~> 1.0)
|
48
|
+
dry-configurable (~> 0.1, >= 0.1.3)
|
49
|
+
dry-core (~> 0.2, >= 0.2.1)
|
50
|
+
dry-equalizer (~> 0.2)
|
51
|
+
dry-logic (~> 0.4, >= 0.4.0)
|
52
|
+
dry-types (~> 0.12.0)
|
53
|
+
graphql (1.7.12)
|
54
|
+
ice_nine (0.11.2)
|
55
|
+
inflecto (0.0.2)
|
56
|
+
minitest (5.11.3)
|
57
|
+
rake (12.3.0)
|
58
|
+
require_all (1.5.0)
|
59
|
+
|
60
|
+
PLATFORMS
|
61
|
+
ruby
|
62
|
+
|
63
|
+
DEPENDENCIES
|
64
|
+
bundler (>= 1.16.a)
|
65
|
+
dry-facts!
|
66
|
+
minitest (>= 5.0)
|
67
|
+
rake (>= 10.0)
|
68
|
+
|
69
|
+
BUNDLED WITH
|
70
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 TODO: Write your name
|
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,39 @@
|
|
1
|
+
# Dry::Facts
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/dry/facts`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'dry-facts'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install dry-facts
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
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).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dry-facts.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/ROADMAP.md
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# Roadmap
|
2
|
+
|
3
|
+
Big vision behind this project is to build powerful toolkit that would allow
|
4
|
+
rapid application development using EventSourcing as a core architectural
|
5
|
+
pattern.
|
6
|
+
|
7
|
+
Being able to build core behaviors and exposing those through GraphQL to the
|
8
|
+
customers is the basic idea.
|
9
|
+
|
10
|
+
Final solution is multifaceted:
|
11
|
+
GraphQL generation (?),
|
12
|
+
persistance,
|
13
|
+
serialization,
|
14
|
+
validations,
|
15
|
+
transactions,
|
16
|
+
composition - all of those are hard topics.
|
17
|
+
|
18
|
+
In order to achieve such a goal
|
19
|
+
I'll try to coordinate'Breadth-first' & 'Depth first' design strategies
|
20
|
+
|
21
|
+
## Top to bottom vision
|
22
|
+
|
23
|
+
Autogenerated GraphQL used to invoke Commands and Queries, which read from Projections,
|
24
|
+
Aggregates, generate and persist events. Events persisted in EventStore.
|
25
|
+
|
26
|
+
## Approach
|
27
|
+
|
28
|
+
Type system will be used to allow generation of GraphQL from core types
|
29
|
+
|
30
|
+
## Challenges
|
31
|
+
|
32
|
+
Brain split,
|
33
|
+
Deliver once,
|
34
|
+
Transactional consistency,
|
35
|
+
Nesting transactions,
|
36
|
+
Event/Command/Schema evolution (behavior, naming, structure),
|
37
|
+
Identity (Sequent -- Repository for aggregates.
|
38
|
+
Implements the Unit-Of-Work and Identity-Map patterns to ensure each aggregate is only loaded once per transaction and that you always get the same aggregate instance back.)
|
39
|
+
|
40
|
+
|
41
|
+
## Alternatives
|
42
|
+
|
43
|
+
No explicit types ?
|
44
|
+
|
45
|
+
|
46
|
+
## Step by step
|
47
|
+
* Command
|
48
|
+
- [x] Skeleton without any meaningful structure
|
49
|
+
- [x] With inner class input contract
|
50
|
+
- [x] With inner class output contract
|
51
|
+
- [x] With explicit method input contract
|
52
|
+
- [x] With explicit method output contract
|
53
|
+
- [ ] Without input contract
|
54
|
+
- [ ] Without output contract
|
55
|
+
- [ ] Unified result
|
56
|
+
- [ ] Multistep
|
57
|
+
- [ ] Composition
|
58
|
+
- [ ] Transactions
|
59
|
+
- [ ] Docs from comments
|
60
|
+
- [ ] Validation with contract
|
61
|
+
- [ ] Validation with multiple steps
|
62
|
+
- [ ] Validation with custom behavior in the arbitrary point of command
|
63
|
+
- [ ] Code & test generator
|
64
|
+
* EventStore
|
65
|
+
- [ ] In memory
|
66
|
+
- [ ] Serialization and typecasting?
|
67
|
+
- [ ] Transactions, causality, correlations
|
68
|
+
- [ ] Lifecycle and maintenance tasks (snapshots (rolling, deployment) and versioning)
|
69
|
+
- [ ] Sequel
|
70
|
+
- [ ] ActiveRecord
|
71
|
+
- [ ] Kafka
|
72
|
+
- [ ] Docs from comments
|
73
|
+
* Event
|
74
|
+
- [ ] Hash
|
75
|
+
- [ ] Object?
|
76
|
+
- [ ] With schema?
|
77
|
+
- [ ] Naming and code evolution?
|
78
|
+
- [ ] Code & test generator
|
79
|
+
- [ ] Docs from comments
|
80
|
+
* Aggregates
|
81
|
+
- [ ] Skeleton without any functionality
|
82
|
+
- [ ] Class instance
|
83
|
+
- [ ] Hash instance
|
84
|
+
- [ ] With contract
|
85
|
+
- [ ] With events
|
86
|
+
- [ ] Code & test generator
|
87
|
+
- [ ] Docs from comments
|
88
|
+
* GraphQL generation
|
89
|
+
- [x] Skeleton without generation
|
90
|
+
- [x] From hash
|
91
|
+
- [ ] Naive generation
|
92
|
+
- [ ] Customizable & helpers
|
93
|
+
* CI
|
94
|
+
* JRuby tests
|
95
|
+
* OpalRuby tests
|
96
|
+
|
97
|
+
t = Types::Strict::String.constrained(size: 2..16)
|
98
|
+
=> #<Dry::Types::Constrained type=#<Dry::Types::Definition primitive=String options={} meta={}> options={:rule=>#<Dry::Logic::Operations::And rules=[#<Dry::Logic::Rule::Predicate predicate=#<Method: Module(Dry::Logic::Predicates::Methods)#type?> options={:args=>[String]}>, #<Dry::Logic::Rule::Predicate predicate=#<Method: Module(Dry::Logic::Predicates::Methods)#size?> options={:args=>[2..16]}>] options={}>} rule=#<Dry::Logic::Operations::And rules=[#<Dry::Logic::Rule::Predicate predicate=#<Method: Module(Dry::Logic::Predicates::Methods)#type?> options={:args=>[String]}>, #<Dry::Logic::Rule::Predicate predicate=#<Method: Module(Dry::Logic::Predicates::Methods)#size?> options={:args=>[2..16]}>] options={}> meta={}>
|
99
|
+
> t.to_ast
|
100
|
+
=> [:constrained, [[:definition, [String, {}]], [:and, [[:predicate, [:type?, [[:type, String], [:input, Undefined]]]], [:predicate, [:size?, [[:size, 2..16], [:input, Undefined]]]]]], {}]]
|
101
|
+
|
102
|
+
|
103
|
+
irb(main):009:0> require 'dry/types/compiler'
|
104
|
+
=> false
|
105
|
+
irb(main):010:0> tc = Dry::Types::Compiler.new(Dry::Types)
|
106
|
+
=> #<Dry::Types::Compiler:0x007fca038cacd8 @registry=Dry::Types>
|
107
|
+
irb(main):011:0> Undefined = Dry::Core::Constants::Undefined
|
108
|
+
=> Undefined
|
109
|
+
irb(main):012:0> tc.visit([:constrained, [[:definition, [String, {}]], [:and, [[:predicate, [:type?, [[:type, String], [:input, Undefined]]]], [:predicate, [:size?, [[:size, 2..16], [:input, Undefined]]]]]], {}]])
|
110
|
+
=> #<Dry::Types::Constrained type=#<Dry::Types::Definition primitive=String options={} meta={}> options={:rule=>#<Dry::Logic::Operations::And rules=[#<Dry::Logic::Rule::Predicate predicate=#<Method: Module(Dry::Logic::Predicates::Methods)#type?> options={:args=>[String]}>, #<Dry::Logic::Rule::Predicate predicate=#<Method: Module(Dry::Logic::Predicates::Methods)#size?> options={:args=>[2..16]}>] options={}>} rule=#<Dry::Logic::Operations::And rules=[#<Dry::Logic::Rule::Predicate predicate=#<Method: Module(Dry::Logic::Predicates::Methods)#type?> options={:args=>[String]}>, #<Dry::Logic::Rule::Predicate predicate=#<Method: Module(Dry::Logic::Predicates::Methods)#size?> options={:args=>[2..16]}>] options={}> meta={}>
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "dry/facts"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/dry-facts.gemspec
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "dry/facts/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "dry-facts"
|
8
|
+
spec.version = Dry::Facts::VERSION
|
9
|
+
spec.authors = ["Andriy Tyurnikov"]
|
10
|
+
spec.email = ["Andriy.Tyurnikov@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{EventSourcing toolkit, based on dry-rb.}
|
13
|
+
spec.description = %q{EventSourcing toolkit, based on dry-rb.}
|
14
|
+
spec.homepage = "https://github.com/andriytyurnikov/dry-facts"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
# else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
# "public gem pushes."
|
24
|
+
# end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_dependency "require_all"
|
34
|
+
spec.add_dependency "dry-struct"
|
35
|
+
spec.add_dependency "dry-types", ">= 0.12.1"
|
36
|
+
spec.add_dependency "dry-validation", ">= 0.11.1"
|
37
|
+
spec.add_dependency "dry-equalizer", ">= 0.0.11"
|
38
|
+
spec.add_dependency "dry-events", ">= 0.1.0"
|
39
|
+
spec.add_dependency "graphql", ">= 1.7.12"
|
40
|
+
|
41
|
+
spec.add_development_dependency "bundler", ">= 1.16.a"
|
42
|
+
spec.add_development_dependency "rake", ">= 10.0"
|
43
|
+
spec.add_development_dependency "minitest", ">= 5.0"
|
44
|
+
|
45
|
+
end
|
data/lib/dry/facts.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
module Dry
|
2
|
+
module Facts
|
3
|
+
class Aggregate
|
4
|
+
# object/hash, representing a state,
|
5
|
+
# computed from many events
|
6
|
+
# state computation rules
|
7
|
+
|
8
|
+
@event_handlers = Hash.new
|
9
|
+
|
10
|
+
attr_reader :events
|
11
|
+
attr_reader :id
|
12
|
+
attr_reader :uuid
|
13
|
+
|
14
|
+
class << self
|
15
|
+
def event_handlers
|
16
|
+
@event_handlers
|
17
|
+
end
|
18
|
+
|
19
|
+
def find_event_handler(key)
|
20
|
+
(@event_handlers)[key]
|
21
|
+
end
|
22
|
+
|
23
|
+
def define_event_handler(name, method_name= :no_op)
|
24
|
+
@event_handlers ||= {}
|
25
|
+
@event_handlers[name] = method_name
|
26
|
+
end
|
27
|
+
|
28
|
+
def aggregate_data_from_events(*event_klasses)
|
29
|
+
event_klasses.each do |klass|
|
30
|
+
define_event_handler klass.name, :_transfer_data_from_event
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
def initialize(events)
|
37
|
+
@events = events || []
|
38
|
+
#TODO ensure same aggregate_id
|
39
|
+
@events.each {|e| self.send :_handle_event, e}
|
40
|
+
@uuid = @id = @events.first.aggregate_id
|
41
|
+
end
|
42
|
+
|
43
|
+
def no_op
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def _default_aggregate_metadata
|
49
|
+
uuid = SecureRandom.uuid,
|
50
|
+
{ id: uuid,
|
51
|
+
uuid: uuid,
|
52
|
+
type: { name: self.class.name,
|
53
|
+
version: '0' } }
|
54
|
+
end
|
55
|
+
|
56
|
+
def _inject_aggregate_metadata_into_event event
|
57
|
+
event.metadata[:aggregate] =
|
58
|
+
_default_aggregate_metadata
|
59
|
+
.merge(event.metadata[:aggregate] || {})
|
60
|
+
end
|
61
|
+
|
62
|
+
def _handle_event event
|
63
|
+
handler_name =
|
64
|
+
self
|
65
|
+
.class
|
66
|
+
.find_event_handler(event.metadata[:type][:name])
|
67
|
+
|
68
|
+
self.send(handler_name, event)
|
69
|
+
end
|
70
|
+
|
71
|
+
def _transfer_data_from_event event
|
72
|
+
event.data.keys.each do |key|
|
73
|
+
self.send "#{key}=", event.data[key]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require "dry-struct"
|
2
|
+
require "dry-types"
|
3
|
+
|
4
|
+
|
5
|
+
module Dry
|
6
|
+
module Facts
|
7
|
+
class Command
|
8
|
+
|
9
|
+
@input_contract = nil
|
10
|
+
@output_contract = nil
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def input_contract
|
14
|
+
@input_contract
|
15
|
+
end
|
16
|
+
|
17
|
+
def input_contract= value
|
18
|
+
@input_contract = value
|
19
|
+
end
|
20
|
+
|
21
|
+
def output_contract
|
22
|
+
@output_contract
|
23
|
+
end
|
24
|
+
|
25
|
+
def output_contract= value
|
26
|
+
@output_contract = value
|
27
|
+
end
|
28
|
+
|
29
|
+
def call(input)
|
30
|
+
input
|
31
|
+
.yield_self {|it| _enforce_input_contract_on it }
|
32
|
+
.yield_self {|it| _execute_with it.to_hash }
|
33
|
+
.yield_self {|it| _enforce_output_contract_on it }
|
34
|
+
end
|
35
|
+
|
36
|
+
def define_input_contract &block
|
37
|
+
@input_contract =
|
38
|
+
Class
|
39
|
+
.new(Dry::Struct)
|
40
|
+
.yield_self { |k| k.instance_eval(&block) if block }
|
41
|
+
end
|
42
|
+
|
43
|
+
def define_output_contract &block
|
44
|
+
@output_contract =
|
45
|
+
Class
|
46
|
+
.new(Dry::Struct)
|
47
|
+
.yield_self { |k| k.instance_eval(&block) if block }
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def _enforce_input_contract_on input
|
53
|
+
if defined?(self::InputContract)
|
54
|
+
self::InputContract
|
55
|
+
else
|
56
|
+
self.input_contract
|
57
|
+
end.new(**input)
|
58
|
+
end
|
59
|
+
|
60
|
+
def _enforce_output_contract_on output
|
61
|
+
if defined?(self::OutputContract)
|
62
|
+
self::OutputContract
|
63
|
+
else
|
64
|
+
self.output_contract
|
65
|
+
end.new(**output)
|
66
|
+
end
|
67
|
+
|
68
|
+
def _execute_with input
|
69
|
+
self.new.call input
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
3
|
+
module Dry
|
4
|
+
module Facts
|
5
|
+
class Event
|
6
|
+
include Dry::Equalizer(:id, :data, :metadata)
|
7
|
+
|
8
|
+
@@_aggregate_constructor = false
|
9
|
+
|
10
|
+
attr_reader :data
|
11
|
+
attr_reader :metadata
|
12
|
+
|
13
|
+
class << self
|
14
|
+
def aggregate_constructor
|
15
|
+
@@_aggregate_constructor = true
|
16
|
+
end
|
17
|
+
|
18
|
+
def from_h(h)
|
19
|
+
Object
|
20
|
+
.const_get(h[:metadata][:type][:name])
|
21
|
+
.new(metadata: h[:metadata], data: h[:data])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
# object/hash
|
25
|
+
# has some infrastructure-related metadata?
|
26
|
+
# metadata
|
27
|
+
# id
|
28
|
+
# event-type-name
|
29
|
+
# event-type-version
|
30
|
+
# aggregate-id ?
|
31
|
+
# correlation-id ?
|
32
|
+
# causation-id ?
|
33
|
+
# stream-name
|
34
|
+
# data
|
35
|
+
|
36
|
+
def initialize data: {},
|
37
|
+
metadata: {}
|
38
|
+
@metadata = _default_metadata_merged_with(metadata) || Hash.new
|
39
|
+
@data = {}.merge(data) || Hash.new
|
40
|
+
end
|
41
|
+
|
42
|
+
def id
|
43
|
+
@metadata[:id]
|
44
|
+
end
|
45
|
+
|
46
|
+
def uuid
|
47
|
+
@metadata[:uuid]
|
48
|
+
end
|
49
|
+
|
50
|
+
def metadata
|
51
|
+
@metadata
|
52
|
+
end
|
53
|
+
|
54
|
+
def aggregate_id
|
55
|
+
self.metadata[:aggregate_id]
|
56
|
+
end
|
57
|
+
|
58
|
+
def to_h
|
59
|
+
{ data: self.data,
|
60
|
+
metadata: self.metadata }
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def _default_metadata_merged_with(metadata={})
|
66
|
+
generated_default_aggregate_id = if @@_aggregate_constructor
|
67
|
+
SecureRandom.uuid
|
68
|
+
end
|
69
|
+
generated_default_uuid = SecureRandom.uuid
|
70
|
+
generated_default_id = generated_default_uuid
|
71
|
+
|
72
|
+
{ id: (generated_default_id),
|
73
|
+
uuid: (generated_default_uuid),
|
74
|
+
aggregate_id: generated_default_aggregate_id,
|
75
|
+
type: { name: self.class.name,
|
76
|
+
version: 0 } }
|
77
|
+
.merge(metadata || {})
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Dry
|
2
|
+
module Facts
|
3
|
+
module EventStore
|
4
|
+
class InMemoryFactStore
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@serialized_events = []
|
8
|
+
end
|
9
|
+
|
10
|
+
def aggregate_from_event(klass, event)
|
11
|
+
get_events_by_key(
|
12
|
+
key: :aggregate_id,
|
13
|
+
value: event.to_h[:data][:aggregate_id])
|
14
|
+
.yield_self {|events| klass.new(events)}
|
15
|
+
end
|
16
|
+
|
17
|
+
# persist_event(event)
|
18
|
+
def persist_event(event)
|
19
|
+
# validate & fail
|
20
|
+
@serialized_events << event.to_h
|
21
|
+
end
|
22
|
+
|
23
|
+
# get_event_by_id(42)
|
24
|
+
def get_event_by_id(fact_id)
|
25
|
+
# fail if event_id nil?
|
26
|
+
# fail if no events found?
|
27
|
+
# fail if many events found?
|
28
|
+
@serialized_events
|
29
|
+
.find_all {|f| fact_id == (f && f[:metadata] && f[:metadata][:id])}
|
30
|
+
.yield_self {|it| (0 == it.length) ? fail("No fact found") : it }
|
31
|
+
.yield_self {|it| (1 < it.length) ? fail("Too may facts") : it }
|
32
|
+
.first
|
33
|
+
.yield_self {|it| deserialize_event(it)}
|
34
|
+
end
|
35
|
+
|
36
|
+
# get_events_by_key(key: 'aggregate-id', value: 42)
|
37
|
+
def get_events_by_key(key:, value:)
|
38
|
+
@serialized_events
|
39
|
+
.find_all {|f| value == f[key]}
|
40
|
+
.map {|it| deserialize_event(it)}
|
41
|
+
end
|
42
|
+
|
43
|
+
def serialize_event(fact)
|
44
|
+
fact.to_h
|
45
|
+
end
|
46
|
+
|
47
|
+
def deserialize_event(serialized_event)
|
48
|
+
Event.from_h(serialized_event)
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
metadata
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dry-facts
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andriy Tyurnikov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-02-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: require_all
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: dry-struct
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: dry-types
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.12.1
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.12.1
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: dry-validation
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.11.1
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.11.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: dry-equalizer
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.0.11
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.0.11
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: dry-events
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.1.0
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.1.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: graphql
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 1.7.12
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 1.7.12
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: bundler
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 1.16.a
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 1.16.a
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rake
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '10.0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '10.0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: minitest
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '5.0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '5.0'
|
153
|
+
description: EventSourcing toolkit, based on dry-rb.
|
154
|
+
email:
|
155
|
+
- Andriy.Tyurnikov@gmail.com
|
156
|
+
executables: []
|
157
|
+
extensions: []
|
158
|
+
extra_rdoc_files: []
|
159
|
+
files:
|
160
|
+
- ".gitignore"
|
161
|
+
- ".travis.yml"
|
162
|
+
- Gemfile
|
163
|
+
- Gemfile.lock
|
164
|
+
- LICENSE.txt
|
165
|
+
- README.md
|
166
|
+
- ROADMAP.md
|
167
|
+
- Rakefile
|
168
|
+
- bin/console
|
169
|
+
- bin/setup
|
170
|
+
- dry-facts.gemspec
|
171
|
+
- lib/dry/facts.rb
|
172
|
+
- lib/dry/facts/aggregate.rb
|
173
|
+
- lib/dry/facts/command.rb
|
174
|
+
- lib/dry/facts/event.rb
|
175
|
+
- lib/dry/facts/event_store.rb
|
176
|
+
- lib/dry/facts/version.rb
|
177
|
+
- lib/dry/facts/workflow.rb
|
178
|
+
homepage: https://github.com/andriytyurnikov/dry-facts
|
179
|
+
licenses:
|
180
|
+
- MIT
|
181
|
+
metadata: {}
|
182
|
+
post_install_message:
|
183
|
+
rdoc_options: []
|
184
|
+
require_paths:
|
185
|
+
- lib
|
186
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
187
|
+
requirements:
|
188
|
+
- - ">="
|
189
|
+
- !ruby/object:Gem::Version
|
190
|
+
version: '0'
|
191
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
192
|
+
requirements:
|
193
|
+
- - ">="
|
194
|
+
- !ruby/object:Gem::Version
|
195
|
+
version: '0'
|
196
|
+
requirements: []
|
197
|
+
rubyforge_project:
|
198
|
+
rubygems_version: 2.7.3
|
199
|
+
signing_key:
|
200
|
+
specification_version: 4
|
201
|
+
summary: EventSourcing toolkit, based on dry-rb.
|
202
|
+
test_files: []
|