Class: RubyEventStore::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_event_store/record.rb

Constant Summary collapse

StringsRequired =
Class.new(StandardError)
BIG_VALUE =
0b110011100100000010010010110011101011110101010101001100111110011

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_id:, data:, metadata:, event_type:, timestamp:, valid_at:) ⇒ Record

Returns a new instance of Record.

Raises:


6
7
8
9
10
11
12
13
14
15
16
# File 'lib/ruby_event_store/record.rb', line 6

def initialize(event_id:, data:, metadata:, event_type:, timestamp:, valid_at:)
  raise StringsRequired unless [event_id, event_type].all? { |v| v.instance_of?(String) }
  @event_id   = event_id
  @data       = data
  @metadata   = 
  @event_type = event_type
  @timestamp  = timestamp
  @valid_at   = valid_at
  @serialized_records = {}
  freeze
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.


18
19
20
# File 'lib/ruby_event_store/record.rb', line 18

def data
  @data
end

#event_idObject (readonly)

Returns the value of attribute event_id.


18
19
20
# File 'lib/ruby_event_store/record.rb', line 18

def event_id
  @event_id
end

#event_typeObject (readonly)

Returns the value of attribute event_type.


18
19
20
# File 'lib/ruby_event_store/record.rb', line 18

def event_type
  @event_type
end

#metadataObject (readonly)

Returns the value of attribute metadata.


18
19
20
# File 'lib/ruby_event_store/record.rb', line 18

def 
  @metadata
end

#timestampObject (readonly)

Returns the value of attribute timestamp.


18
19
20
# File 'lib/ruby_event_store/record.rb', line 18

def timestamp
  @timestamp
end

#valid_atObject (readonly)

Returns the value of attribute valid_at.


18
19
20
# File 'lib/ruby_event_store/record.rb', line 18

def valid_at
  @valid_at
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?


33
34
35
36
37
38
39
40
41
# File 'lib/ruby_event_store/record.rb', line 33

def ==(other)
  other.instance_of?(self.class) &&
    other.event_id.eql?(event_id) &&
    other.data.eql?(data) &&
    other..eql?() &&
    other.event_type.eql?(event_type) &&
    other.timestamp.eql?(timestamp) &&
    other.valid_at.eql?(valid_at)
end

#hashObject


21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ruby_event_store/record.rb', line 21

def hash
  [
    self.class,
    event_id,
    data,
    ,
    event_type,
    timestamp,
    valid_at,
  ].hash ^ BIG_VALUE
end

#serialize(serializer) ⇒ Object


54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ruby_event_store/record.rb', line 54

def serialize(serializer)
  @serialized_records[serializer] ||=
    SerializedRecord.new(
      event_id:   event_id,
      event_type: event_type,
      data:       serializer.dump(data),
      metadata:   serializer.dump(),
      timestamp:  timestamp.iso8601(TIMESTAMP_PRECISION),
      valid_at:   valid_at.iso8601(TIMESTAMP_PRECISION),
    )
end

#to_hObject


43
44
45
46
47
48
49
50
51
52
# File 'lib/ruby_event_store/record.rb', line 43

def to_h
  {
    event_id: event_id,
    data: data,
    metadata: ,
    event_type: event_type,
    timestamp: timestamp,
    valid_at: valid_at,
  }
end