event_source 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f482b09497aecd4c9d777e9044129236d63492e4
4
- data.tar.gz: cd32bd6e04351a71e77aaf1c22e51e14f437dca1
3
+ metadata.gz: c19116c721153204680c617cdc457f7a25f6a2f2
4
+ data.tar.gz: e98c273e30762a3f39837da4acc5fa90aa3f8c04
5
5
  SHA512:
6
- metadata.gz: bef50c5d9b5fa8e3691da030a59cc002f1c3ea62794a5510b5c8318252d7704c6a07893077902073b02312f6ace48e2078b351dab3e009ed447847da72be7625
7
- data.tar.gz: 09fb34fd611b0a9146e913f3a512cac0afb31334d744bb31024db007889debc1c72382c8f53e5729c957006428ac76751b19d2fadfbcb25bb65221b90c7d7c0e
6
+ metadata.gz: 01fbce1dd57be53843a795d7af98dcb4eff52d685e9d80658bc266ab50730af8cd939fd2478cc87a77b485c20b62ca316e33d56a108cbf843e073d487e29e186
7
+ data.tar.gz: 9f0bc832b4cf6188110bd9e018be52c4120b1d67fa006698f90b161db09655213f897852aebb171f465971ed4913b4f0472bbb6fff5a20f774927539b8184853
data/README.md CHANGED
@@ -10,6 +10,12 @@ An event store must be initialized. Currently, only the in_memory SQLlite3 type
10
10
  EventSource::EventRepository.create(in_memory: true)
11
11
  ```
12
12
 
13
+ Once initialized, the event repository is memoized and can be retrieved with:
14
+
15
+ ```ruby
16
+ EventSource::EventRepository.current
17
+ ```
18
+
13
19
  ## Your entities
14
20
 
15
21
  An entity is an object that you intend to persist. You must extend and include some class methods and instance methods. Let's create an entity called BankAccount.
@@ -77,6 +83,8 @@ account = BankAccount.create
77
83
 
78
84
  This repository is used to store and load entities. It is also used to monitor changes to entities. When a transaction is created, all changes to all entities and all events created will be saved in the event repository.
79
85
 
86
+ The entity repository needs an instance of an event repository to work. If you decide to create your own instance of the entity repository, be sure to pass one as a parameter to the initialize method. Thankfully, however, since the event repository is memoize, the entity repository will simply call use it if you don't specify your own.
87
+
80
88
  ### Transactions
81
89
 
82
90
  ```ruby
@@ -93,9 +101,25 @@ To load an entity, you must create an entity repository and pass it an instance
93
101
 
94
102
  The 'find' method expects an entity type and the entity's unique identifier. The type, by convention, is simply a lowercase undescored string of the entity class name.
95
103
 
104
+ The following will use the default event and entity repositories. No need to create your own:
105
+
96
106
  ```ruby
97
- event_repo = EventSource::EventRepository.current
98
- entity_repo = EventSource::EntityRepository.new(event_repo)
107
+ entity_repo = EventSource::EntityRepository.current
99
108
  entity = entity_repo.find(:bank_account, uid)
100
109
  ```
101
110
 
111
+ Since the entity repository will automatically grab a memoize event repository, simply creating one ahead of time will ensure that it is picked up by the entity repository:
112
+
113
+ ```ruby
114
+ event_repo = EventSource::EventRepository.create(:in_memory => true)
115
+ entity_repo = EventSource::EntityRepository.current
116
+ entity = entity_repo.find(:bank_account, uid)
117
+ ```
118
+
119
+ Alternatively, you can instantiate everything yourself
120
+
121
+ ```ruby
122
+ event_repo = EventSource::EventRepository.create(:in_memory => true)
123
+ entity_repo = EventSource::EntityRepository.new(event_repo)
124
+ entity = entity_repo.find(:bank_account, uid)
125
+ ```
@@ -7,20 +7,15 @@ require 'active_support/inflector'
7
7
 
8
8
  module EventSource
9
9
  class EntityRepository
10
+ extend EventSource::MemoizeInstance
11
+
10
12
  attr_reader :entities
11
13
 
12
14
  class << self
13
- @@current = nil
14
-
15
15
  def transaction
16
- @@current = self.new
16
+ self.current.clear
17
17
  yield
18
- @@current.commit
19
- @@current = nil
20
- end
21
-
22
- def current
23
- @@current
18
+ self.current.commit
24
19
  end
25
20
  end
26
21
 
@@ -29,6 +24,10 @@ module EventSource
29
24
  @event_repo = event_repo
30
25
  end
31
26
 
27
+ def clear
28
+ @entities.clear
29
+ end
30
+
32
31
  def add(entity)
33
32
  @entities << entity
34
33
  end
@@ -36,6 +35,7 @@ module EventSource
36
35
  def commit
37
36
  # TODO: gather all events of all entities, maintain order and save in batch
38
37
  @entities.each {|e| e.save}
38
+ clear
39
39
  end
40
40
 
41
41
  def find(type, uid)
@@ -53,5 +53,11 @@ module EventSource
53
53
 
54
54
  entity
55
55
  end
56
+
57
+ private
58
+
59
+ def self.default_args
60
+ [EventSource::EventRepository.current]
61
+ end
56
62
  end
57
63
  end
@@ -2,15 +2,13 @@ require 'sequel'
2
2
 
3
3
  module EventSource
4
4
  class EventRepository
5
+ extend EventSource::MemoizeInstance
6
+
5
7
  attr_reader :db
6
8
 
7
9
  class << self
8
- def current
9
- @@instance ||= self.new(in_memory: true)
10
- end
11
-
12
10
  def create(options)
13
- @@instance = self.new(options)
11
+ @instance = self.new(options)
14
12
  end
15
13
  end
16
14
 
@@ -31,6 +29,10 @@ module EventSource
31
29
 
32
30
  private
33
31
 
32
+ def self.default_args
33
+ [in_memory: true]
34
+ end
35
+
34
36
  def initialize(options)
35
37
  if options[:in_memory]
36
38
  @db = Sequel.sqlite
@@ -0,0 +1,11 @@
1
+ module EventSource
2
+ module MemoizeInstance
3
+ def current
4
+ @instance ||= self.send(:new, *default_args())
5
+ end
6
+
7
+ def default_args
8
+ []
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module EventSource
2
- Version = '0.1.0'
2
+ Version = '0.1.1'
3
3
  end
data/lib/event_source.rb CHANGED
@@ -2,6 +2,7 @@ require './lib/event_source/exceptions'
2
2
  require './lib/event_source/uid_generator'
3
3
  require './lib/event_source/event'
4
4
  require './lib/event_source/entity'
5
+ require './lib/event_source/memoize_instance'
5
6
  require './lib/event_source/entity_repository'
6
7
  require './lib/event_source/event_repository'
7
8
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: event_source
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Louis Salin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-06 00:00:00.000000000 Z
11
+ date: 2013-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -82,6 +82,7 @@ files:
82
82
  - lib/event_source/entity_repository.rb
83
83
  - lib/event_source/version.rb
84
84
  - lib/event_source/exceptions.rb
85
+ - lib/event_source/memoize_instance.rb
85
86
  - LICENSE
86
87
  - README.md
87
88
  - CHANGELOG.md