Sha256: 1c6421b87e5f8af6f6acc3b87ee75a9c00172746a68aaa729611b1c0ae9adf0e

Contents?: true

Size: 1004 Bytes

Versions: 1

Compression:

Stored size: 1004 Bytes

Contents

module Qi
  # Main class.
  class Store
    # @param size    [Fixnum] The number of cell.
    # @param options [Hash]   A content per cell.
    def initialize(size, options = {})
      @cells = Array.new(size)

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

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

    # @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

      new_store = self.class.new(cells.length, h)

      Result.new(new_store, deleted_content)
    end

    private

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

require_relative 'result'

Version data entries

1 entries across 1 versions & 1 rubygems

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