Class: Subtrigger::Template
- Inherits:
-
Object
- Object
- Subtrigger::Template
- Defined in:
- lib/subtrigger/template.rb
Overview
Reads, parses and manages inline templates.
When you define string templates at the end of your rules file, this class can parse and keep track of them. You can then easily retrieve them again and optionally format it like with String#%.
You simply define a new template using @@, followed by a name and then the textual contents (see the example below).
You can read the templates and use them, for example, in e-mails.
Constant Summary collapse
- TEMPLATE_DELIMITER =
The pattern that separates one template from another
/^@@ (.*)\n/- Unparseable =
Exception raised when a string cannot be parsed into templates, because the delimiter cannot be found
Class.new(Exception)
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
The unique identifier for this template.
-
#string ⇒ Object
readonly
The actual contents of the template.
Class Method Summary collapse
-
.find(name) ⇒ String
Finds and returns the content of the template by the given name.
-
.parse(string) ⇒ nil
Parse the contents of a string and extract templates from it.
Instance Method Summary collapse
-
#format(*args) ⇒ String
Get the contents of the template and interpolate any given arguments into it.
-
#initialize(name, string) ⇒ Template
constructor
A new instance of Template.
-
#to_s ⇒ String
Convert to string using the textual contents of the template.
Constructor Details
#initialize(name, string) ⇒ Template
Returns a new instance of Template.
92 93 94 |
# File 'lib/subtrigger/template.rb', line 92 def initialize(name, string) @name, @string = name, string end |
Instance Attribute Details
#name ⇒ Object (readonly)
The unique identifier for this template
35 36 37 |
# File 'lib/subtrigger/template.rb', line 35 def name @name end |
#string ⇒ Object (readonly)
The actual contents of the template
38 39 40 |
# File 'lib/subtrigger/template.rb', line 38 def string @string end |
Class Method Details
.find(name) ⇒ String
Finds and returns the content of the template by the given name.
67 68 69 70 71 |
# File 'lib/subtrigger/template.rb', line 67 def self.find(name) @children.find { |child| child.name == name } end |
.parse(string) ⇒ nil
Parse the contents of a string and extract templates from it. These are tracked so you can use find to retrieve them by name.
55 56 57 58 59 60 61 |
# File 'lib/subtrigger/template.rb', line 55 def self.parse(string) raise Unparseable, "Could not split into templates: #{string.inspect}" unless string =~ TEMPLATE_DELIMITER string.split(TEMPLATE_DELIMITER).map(&:chomp).slice(1..-1).each_slice(2) do |name, content| @children << new(name, content) end nil end |
Instance Method Details
#format(*args) ⇒ String
Get the contents of the template and interpolate any given arguments into it.
86 87 88 |
# File 'lib/subtrigger/template.rb', line 86 def format(*args) to_s % [*args] end |
#to_s ⇒ String
Convert to string using the textual contents of the template
75 76 77 |
# File 'lib/subtrigger/template.rb', line 75 def to_s string end |