class FrpEventsourcing::Stream

Constants

PersistentTypeMissingError
UnsupportedPersistentTypeError

Attributes

resource_type[R]
unique_resource_identifier[R]

Public Class Methods

merge(stream_one, stream_two) click to toggle source
# File lib/frp-eventsourcing/stream.rb, line 90
def self.merge(stream_one, stream_two)
  new(stream_one, stream_two)
end
new(*sources) click to toggle source
# File lib/frp-eventsourcing/stream.rb, line 24
def initialize(*sources)
  @resource_type = nil
  @unique_resource_identifier = nil

  sources.each do |source|
    source.add_observer(self)
  end
end

Public Instance Methods

as_persistent_type(resource_type, unique_resource_identifier = []) click to toggle source
# File lib/frp-eventsourcing/stream.rb, line 37
def as_persistent_type(resource_type, unique_resource_identifier = [])
  @resource_type =
    if resource_type.is_a?(String)
      Object.const_set(resource_type, Class.new(ActiveRecord::Base))
    elsif resource_type.is_a?(Symbol)
      Object.const_set(resource_type.to_s.capitalize, Class.new(ActiveRecord::Base))
    elsif resource_type < ActiveRecord::Base
      resource_type
    else
      raise(
        UnsupportedPersistentTypeError,
        'Supported persistent type is String, Symbol or any subclass of ActiveRecord::Base, '\
        "but given: #{resource_type}"
      )
    end

  @unique_resource_identifier =
    if unique_resource_identifier.any?
      unique_resource_identifier
    else
      "#{@resource_type.to_s.downcase}_id"
    end

  return self
end
collect(blk)
Alias for: map
each(blk) click to toggle source
# File lib/frp-eventsourcing/stream.rb, line 72
def each(blk)
  Each.new(self, blk)
end
filter(blk) click to toggle source
# File lib/frp-eventsourcing/stream.rb, line 81
def filter(blk)
  Filter.new(self, blk)
end
Also aliased as: select
init(blk) click to toggle source
# File lib/frp-eventsourcing/stream.rb, line 63
def init(blk)
  Init.new(self, blk)
end
Also aliased as: inject
inject(blk)
Alias for: init
map(blk) click to toggle source
# File lib/frp-eventsourcing/stream.rb, line 76
def map(blk)
  Map.new(self, blk)
end
Also aliased as: collect
merge(another_stream) click to toggle source
# File lib/frp-eventsourcing/stream.rb, line 86
def merge(another_stream)
  Stream.new(self, another_stream)
end
select(blk)
Alias for: filter
update(event) click to toggle source
# File lib/frp-eventsourcing/stream.rb, line 33
def update(event)
  occur(event)
end
when(event_type, blk) click to toggle source
# File lib/frp-eventsourcing/stream.rb, line 68
def when(event_type, blk)
  When.new(self, event_type, blk)
end

Protected Instance Methods

check_resource_type_presence() click to toggle source
# File lib/frp-eventsourcing/stream.rb, line 105
def check_resource_type_presence
  return if resource_type

  raise(
    PersistentTypeMissingError,
    'Persistent type missing. Make sure to call #as_persistent_type method on stream.'
  )
end
extract_entity_id(event) click to toggle source
# File lib/frp-eventsourcing/stream.rb, line 101
def extract_entity_id(event)
  event[:data].slice(*@unique_resource_identifier)
end
occur(value) click to toggle source
# File lib/frp-eventsourcing/stream.rb, line 96
def occur(value)
  changed
  notify_observers(value)
end