Class: Rcqrs::BaseEvent

Inherits:
Object
  • Object
show all
Defined in:
lib/rcqrs/base_event.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ BaseEvent

Returns a new instance of BaseEvent.



7
8
9
10
11
12
13
# File 'lib/rcqrs/base_event.rb', line 7

def initialize data = {}
  data.each do |k,v|
    raise "Property not defined" unless self.class.has_property(k)
    instance_variable_set("@#{k}",v)
  end
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/rcqrs/base_event.rb', line 5

def data
  @data
end

#published_atObject (readonly)

Returns the value of attribute published_at.



5
6
7
# File 'lib/rcqrs/base_event.rb', line 5

def published_at
  @published_at
end

Class Method Details

.has_property(property) ⇒ Object



26
27
28
29
# File 'lib/rcqrs/base_event.rb', line 26

def self.has_property(property)
  return false unless @properties
  @properties.include?(property.to_s)
end

.load_from(data, published_at) ⇒ Object



35
36
37
38
39
# File 'lib/rcqrs/base_event.rb', line 35

def self.load_from data, published_at
  event = self.new(data)
  event.instance_variable_set("@published_at",published_at)
  event
end

.property(*property_names) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/rcqrs/base_event.rb', line 15

def self.property(*property_names)
  property_names.each do |name|
    raise "data and published_at are not allowed property names" if (["data","published_at"].include? name.to_s)
  end
  property_names.each do |property_name|
    @properties = Array.new unless @properties
    @properties << property_name.to_s
    attr_reader property_name
  end
end

.restore_from(data, published_at) ⇒ Object



31
32
33
# File 'lib/rcqrs/base_event.rb', line 31

def self.restore_from(data, published_at)
  self.load_from(data, published_at)
end

Instance Method Details

#==(other) ⇒ Object



56
57
58
# File 'lib/rcqrs/base_event.rb', line 56

def == other
  @data == other.data
end

#[](index) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/rcqrs/base_event.rb', line 46

def [](index)
  if @data.has_key?(index)
    return @data[index]
  elsif @data.has_key?(index.to_s)
    return @data[index.to_s]
  elsif @data.has_key?(index.to_sym)
    return @data[index.to_sym]
  end
end

#store_publish_timeObject



41
42
43
44
# File 'lib/rcqrs/base_event.rb', line 41

def store_publish_time
  raise "Event has allready a publish date" if(@published_at != nil)
  @published_at = Time.now
end