Sha256: d7794aff38ac5d01f1a587a04d50008dfc1bc329bb721babef6daca8af78e433

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

module Qi
  # Main class.
  class Store
    # @example Instanciate a store with 88 cells.
    #   new(88)
    #
    # @param size                 [Fixnum]      The number of cell.
    # @param deleted_content      [Object, nil] Deleted content.
    # @param options              [Hash]        A content per cell.
    def initialize(size, deleted_content = nil, options = {})
      @cells            = Array.new(size)
      @deleted_content  = deleted_content

      options.each do |cell, piece|
        @cells[cell] = piece
      end
    end

    # @!attribute [r] cells
    #
    # @return [Array] The cells in the store.
    attr_reader :cells

    # @!attribute [r] deleted_content
    #
    # @return [Object, nil] Deleted content.
    attr_reader :deleted_content

    # @param src_cell [Fixnum] Source cell.
    # @param dst_cell [Fixnum] Destination cell.
    # @param content  [Object] Content.
    #
    # @return [Store] The new store.
    def call(src_cell, dst_cell, content)
      h = contents
      h.delete(src_cell)
      deleted_content = h.delete(dst_cell)
      h[dst_cell] = content

      self.class.new(cells.length, deleted_content, h)
    end

    private

    # @return [Hash] The contents in the store.
    def contents
      Hash[[*cells.map.with_index]].invert
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
qi-4.0.0 lib/qi/store.rb