Class: Pakyow::Support::StringBuilder
- Inherits:
-
Object
- Object
- Pakyow::Support::StringBuilder
- Includes:
- SafeStringHelpers
- Defined in:
- lib/pakyow/support/string_builder.rb
Overview
Builds a string from a template.
Constant Summary collapse
- PATTERN =
/{([^}]*)}/
Instance Method Summary collapse
- #build(**values) ⇒ Object
-
#initialize(template, html_safe: false, &block) ⇒ StringBuilder
constructor
A new instance of StringBuilder.
Methods included from SafeStringHelpers
#ensure_html_safety, #html_escape, #html_safe, #html_safe?, #sanitize, #strip_tags
Constructor Details
#initialize(template, html_safe: false, &block) ⇒ StringBuilder
Returns a new instance of StringBuilder.
14 15 16 |
# File 'lib/pakyow/support/string_builder.rb', line 14 def initialize(template, html_safe: false, &block) @template, @html_safe, @block = template.to_s, html_safe, block end |
Instance Method Details
#build(**values) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/pakyow/support/string_builder.rb', line 18 def build(**values) @template.dup.tap do |working_template| working_template.scan(PATTERN).each do |match| value = if match[0].include?(".") object, property = match[0].split(".").map(&:to_sym) if object_value = get_value(object, values) ensure_real_value(object_value)[property] end else get_value(match[0].to_sym, values) end value = if @html_safe ensure_html_safety(value) else value.to_s end working_template.gsub!("{#{match[0]}}", value) end end end |