Class: SandthornSequelProjection::Projection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
SimpleMigrator::Migratable
Defined in:
lib/sandthorn_sequel_projection/projection.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db_connection = nil) ⇒ Projection

Returns a new instance of Projection.



13
14
15
16
17
18
19
20
# File 'lib/sandthorn_sequel_projection/projection.rb', line 13

def initialize(db_connection = nil)
  @db_connection = db_connection || SandthornSequelProjection.configuration.db_connection
  @tracker = ProcessedEventsTracker.new(
      identifier: identifier,
      db_connection: @db_connection,
      event_store: event_store)
  @event_handlers = self.class.event_handlers
end

Class Attribute Details

.event_handlersObject

Returns the value of attribute event_handlers.



48
49
50
# File 'lib/sandthorn_sequel_projection/projection.rb', line 48

def event_handlers
  @event_handlers
end

.event_store_nameObject (readonly)

Returns the value of attribute event_store_name.



33
34
35
# File 'lib/sandthorn_sequel_projection/projection.rb', line 33

def event_store_name
  @event_store_name
end

Instance Attribute Details

#db_connectionObject (readonly)

Returns the value of attribute db_connection.



11
12
13
# File 'lib/sandthorn_sequel_projection/projection.rb', line 11

def db_connection
  @db_connection
end

#event_handlersObject (readonly)

Returns the value of attribute event_handlers.



11
12
13
# File 'lib/sandthorn_sequel_projection/projection.rb', line 11

def event_handlers
  @event_handlers
end

#trackerObject (readonly)

Returns the value of attribute tracker.



11
12
13
# File 'lib/sandthorn_sequel_projection/projection.rb', line 11

def tracker
  @tracker
end

Class Method Details

.define_event_handlers {|@event_handlers| ... } ⇒ Object

Yields:



50
51
52
53
# File 'lib/sandthorn_sequel_projection/projection.rb', line 50

def define_event_handlers
  @event_handlers ||= EventHandlerCollection.new
  yield(@event_handlers)
end

.event_store(event_store = nil) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/sandthorn_sequel_projection/projection.rb', line 35

def event_store(event_store = nil)
  if event_store
    @event_store_name = event_store
  else
    find_event_store
  end
end

.find_event_storeObject



43
44
45
# File 'lib/sandthorn_sequel_projection/projection.rb', line 43

def find_event_store
  SandthornSequelProjection.find_event_store(@event_store_name)
end

.identifierObject



55
56
57
58
59
60
61
# File 'lib/sandthorn_sequel_projection/projection.rb', line 55

def identifier
  self.name.gsub(/::/, '_').
      gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
      gsub(/([a-z\d])([A-Z])/,'\1_\2').
      tr("-", "_").
      downcase
end

Instance Method Details

#migratorObject



28
29
30
# File 'lib/sandthorn_sequel_projection/projection.rb', line 28

def migrator
  SimpleMigrator.migrator(db_connection)
end

#update!Object



22
23
24
25
26
# File 'lib/sandthorn_sequel_projection/projection.rb', line 22

def update!
  tracker.process_events do |batch|
    event_handlers.handle(self, batch)
  end
end