Class: Bade::Runtime::Block
Direct Known Subclasses
Defined Under Namespace
Classes: MissingBlockDefinitionError
Instance Attribute Summary collapse
- #block ⇒ Proc readonly
- #location ⇒ RenderBinding::Location? readonly
- #name ⇒ String readonly
- #render_binding ⇒ RenderBinding readonly
Instance Method Summary collapse
-
#__call ⇒ Void
Calls the block and adds rendered content into current buffer stack.
-
#call ⇒ Void
Calls the block and adds rendered content into current buffer stack.
-
#call! ⇒ Void
Calls the block and adds rendered content into current buffer stack.
-
#initialize(name, location, render_binding, &block) ⇒ Block
constructor
A new instance of Block.
-
#render(*args) ⇒ String
Calls the block and returns rendered content in string.
-
#render!(*args) ⇒ String
Calls the block and returns rendered content in string.
Constructor Details
#initialize(name, location, render_binding, &block) ⇒ Block
Returns a new instance of Block.
52 53 54 55 56 57 |
# File 'lib/bade/runtime/block.rb', line 52 def initialize(name, location, render_binding, &block) @name = name @location = location @render_binding = render_binding @block = block end |
Instance Attribute Details
#block ⇒ Proc (readonly)
33 34 35 |
# File 'lib/bade/runtime/block.rb', line 33 def block @block end |
#location ⇒ RenderBinding::Location? (readonly)
41 42 43 |
# File 'lib/bade/runtime/block.rb', line 41 def location @location end |
#render_binding ⇒ RenderBinding (readonly)
45 46 47 |
# File 'lib/bade/runtime/block.rb', line 45 def render_binding @render_binding end |
Instance Method Details
#__call ⇒ Void
Calls the block and adds rendered content into current buffer stack.
111 112 113 114 115 116 117 118 119 |
# File 'lib/bade/runtime/block.rb', line 111 ruby2_keywords def __call(*args) loc = location.dup render_binding.__buffs_push(loc) @block.call(*args) res = render_binding.__buffs_pop render_binding.__buff&.concat(res) if !res.nil? && !res.empty? end |
#call ⇒ Void
Calls the block and adds rendered content into current buffer stack.
64 65 66 |
# File 'lib/bade/runtime/block.rb', line 64 ruby2_keywords def call(*args) call!(*args) unless @block.nil? end |
#call! ⇒ Void
Calls the block and adds rendered content into current buffer stack.
71 72 73 74 75 |
# File 'lib/bade/runtime/block.rb', line 71 ruby2_keywords def call!(*args) raise MissingBlockDefinitionError.new(name, :call, nil, render_binding.__location_stack) if @block.nil? __call(*args) end |
#render(*args) ⇒ String
Calls the block and returns rendered content in string.
Returns empty string when there is no block.
84 85 86 87 88 89 90 |
# File 'lib/bade/runtime/block.rb', line 84 def render(*args) if @block.nil? '' else render!(*args) end end |
#render!(*args) ⇒ String
Calls the block and returns rendered content in string.
Throws error when there is no block.
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/bade/runtime/block.rb', line 97 def render!(*args) raise MissingBlockDefinitionError.new(name, :render, nil, render_binding.__location_stack) if @block.nil? loc = location.dup render_binding.__buffs_push(loc) @block.call(*args) render_binding.__buffs_pop&.join || '' end |