Class: Subtrigger::Template

Inherits:
Object
  • Object
show all
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.

Examples:

Defining templates

# at the end of your Ruby file:
 __END__
@@ Template 1
Foo
@@ Template 2
Hello, s!

Parsing templates

Template.parse(__DATA__.read)

Using templates

Template.find('Template 1') # => 'Foo'

Formatting templates

Template.find('Template 2') # => 'Hello, %s!'
Template.find('Template 2').format('world') # => 'Hello, world!'

Author:

  • Arjan van der Gaag

Since:

  • 0.3.0

Constant Summary collapse

TEMPLATE_DELIMITER =

The pattern that separates one template from another

Since:

  • 0.3.0

/^@@ (.*)\n/
Unparseable =

Exception raised when a string cannot be parsed into templates, because the delimiter cannot be found

Since:

  • 0.3.0

Class.new(Exception)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, string) ⇒ Template

Returns a new instance of Template.

Parameters:

  • name (String)

    is the unique identifier of a template

  • string (String)

    is the contents of the template.

Since:

  • 0.3.0



92
93
94
# File 'lib/subtrigger/template.rb', line 92

def initialize(name, string)
  @name, @string = name, string
end

Instance Attribute Details

#nameObject (readonly)

The unique identifier for this template

Since:

  • 0.3.0



35
36
37
# File 'lib/subtrigger/template.rb', line 35

def name
  @name
end

#stringObject (readonly)

The actual contents of the template

Since:

  • 0.3.0



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.

Parameters:

  • name (String)

    is the name of the template

Returns:

  • (String)

    is Template#content

Since:

  • 0.3.0



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.

Parameters:

  • the (String)

    contents of your rules file’s __DATA__.read

Returns:

  • (nil)

Raises:

Since:

  • 0.3.0



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.

Examples:

Getting a template and using interpolation

template.to_s           # => 'Dear %s...'
template.format 'John'  # => 'Dear John...'

Returns:

  • (String)

    the formatted template contents

Since:

  • 0.3.0



86
87
88
# File 'lib/subtrigger/template.rb', line 86

def format(*args)
  to_s % [*args]
end

#to_sString

Convert to string using the textual contents of the template

Returns:

  • (String)

Since:

  • 0.3.0



75
76
77
# File 'lib/subtrigger/template.rb', line 75

def to_s
  string
end