Sha256: 5f4305ab63e5b84fb961e72b220a97e034ea61017eac2072295de016c51d1750

Contents?: true

Size: 930 Bytes

Versions: 1

Compression:

Stored size: 930 Bytes

Contents

module WebsocketRails
  class DataStore
    def initialize(base_controller)
      @base = base_controller
      @data = Hash.new {|h,k| h[k] = Hash.new}
      @data = @data.with_indifferent_access
    end

    def []=(k,v)
      @data[cid] = Hash.new unless @data[cid]
      @data[cid][k] = v
    end

    def [](k)
      @data[cid][k] = Hash.new unless @data[cid]
      @data[cid][k]
    end

    def each(&block)
      @data.each do |cid,hash|
        block.call(hash) if block
      end
    end
    
    def remove_client
      @data.delete(cid)
    end
    
    def delete(key)
      @data[cid].delete(key)
    end
    
    def cid
      @base.client_id
    end
    
    def method_missing(method, *args, &block)
      if /each_(?<hash_key>\w*)/ =~ method
        results = []
        @data.each do |cid,hash|
          results << hash[hash_key]
        end
        results
      else
        super
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
websocket-rails-0.0.1 lib/websocket_rails/data_store.rb