Class: HTMLComponent::Builder
- Inherits:
-
Object
- Object
- HTMLComponent::Builder
- Defined in:
- lib/html-native/builder.rb,
lib/html-native/logic.rb
Overview
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.
Instance Method Summary collapse
-
#+(string) ⇒ Object
(also: #<<)
Appends a value to the Builder instance.
-
#concat(*strings) ⇒ Object
Same as +, but allows multiple values to be appended.
-
#initialize(strings = []) ⇒ Builder
constructor
Build a new string builder instance, immediately constructing and caching the initial value.
-
#method_missing(method, *args, &block) ⇒ Object
If the method does not exist on Builder, it is sent to String, by way of the rendered Builder result.
-
#respond_to_missing?(method, include_all) ⇒ Boolean
If String responds to the method, then Builder also responds to it.
-
#to_s ⇒ Object
(also: #to_str)
Converts the Builder to a String.
Constructor Details
#initialize(strings = []) ⇒ Builder
Build a new string builder instance, immediately constructing and caching the initial value.
10 11 12 13 14 15 16 17 18 19 20 21 |
# 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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
If the method does not exist on Builder, it is sent to String, by way of the rendered Builder result. Modify-in-place methods will affect the underlying String.
65 66 67 |
# File 'lib/html-native/builder.rb', line 65 def method_missing(method, *args, &block) to_s.send(method, *args, &block) end |
Instance Method Details
#+(string) ⇒ Object Also known as: <<
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.
27 28 29 30 31 32 33 34 35 36 37 |
# 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 |
#concat(*strings) ⇒ Object
Same as +, but allows multiple values to be appended.
42 43 44 45 46 47 |
# File 'lib/html-native/builder.rb', line 42 def concat(*strings) strings.each do |s| self + s end self end |
#respond_to_missing?(method, include_all) ⇒ Boolean
If String responds to the method, then Builder also responds to it.
70 71 72 |
# File 'lib/html-native/builder.rb', line 70 def respond_to_missing?(method, include_all) "".respond_to?(method, include_all) end |
#to_s ⇒ Object Also known as: to_str
Converts the Builder to a String. If the cache is valid, it is returned. Otherwise, the new result is created, cached, and returned.
51 52 53 54 55 56 57 58 |
# File 'lib/html-native/builder.rb', line 51 def to_s unless @cached @cache << @strings.join @strings.clear @cached = true end @cache end |