Sha256: 8a1a3f8414bf9d54fed80cf341cf5615cca112ff244d1eacfdeb8d354d78d4a5

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

module Cubism
  class CubicleBlockStore
    delegate_missing_to :@blocks

    def initialize
      @blocks = Kredis.hash "cubism-blocks"
    end

    def [](key)
      Marshal.load(@blocks[key]) if @blocks[key]
    end

    def []=(key, value)
      mutex.synchronize do
        @blocks[key] = Marshal.dump value
      end
    end

    def clear
      mutex.synchronize do
        # kredis #remove
        @blocks.remove
      end
    end

    def size
      @blocks.to_h.size
    end

    private

    def mutex
      @mutex ||= Mutex.new
    end
  end

  BlockStoreItem = Struct.new(:block_location, :block_source, :user_gid, :resource_gid, keyword_init: true) do
    def user
      GlobalID::Locator.locate self[:user_gid]
    end

    def resource
      GlobalID::Locator.locate self[:resource_gid]
    end

    def marshal_dump
      to_h
    end

    def marshal_load(serialized_item)
      %i[block_location block_source user_gid resource_gid].each do |arg|
        send("#{arg}=", serialized_item[arg])
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cubism-0.1.0.pre12 lib/cubism/cubicle_block_store.rb~
cubism-0.1.0.pre11 lib/cubism/cubicle_block_store.rb~
cubism-0.1.0.pre10 lib/cubism/cubicle_block_store.rb