Class: General::GTemplate
- Inherits:
-
GBaseTemplate
- Object
- GBaseTemplate
- General::GTemplate
- Defined in:
- lib/gtemplates/gtemplate.rb
Overview
Created: 3 - 4 - 2016
Instance Method Summary collapse
-
#initialize(string) ⇒ GTemplate
constructor
Creates a GTemplate with the given template string.
-
#match(string) ⇒ Object
Matches the given string against the template and returns the collected information.
-
#regex(sub = false) ⇒ Object
Returns the string as a regex.
Methods inherited from GBaseTemplate
Constructor Details
#initialize(string) ⇒ GTemplate
Creates a GTemplate with the given template string
Parameter: string - the string being converted to a template
37 38 39 40 41 42 43 44 45 |
# File 'lib/gtemplates/gtemplate.rb', line 37 def initialize string super(string, [ General::GText, General::GSpecial, General::GArrayPlaceholder, General::GPlaceholder, General::GFullPlaceholder ]) end |
Instance Method Details
#match(string) ⇒ Object
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
73 74 75 76 77 78 79 80 81 |
# 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) ⇒ Object
Returns the string as a regex
Returns: the string as a regex
50 51 52 53 54 55 56 57 |
# 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 |