Class: Realize::Format::StringTemplate

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

Instance Method Summary collapse

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

#expressionObject (readonly)

Returns the value of attribute expression.



23
24
25
# File 'lib/realize/format/string_template.rb', line 23

def expression
  @expression
end

#resolverObject (readonly)

Returns the value of attribute resolver.



23
24
25
# File 'lib/realize/format/string_template.rb', line 23

def resolver
  @resolver
end

#use_recordObject (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