Sha256: 16ec2b099189d023fc60f0a740e34e29aa04a71987df125668cb66655633c87c

Contents?: true

Size: 711 Bytes

Versions: 6

Compression:

Stored size: 711 Bytes

Contents

# frozen_string_literal: true

module Gamefic
  # A tokenization of an input from available syntaxes.
  #
  class Expression
    # @return [Symbol]
    attr_reader :verb

    # @return [Array<String>]
    attr_reader :tokens

    # @param verb [Symbol, nil]
    # @param tokens [Array<String>]
    def initialize verb, tokens
      @verb = verb
      @tokens = tokens
    end

    # Compare two syntaxes for the purpose of ordering them by relevance while
    # dispatching.
    #
    def compare other
      if verb == other.verb
        other.tokens.compact.length <=> tokens.compact.length
      else
        (other.verb ? 1 : 0) <=> (verb ? 1 : 0)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
gamefic-3.5.0 lib/gamefic/expression.rb
gamefic-3.4.0 lib/gamefic/expression.rb
gamefic-3.3.0 lib/gamefic/expression.rb
gamefic-3.2.1 lib/gamefic/expression.rb
gamefic-3.2.0 lib/gamefic/expression.rb
gamefic-3.1.0 lib/gamefic/expression.rb