Class: Stringento::Template
- Inherits:
-
Object
- Object
- Stringento::Template
- Defined in:
- lib/stringento/template.rb
Overview
This is the main rendering engine for the library. It connects together the notions of: parsing, value resolution, and value formatting in a consumable way.
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #evaluate(input, resolver: Resolver.new, formatter: Formatter.new) ⇒ Object
-
#initialize(value) ⇒ Template
constructor
A new instance of Template.
- #placeholders ⇒ Object
Constructor Details
#initialize(value) ⇒ Template
Returns a new instance of Template.
20 21 22 |
# File 'lib/stringento/template.rb', line 20 def initialize(value) @value = value.to_s end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
18 19 20 |
# File 'lib/stringento/template.rb', line 18 def value @value end |
Instance Method Details
#evaluate(input, resolver: Resolver.new, formatter: Formatter.new) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/stringento/template.rb', line 30 def evaluate(input, resolver: Resolver.new, formatter: Formatter.new) placeholders.inject(value) do |output, placeholder| resolved_value = (resolver || default_resolver).resolve(placeholder.name, input) formatted_value = (formatter || default_formatter).formatter( placeholder.formatter, resolved_value, placeholder.arg ) output.gsub("{#{placeholder.value}}", formatted_value) end end |
#placeholders ⇒ Object
24 25 26 27 28 |
# File 'lib/stringento/template.rb', line 24 def placeholders @placeholders ||= value.scan(TOKEN_REGULAR_EXPRESSION) .flatten .map { |str| Placeholder.new(str) } end |