Sha256: aac898d11e475c537ff854d5cc6b1b461107ef47771308af02a5ad7429add9f2

Contents?: true

Size: 1.46 KB

Versions: 8

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module Gamefic
  class Syntax
    # Template data for syntaxes.
    #
    class Template
      PARAM_REGEXP = /^:[a-z0-9_]+$/i.freeze

      # @return [String]
      attr_reader :text

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

      def initialize text
        @text = text.normalize
        @params = @text.keywords.select { |word| word.start_with?(':') }
      end

      def keywords
        text.keywords
      end

      def to_s
        text
      end

      def regexp
        @regexp ||= Regexp.new("^#{make_tokens.join(' ')}$", Regexp::IGNORECASE)
      end

      def verb
        @verb ||= Syntax.literal_or_nil(keywords.first)
      end

      def compare other
        if keywords.length == other.keywords.length
          other.verb <=> verb
        else
          other.keywords.length <=> keywords.length
        end
      end

      # @param tmpl_or_str [Template, String]
      # @return [Template]
      def self.to_template tmpl_or_str
        return tmpl_or_str if tmpl_or_str.is_a?(Template)

        Template.new(tmpl_or_str)
      end

      private

      # @return [Array<String>]
      def make_tokens
        keywords.map.with_index do |word, idx|
          next word unless word.match?(PARAM_REGEXP)

          next nil if idx.positive? && keywords[idx - 1].match?(PARAM_REGEXP)

          '([\w\W\s\S]*?)'
        end.compact
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
gamefic-3.6.0 lib/gamefic/syntax/template.rb
gamefic-3.5.0 lib/gamefic/syntax/template.rb
gamefic-3.4.0 lib/gamefic/syntax/template.rb
gamefic-3.3.0 lib/gamefic/syntax/template.rb
gamefic-3.2.1 lib/gamefic/syntax/template.rb
gamefic-3.2.0 lib/gamefic/syntax/template.rb
gamefic-3.1.0 lib/gamefic/syntax/template.rb
gamefic-3.0.0 lib/gamefic/syntax/template.rb