Class: Burner::Util::StringTemplate
- Inherits:
-
Object
- Object
- Burner::Util::StringTemplate
- Includes:
- Singleton
- Defined in:
- lib/burner/util/string_template.rb
Overview
Can take in a string and an object and use the object for formatting string interpolations using tokens of form: attribute_name. This templating class does not understand nested structures, so input should be a flat object/hash in the form of key-value pairs. A benefit of using Objectable for resolution is that it can understand almost any type of object: Hash, Struct, OpenStruct, custom objects, etc. For more information see underlying libraries:
* Stringento: https://github.com/bluemarblepayroll/stringento
* Objectable: https://github.com/bluemarblepayroll/objectable
Instance Attribute Summary collapse
-
#resolver ⇒ Object
readonly
Returns the value of attribute resolver.
Instance Method Summary collapse
-
#evaluate(expression, input) ⇒ Object
For general consumption.
-
#initialize ⇒ StringTemplate
constructor
A new instance of StringTemplate.
-
#resolve(value, input) ⇒ Object
For Stringento consumption.
Constructor Details
#initialize ⇒ StringTemplate
Returns a new instance of StringTemplate.
25 26 27 28 29 |
# File 'lib/burner/util/string_template.rb', line 25 def initialize @resolver = Objectable.resolver(separator: '') freeze end |
Instance Attribute Details
#resolver ⇒ Object (readonly)
Returns the value of attribute resolver.
23 24 25 |
# File 'lib/burner/util/string_template.rb', line 23 def resolver @resolver end |
Instance Method Details
#evaluate(expression, input) ⇒ Object
For general consumption
32 33 34 |
# File 'lib/burner/util/string_template.rb', line 32 def evaluate(expression, input) Stringento.evaluate(expression, input, resolver: self) end |
#resolve(value, input) ⇒ Object
For Stringento consumption
37 38 39 |
# File 'lib/burner/util/string_template.rb', line 37 def resolve(value, input) resolver.get(input, value) end |