Module: EntityStore::Entity

Defined in:
lib/entity_store/entity.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/entity_store/entity.rb', line 3

def id
  @id
end

Class Method Details

.included(klass) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/entity_store/entity.rb', line 5

def self.included(klass)
  klass.class_eval do
    include HashSerialization
    include Attributes

    version_incremented_event_class = "#{self.name}VersionIncremented".split('::').inject(Object) {|obj, name|
        obj.const_defined?(name) ? obj.const_get(name) : obj.const_set(name, Class.new)
      }

    version_incremented_event_class.class_eval %Q"
      include ::EntityStore::Event

      attr_accessor :version

      def apply(entity)
        # nothing to do as signal event
      end
    "
  end
end

Instance Method Details

#apply_event(event) ⇒ Object



65
66
67
# File 'lib/entity_store/entity.rb', line 65

def apply_event(event)
  event.apply(self)
end

#clear_pending_eventsObject



56
57
58
# File 'lib/entity_store/entity.rb', line 56

def clear_pending_events
  @pending_events = []
end

#generate_version_incremented_eventObject



47
48
49
50
# File 'lib/entity_store/entity.rb', line 47

def generate_version_incremented_event
  event_class= "#{self.class.name}VersionIncremented".split('::').inject(Object) {|obj, name| obj.const_get(name) }
  event_class.new(:entity_id => id, :version => version)
end

#inspectObject



69
70
71
# File 'lib/entity_store/entity.rb', line 69

def inspect
  "<#{self.class.name} #{id} #{self.attributes.inspect}>"
end

#pending_eventsObject



52
53
54
# File 'lib/entity_store/entity.rb', line 52

def pending_events
  @pending_events ||= []
end

#record_event(event) ⇒ Object



60
61
62
63
# File 'lib/entity_store/entity.rb', line 60

def record_event(event)
  apply_event(event)
  pending_events<<event
end

#snapshot_due?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
# File 'lib/entity_store/entity.rb', line 39

def snapshot_due?
  if version % Config.snapshot_threshold == 0
    true
  else
    @_snapshot_version and (version - @_snapshot_version) >= Config.snapshot_threshold
  end
end

#typeObject



26
27
28
# File 'lib/entity_store/entity.rb', line 26

def type
  self.class.name
end

#versionObject



30
31
32
# File 'lib/entity_store/entity.rb', line 30

def version
  @_version ||= 1
end

#version=(value) ⇒ Object



34
35
36
37
# File 'lib/entity_store/entity.rb', line 34

def version=(value)
  @_snapshot_version = value unless @_snapshot_version
  @_version = value
end