akasha 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 01eaf484143fe6572ed3efe8338c2b2fb1251bf2
4
+ data.tar.gz: 99b1cfb52b4cb781dd83b2c711d22cbb1ff27ace
5
+ SHA512:
6
+ metadata.gz: f67f3ffbe4d1e75c4c514cff8770f8d8aa95948bafc8241bd1161d27e660765d2de0036d6578e7be60a3cc05f8bd1899400ad24b4461cc85d2b5fd36095ec3a0
7
+ data.tar.gz: 447548998189e251c818d4ab8dfb88b71b85e31aa5e70934d22fa6498b8703ba269ca331d71dde6c0f2936330b231c23dbfaf341b0533a78dabec0b98970e898
@@ -0,0 +1 @@
1
+ /pkg
@@ -0,0 +1,4 @@
1
+ Style/FrozenStringLiteralComment:
2
+ Enabled: false
3
+ Metrics/LineLength:
4
+ Max: 120
@@ -0,0 +1 @@
1
+ 2.4.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Gem's dependencies in akasha.gemspec
6
+ gemspec
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ akasha (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.7.0)
12
+ rspec-core (~> 3.7.0)
13
+ rspec-expectations (~> 3.7.0)
14
+ rspec-mocks (~> 3.7.0)
15
+ rspec-core (3.7.1)
16
+ rspec-support (~> 3.7.0)
17
+ rspec-expectations (3.7.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.7.0)
20
+ rspec-mocks (3.7.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.7.0)
23
+ rspec-support (3.7.1)
24
+ timecop (0.9.1)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ akasha!
31
+ bundler (~> 1.16)
32
+ rake (~> 10.0)
33
+ rspec (~> 3.7)
34
+ timecop
35
+
36
+ BUNDLED WITH
37
+ 1.16.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Marcin Bilski
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.
@@ -0,0 +1,75 @@
1
+ # Akasha
2
+
3
+ A budding CQRS library for Ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'akasha'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install akasha
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'akasha'
25
+
26
+
27
+ class User < Akasha::Aggregate
28
+ def sign_up(email, password)
29
+ changeset << Akasha::Event.new(:user_signed_up, email: email, password: password)
30
+ end
31
+
32
+ def on_user_signed_up(email:, password:, **_)
33
+ @email = email
34
+ @password = password
35
+ end
36
+ end
37
+
38
+ def initialize
39
+ repository = Akasha::Repository.new(Akasha::Storage::MemoryEventStore.new)
40
+ Akasha::Aggregate.connect!(repository)
41
+ end
42
+
43
+ def handle_sign_up_command(params)
44
+ user = User.find_or_create(params[:id])
45
+ user.email = params[:email]
46
+ user.password = params[:password]
47
+ user.save!
48
+ end
49
+ ```
50
+
51
+ > Currently, only memory-based repository is supported.
52
+
53
+ ## Next steps
54
+
55
+ - [ ] Command routing (default and user-defined)
56
+ - [ ] EventHandler (relying only on Eventstore)
57
+ - [ ] HTTP Eventstore storage backend
58
+ - [ ] Namespacing for events and aggregates
59
+ - [ ] Version-based concurrency
60
+ - [ ] Rake task for running EventHandlers
61
+ - [ ] Socket-based Eventstore storage backend
62
+
63
+ ## Development
64
+
65
+ 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.
66
+
67
+ 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).
68
+
69
+ ## Contributing
70
+
71
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bilus/akasha.
72
+
73
+ ## License
74
+
75
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,30 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'akasha/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'akasha'
8
+ spec.version = Akasha::VERSION
9
+ spec.authors = ['Marcin Bilski']
10
+ spec.email = ['marcin@tooploox.com']
11
+
12
+ spec.summary = %q{CQRS library for Ruby}
13
+ spec.description = %q{A simple CQRS library for Ruby.}
14
+ spec.homepage = "https://github.com/bilus/akasha"
15
+ spec.license = 'MIT'
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.16'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.7'
29
+ spec.add_development_dependency 'timecop'
30
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "akasha"
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__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,4 @@
1
+ require 'akasha/event'
2
+ require 'akasha/aggregate'
3
+ require 'akasha/repository'
4
+ require 'akasha/storage/memory_event_store'
@@ -0,0 +1,42 @@
1
+ require_relative 'changeset'
2
+ require_relative 'aggregate/syntax_helpers'
3
+
4
+ module Akasha
5
+ # CQRS Aggregate base class.
6
+ #
7
+ # Usage:
8
+ #
9
+ # class User < Akasha::Aggregate
10
+ # def sign_up(email, password)
11
+ # changeset << Akasha::Event.new(:user_signed_up, email: email, password: password)
12
+ # end
13
+ #
14
+ # def on_user_signed_up(email:, password:, **_)
15
+ # @email = email
16
+ # @password = password
17
+ # end
18
+ # end
19
+ class Aggregate
20
+ include SyntaxHelpers
21
+
22
+ attr_reader :changeset
23
+
24
+ def initialize(id)
25
+ @changeset = Changeset.new(id)
26
+ end
27
+
28
+ # Replay events, building up the state of the aggregate.
29
+ # Used by Repository.
30
+ def apply_events(events)
31
+ events.each do |event|
32
+ send(event_handler(event), event.data)
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def event_handler(event)
39
+ :"on_#{event.name}"
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,45 @@
1
+ module Akasha
2
+ # Adds syntax sugar to aggregates.
3
+ #
4
+ # Initialize using the `connect!` method:
5
+ # repository = Akasha::Repository.new(Akasha::Storage::MemoryEventStore.new)
6
+ # Aggregate.connect!(repository)
7
+ #
8
+ # Example usage:
9
+ # item = Item.find_or_create('item-1')
10
+ # ... modify item ..
11
+ # item.save!
12
+ #
13
+ module SyntaxHelpers
14
+ def self.included(base)
15
+ base.include(InstanceMethods)
16
+ base.extend(ClassMethods)
17
+ end
18
+
19
+ # Aggregate class methods.
20
+ module ClassMethods
21
+ # Connects to a repository.
22
+ def connect!(repository)
23
+ @@repository = repository
24
+ end
25
+
26
+ # Returns repository or nil if `connect!` not called.
27
+ def repository
28
+ @@repository
29
+ end
30
+
31
+ # Creates and loads the aggregate.
32
+ def find_or_create(id)
33
+ @@repository.load_aggregate(self, id)
34
+ end
35
+ end
36
+
37
+ # Aggregate instance methods.
38
+ module InstanceMethods
39
+ # Saves the aggregate.
40
+ def save!
41
+ self.class.repository.save_aggregate(self)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,17 @@
1
+ module Akasha
2
+ # Represents changes to an aggregate, for example an array of
3
+ # events generated when handling a command.
4
+ class Changeset
5
+ attr_reader :aggregate_id, :events
6
+
7
+ def initialize(aggregate_id)
8
+ @aggregate_id = aggregate_id
9
+ @events = []
10
+ end
11
+
12
+ # Adds an event to the changeset.
13
+ def <<(event)
14
+ @events << event
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ require 'time'
2
+
3
+ module Akasha
4
+ # Event contains all information pertaining to a single
5
+ # event recorded by the system.
6
+ class Event
7
+ attr_reader :name, :data, :created_at
8
+
9
+ def initialize(name, created_at = Time.now.utc, **data)
10
+ @name = name
11
+ @created_at = created_at
12
+ @data = data
13
+ end
14
+
15
+ def ==(other)
16
+ self.class == other.class &&
17
+ name == other.name &&
18
+ data == other.data &&
19
+ created_at == other.created_at
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,39 @@
1
+ module Akasha
2
+ # Aggregate repository.
3
+ # Not meant to be used directly (see aggregate/syntax_helpers.rb)
4
+ # See specs for usage.
5
+ class Repository
6
+ STREAM_NAME_SEP = '-'.freeze
7
+
8
+ def initialize(store)
9
+ @store = store
10
+ end
11
+
12
+ def load_aggregate(klass, id)
13
+ agg = klass.new(id)
14
+
15
+ start = 0
16
+ chunk_size = 100
17
+ stream(klass, id).read_events(start, chunk_size) do |events|
18
+ agg.apply_events(events)
19
+ end
20
+
21
+ agg
22
+ end
23
+
24
+ def save_aggregate(aggregate)
25
+ changeset = aggregate.changeset
26
+ stream(aggregate.class, changeset.aggregate_id).write_events(changeset.events)
27
+ end
28
+
29
+ private
30
+
31
+ def stream_name(aggregate_klass, aggregate_id)
32
+ "#{aggregate_klass}#{STREAM_NAME_SEP}#{aggregate_id}"
33
+ end
34
+
35
+ def stream(aggregate_klass, aggregate_id)
36
+ @store.streams[stream_name(aggregate_klass, aggregate_id)]
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,17 @@
1
+ require_relative 'memory_event_store/stream'
2
+
3
+ module Akasha
4
+ module Storage
5
+ # Memory-based event store.
6
+ class MemoryEventStore
7
+ # Access to streams
8
+ # Example:
9
+ # store.streams['some-stream-name']
10
+ attr_reader :streams
11
+
12
+ def initialize
13
+ @streams = Hash.new { |streams, name| streams[name] = Stream.new }
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ module Akasha
2
+ module Storage
3
+ class MemoryEventStore
4
+ # Memory-based event stream.
5
+ class Stream
6
+ def initialize
7
+ @events = []
8
+ end
9
+
10
+ # Appends events to the stream.
11
+ def write_events(events)
12
+ @events += events
13
+ end
14
+
15
+ # Reads events from the stream starting from `position` inclusive.
16
+ # If block given, reads all events from the position in chunks
17
+ # of `size`.
18
+ # If block not given, reads `size` events from the position.
19
+ def read_events(position, size, &block)
20
+ if block_given?
21
+ @events.lazy.drop(position).each_slice(size, &block)
22
+ else
23
+ @events[position..position + size]
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module Akasha
2
+ VERSION='0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: akasha
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Marcin Bilski
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: timecop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A simple CQRS library for Ruby.
70
+ email:
71
+ - marcin@tooploox.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rubocop.yml"
78
+ - ".ruby-version"
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - akasha.gemspec
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/akasha.rb
88
+ - lib/akasha/aggregate.rb
89
+ - lib/akasha/aggregate/syntax_helpers.rb
90
+ - lib/akasha/changeset.rb
91
+ - lib/akasha/event.rb
92
+ - lib/akasha/repository.rb
93
+ - lib/akasha/storage/memory_event_store.rb
94
+ - lib/akasha/storage/memory_event_store/stream.rb
95
+ - lib/akasha/version.rb
96
+ homepage: https://github.com/bilus/akasha
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.6.11
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: CQRS library for Ruby
120
+ test_files: []