Sha256: 81ce5b4046cc8f382ba4bdf8a3dcb7b5329c76a7bb3869ced1e897df652ebeab

Contents?: true

Size: 1.06 KB

Versions: 13

Compression:

Stored size: 1.06 KB

Contents

require 'delegate'

module Sandthorn
  class Event < SimpleDelegator
    ATTRS = %i(
      aggregate_id
      aggregate_type
      aggregate_version
      timestamp
      event_name
      event_args
      method_args
      trace
    )

    ATTRS.each do |attr|
      define_method(attr) do
        self[attr]
      end
    end

    def new_values
      @changed_attributes ||= build_new_values
    end

    def attribute_deltas
      @attribute_deltas ||= build_deltas
    end

    private

    def build_deltas
      raw_deltas.map { |delta| AttributeDelta.new(delta) }
    end

    def build_new_values
      attribute_deltas.each_with_object({}) do |delta, changed|
        changed[delta.attribute_name.to_sym] = delta.new_value
      end
    end

    def raw_deltas
      fetch(:event_args, {}).fetch(:attribute_deltas, [])
    end

    class AttributeDelta < SimpleDelegator
      ATTRS = %i(
        attribute_name
        old_value
        new_value
      )

      ATTRS.each do |attr|
        define_method(attr) do
          self[attr]
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
sandthorn-0.13.0 lib/sandthorn/event.rb
sandthorn-0.12.0 lib/sandthorn/event.rb
sandthorn-0.11.0 lib/sandthorn/event.rb
sandthorn-0.10.3 lib/sandthorn/event.rb
sandthorn-0.10.2 lib/sandthorn/event.rb
sandthorn-0.10.1 lib/sandthorn/event.rb
sandthorn-0.10.0 lib/sandthorn/event.rb
sandthorn-0.9.2 lib/sandthorn/event.rb
sandthorn-0.9.1 lib/sandthorn/event.rb
sandthorn-0.9.0 lib/sandthorn/event.rb
sandthorn-0.8.1 lib/sandthorn/event.rb
sandthorn-0.8.0 lib/sandthorn/event.rb
sandthorn-0.7.0 lib/sandthorn/event.rb