Sha256: 85d67b9d1fea3c105da23e453b3691558f0a8264043ee25b80bd07df6e65a137

Contents?: true

Size: 1.04 KB

Versions: 15

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module Esse
  module Events
    class Event
      extend Forwardable
      def_delegators :@payload, :[], :fetch, :to_h, :key?
      alias_method :to_hash, :to_h

      attr_reader :id

      # Initialize a new event
      #
      # @param [Symbol, String] id The event identifier
      # @param [Hash] payload
      #
      # @return [Event]
      #
      # @api private
      def initialize(id, payload = {})
        @id = id
        @payload = payload
      end

      # Get or set a payload
      #
      # @overload
      #   @return [Hash] payload
      #
      # @overload payload(data)
      #   @param [Hash] data A new payload
      #   @return [Event] A copy of the event with the provided payload
      #
      # @api public
      def payload(data = nil)
        if data
          self.class.new(id, @payload.merge(data))
        else
          @payload
        end
      end

      # @api private
      def listener_method
        @listener_method ||= Hstring.new("on_#{id}").underscore.to_sym
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
esse-0.4.0.rc4 lib/esse/events/event.rb
esse-0.4.0.rc3 lib/esse/events/event.rb
esse-0.4.0.rc2 lib/esse/events/event.rb
esse-0.4.0.rc1 lib/esse/events/event.rb
esse-0.3.5 lib/esse/events/event.rb
esse-0.3.4 lib/esse/events/event.rb
esse-0.3.3 lib/esse/events/event.rb
esse-0.3.2 lib/esse/events/event.rb
esse-0.3.1 lib/esse/events/event.rb
esse-0.3.0 lib/esse/events/event.rb
esse-0.2.6 lib/esse/events/event.rb
esse-0.2.5 lib/esse/events/event.rb
esse-0.2.4 lib/esse/events/event.rb
esse-0.2.3 lib/esse/events/event.rb
esse-0.2.2 lib/esse/events/event.rb