Module: EventSourcedRecord::Event

Includes:
ActiveRecord::Immutable
Defined in:
lib/event_sourced_record/event.rb

Defined Under Namespace

Modules: ClassMethods Classes: EventTypeConfig, EventTypeImmutableError

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/event_sourced_record/event.rb', line 64

def method_missing(meth, *args, &block)
  if event_type_config && event_type_config.attributes.include?(meth)
    ensure_data
    self.data[meth.to_s]
  elsif event_type_config && event_type_config.attributes.any? { |a| "#{a}=".to_sym == meth }
    ensure_data
    attr = meth.to_s.gsub(/=$/, '')
    self.data[attr] = args.first
  else
    super
  end
end

Class Method Details

.included(model) ⇒ Object



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

def self.included(model)
  model.cattr_accessor :_event_type_configs
  model.extend ClassMethods
  model.after_initialize :ensure_data
  model.after_initialize :ensure_projection_uuid
  model.after_initialize :lock_event_type
  model.before_validation :ensure_occurred_at, on: :create
  model.validates :event_type, presence: true
  model.validate :validate_corrent_event_type
  model.validate :validate_by_event_type
end

Instance Method Details

#event_typeObject



18
19
20
# File 'lib/event_sourced_record/event.rb', line 18

def event_type
  attributes["event_type"]
end

#event_type=(value) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/event_sourced_record/event.rb', line 22

def event_type=(value)
  if @event_type_locked
    raise EventTypeImmutableError, "Event types can't be changed"
  else
    write_attribute(:event_type, value)
  end
end

#respond_to?(meth, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
# File 'lib/event_sourced_record/event.rb', line 30

def respond_to?(meth, include_all = false)
  if event_type_config && event_type_config.attributes.include?(meth)
    true
  elsif event_type_config && event_type_config.attributes.any? { |a| "#{a}=" == meth }
    true
  else
    super
  end
end