class Gamefic::Syntax::Template

Template data for syntaxes.

Constants

PARAM_REGEXP

Attributes

params[R]

@return [Array<String>]

text[R]

@return [String]

Public Class Methods

new(text) click to toggle source
# File lib/gamefic/syntax/template.rb, line 16
def initialize text
  @text = text.normalize
  @params = @text.keywords.select { |word| word.start_with?(':') }
end
to_template(tmpl_or_str) click to toggle source

@param tmpl_or_str [Template, String] @return [Template]

# File lib/gamefic/syntax/template.rb, line 47
def self.to_template tmpl_or_str
  return tmpl_or_str if tmpl_or_str.is_a?(Template)

  Template.new(tmpl_or_str)
end

Public Instance Methods

compare(other) click to toggle source
# File lib/gamefic/syntax/template.rb, line 37
def compare other
  if keywords.length == other.keywords.length
    other.verb <=> verb
  else
    other.keywords.length <=> keywords.length
  end
end
keywords() click to toggle source
# File lib/gamefic/syntax/template.rb, line 21
def keywords
  text.keywords
end
regexp() click to toggle source
# File lib/gamefic/syntax/template.rb, line 29
def regexp
  @regexp ||= Regexp.new("^#{make_tokens.join(' ')}$", Regexp::IGNORECASE)
end
to_s() click to toggle source
# File lib/gamefic/syntax/template.rb, line 25
def to_s
  text
end
verb() click to toggle source
# File lib/gamefic/syntax/template.rb, line 33
def verb
  @verb ||= Syntax.literal_or_nil(keywords.first)
end

Private Instance Methods

make_tokens() click to toggle source

@return [Array<String>]

# File lib/gamefic/syntax/template.rb, line 56
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