Class: Burner::Util::StringTemplate

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

Instance Method Summary collapse

Constructor Details

#initializeStringTemplate

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

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