Class: Gamefic::Syntax::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/gamefic/syntax/template.rb

Overview

Template data for syntaxes.

Constant Summary collapse

PARAM_REGEXP =
/^:[a-z0-9_]+$/i.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Template

Returns a new instance of Template.



16
17
18
19
# File 'lib/gamefic/syntax/template.rb', line 16

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

Instance Attribute Details

#paramsArray<String> (readonly)

Returns:



14
15
16
# File 'lib/gamefic/syntax/template.rb', line 14

def params
  @params
end

#textString (readonly)

Returns:



11
12
13
# File 'lib/gamefic/syntax/template.rb', line 11

def text
  @text
end

Class Method Details

.to_template(tmpl_or_str) ⇒ Template

Parameters:

Returns:



47
48
49
50
51
# 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

Instance Method Details

#compare(other) ⇒ Object



37
38
39
40
41
42
43
# 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

#keywordsObject



21
22
23
# File 'lib/gamefic/syntax/template.rb', line 21

def keywords
  text.keywords
end

#regexpObject



29
30
31
# File 'lib/gamefic/syntax/template.rb', line 29

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

#to_sObject



25
26
27
# File 'lib/gamefic/syntax/template.rb', line 25

def to_s
  text
end

#verbObject



33
34
35
# File 'lib/gamefic/syntax/template.rb', line 33

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