class HTMLComponent::Builder
Represents a String
being constructed, and can be more or less treated as a String
. Builder
creates a String
from whatever is put into it, but it delays construction until it's absolutely necessary, then it caches the result.
Public Class Methods
new(strings = [])
click to toggle source
Build a new string builder instance, immediately constructing and caching the initial value.
# File lib/html-native/builder.rb, line 10 def initialize(strings = []) @strings = [] @cache = if strings.kind_of? Array strings.join elsif strings.kind_of? Enumerable strings.to_a.join else strings.to_s end @cached = true end
Public Instance Methods
+(string)
click to toggle source
Appends a value to the Builder
instance. If it is another builder, it is added, but not converted to a String
yet. If it is an HTMLComponent
, it is rendered. If it is anything else, it is converted to a String
. This invalidates the cache.
# File lib/html-native/builder.rb, line 27 def +(string) if string.kind_of? Builder @strings << string elsif string.kind_of? HTMLComponent @strings << string.render else @strings << string.to_s end @cached = false self end
Also aliased as: <<
concat(*strings)
click to toggle source
Same as +, but allows multiple values to be appended.
# File lib/html-native/builder.rb, line 42 def concat(*strings) strings.each do |s| self + s end self end
method_missing(method, *args, &block)
click to toggle source
respond_to_missing?(method, include_all)
click to toggle source
to_s()
click to toggle source
Converts the Builder
to a String
. If the cache is valid, it is returned. Otherwise, the new result is created, cached, and returned.
# File lib/html-native/builder.rb, line 51 def to_s unless @cached @cache << @strings.join @strings.clear @cached = true end @cache end
Also aliased as: to_str