class General::GTemplate

Implements the general templating system for strings

Author: Anshul Kharbanda Created: 3 - 4 - 2016

Public Class Methods

new(string) click to toggle source

Creates a GTemplate with the given template string

Parameter: string - the string being converted to a template

Calls superclass method General::GBaseTemplate::new
# File lib/gtemplates/gtemplate.rb, line 37
def initialize string
        super(string, [
                General::GText,
                General::GSpecial,
                General::GArrayPlaceholder,
                General::GPlaceholder,
                General::GFullPlaceholder
        ])
end

Public Instance Methods

match(string) { |hash| ... } click to toggle source

Matches the given string against the template and returns the collected information. Returns nil if the given string does not match.

If a block is given, it will be run with the generated hash if the string matches. Alias for:

if m = template.match(string)

# Run block

end

Parameter: string the string to match

Return: Information matched from the string or nil

# File lib/gtemplates/gtemplate.rb, line 73
def match string
        regex.match(string) do |match|
                hash = match.names.collect { |name|
                        [name.to_sym, match[name.to_sym]]
                }.to_h
                yield hash if block_given?
                return hash
        end
end
regex(sub=false) click to toggle source

Returns the string as a regex

Returns: the string as a regex

# File lib/gtemplates/gtemplate.rb, line 50
def regex sub=false
        first = Hash.new(true); str = ""
        @partials.each do |part|
                str += part.regex(first[part.name]);
                first[part.name] &&= false
        end
        return sub ? str : Regexp.new("\\A" + str + "\\z")
end