Sha256: f68ae95a84462f2264cf7afd6c9499e82d0cb55c01ea0687d56568b672ab38a8

Contents?: true

Size: 909 Bytes

Versions: 1

Compression:

Stored size: 909 Bytes

Contents

# frozen_string_literal: true

module Qi
  # The Action abstraction.
  class Action
    CAPTURE_CHAR = "&"
    DROP_CHAR = "*"

    # Action initializer.
    def initialize(*captures, **squares)
      @captures = captures
      @squares  = squares
    end

    # Commit an action to the position.
    #
    # @return [Array] An action to change the position.
    def call(**diffs)
      captures = @captures
      squares  = @squares

      diffs.each do |k, v|
        case String(k)
        when CAPTURE_CHAR
          captures = capture(v, *captures)
        when DROP_CHAR
          captures = drop(v, *captures)
        else
          squares = squares.merge(k => v)
        end
      end

      [captures, squares.compact]
    end

    private

    def capture(item, *items)
      [item] + items
    end

    def drop(item, *items)
      items.delete_at(items.index(item))
      items
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
qi-10.0.0.beta1 lib/qi/action.rb