class Stringento::Template
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.
Constants
- TOKEN_REGULAR_EXPRESSION
Attributes
value[R]
Public Class Methods
new(value)
click to toggle source
# File lib/stringento/template.rb, line 20 def initialize(value) @value = value.to_s end
Public Instance Methods
evaluate(input, resolver: Resolver.new, formatter: Formatter.new)
click to toggle source
# 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()
click to toggle source
# File lib/stringento/template.rb, line 24 def placeholders @placeholders ||= value.scan(TOKEN_REGULAR_EXPRESSION) .flatten .map { |str| Placeholder.new(str) } end