Class: Realize::Format::StringTemplate
- Inherits:
-
Object
- Object
- Realize::Format::StringTemplate
- Defined in:
- lib/realize/format/string_template.rb
Overview
Use an expression as a template and string interpolate it using the Stringento library. Stringento also uses Objectable to provide (optional) key-path notation for handling nested objects. For more information see underlying libraries:
* Stringento: https://github.com/bluemarblepayroll/stringento
* Objectable: https://github.com/bluemarblepayroll/objectable
Constant Summary collapse
- DEFAULT_SEPARATOR =
'.'
Instance Attribute Summary collapse
-
#expression ⇒ Object
readonly
Returns the value of attribute expression.
-
#resolver ⇒ Object
readonly
Returns the value of attribute resolver.
-
#use_record ⇒ Object
readonly
Returns the value of attribute use_record.
Instance Method Summary collapse
-
#initialize(expression: '', separator: DEFAULT_SEPARATOR, use_record: false) ⇒ StringTemplate
constructor
A new instance of StringTemplate.
-
#resolve(value, input) ⇒ Object
For Stringento consumption.
- #transform(_resolver, value, _time, record) ⇒ Object
Constructor Details
#initialize(expression: '', separator: DEFAULT_SEPARATOR, use_record: false) ⇒ StringTemplate
Returns a new instance of StringTemplate.
25 26 27 28 29 30 31 |
# File 'lib/realize/format/string_template.rb', line 25 def initialize(expression: '', separator: DEFAULT_SEPARATOR, use_record: false) @expression = expression.to_s @resolver = Objectable.resolver(separator: separator) @use_record = use_record || false freeze end |
Instance Attribute Details
#expression ⇒ Object (readonly)
Returns the value of attribute expression.
23 24 25 |
# File 'lib/realize/format/string_template.rb', line 23 def expression @expression end |
#resolver ⇒ Object (readonly)
Returns the value of attribute resolver.
23 24 25 |
# File 'lib/realize/format/string_template.rb', line 23 def resolver @resolver end |
#use_record ⇒ Object (readonly)
Returns the value of attribute use_record.
23 24 25 |
# File 'lib/realize/format/string_template.rb', line 23 def use_record @use_record end |
Instance Method Details
#resolve(value, input) ⇒ Object
For Stringento consumption
40 41 42 |
# File 'lib/realize/format/string_template.rb', line 40 def resolve(value, input) resolver.get(input, value) end |
#transform(_resolver, value, _time, record) ⇒ Object
33 34 35 36 37 |
# File 'lib/realize/format/string_template.rb', line 33 def transform(_resolver, value, _time, record) input = use_record ? record : value Stringento.evaluate(expression, input, resolver: self) end |