Class: Bade::Runtime::Block

Inherits:
Object show all
Defined in:
lib/bade/runtime/block.rb

Direct Known Subclasses

Mixin

Defined Under Namespace

Classes: MissingBlockDefinitionError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, location, render_binding, &block) ⇒ Block

Returns a new instance of Block.

Parameters:



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

#blockProc (readonly)

Returns:

  • (Proc)


33
34
35
# File 'lib/bade/runtime/block.rb', line 33

def block
  @block
end

#locationRenderBinding::Location? (readonly)

Returns:



41
42
43
# File 'lib/bade/runtime/block.rb', line 41

def location
  @location
end

#nameString (readonly)

Returns:



37
38
39
# File 'lib/bade/runtime/block.rb', line 37

def name
  @name
end

#render_bindingRenderBinding (readonly)

Returns:



45
46
47
# File 'lib/bade/runtime/block.rb', line 45

def render_binding
  @render_binding
end

Instance Method Details

#__callVoid

Calls the block and adds rendered content into current buffer stack.

Returns:

  • (Void)


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

#callVoid

Calls the block and adds rendered content into current buffer stack.

Returns:

  • (Void)


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.

Returns:

  • (Void)


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.

Returns:



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.

Returns:

Raises:



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