Sha256: dd01da0c57be75cb06667cb4249ab8b440d52ec1016309e80767dd8a6ebe862e

Contents?: true

Size: 1.29 KB

Versions: 10

Compression:

Stored size: 1.29 KB

Contents

require "socrates/adapters/adapter"
require "socrates/adapters/stubs"

module Socrates
  module Adapters
    class Memory
      include Socrates::Adapters::Adapter
      include StubUserDirectory

      CLIENT_ID = "MEMORY"
      CHANNEL   = "C1"

      attr_reader :history
      attr_accessor :client_id

      def initialize
        super()
        @history = Hash.new { |hash, key| hash[key] = [] }
      end

      def client_id_from(context: nil, user: nil)
        raise ArgumentError, "Must provide one of :context or :user" if context.nil? && user.nil?

        @client_id || CLIENT_ID
      end

      def channel_from(context: nil, user: nil)
        raise ArgumentError, "Must provide one of :context or :user" if context.nil? && user.nil?

        user.nil? ? CHANNEL : users_channel(user)
      end

      #
      # Methods for fetching messages and dms in specs...
      #

      def msgs
        @history[CHANNEL]
      end

      def last_msg
        msgs[-1]
      end

      def dms(user)
        @history[users_channel(user)]
      end

      def last_dm(user)
        dms(user)[-1]
      end

      private

      def send_message(channel, message)
        @history[channel] << message
      end

      def users_channel(user)
        user.respond_to?(:id) ? user.id : user
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
socrates-0.1.26 lib/socrates/adapters/memory.rb
socrates-0.1.25 lib/socrates/adapters/memory.rb
socrates-0.1.24 lib/socrates/adapters/memory.rb
socrates-0.1.23 lib/socrates/adapters/memory.rb
socrates-0.1.22 lib/socrates/adapters/memory.rb
socrates-0.1.21 lib/socrates/adapters/memory.rb
socrates-0.1.20 lib/socrates/adapters/memory.rb
socrates-0.1.19 lib/socrates/adapters/memory.rb
socrates-0.1.18 lib/socrates/adapters/memory.rb
socrates-0.1.17 lib/socrates/adapters/memory.rb