Class: Bade::Runtime::Block

Inherits:
Object
  • 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, render_binding, &block) ⇒ Block

Returns a new instance of Block.

Parameters:

  • name (String)

    name of the block

  • render_binding (RenderBinding)

    reference to current binding instance

  • block (Proc)

    reference to lambda



47
48
49
50
51
# File 'lib/bade/runtime/block.rb', line 47

def initialize(name, render_binding, &block)
  @name = name
  @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

#nameString (readonly)

Returns:



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

def name
  @name
end

#render_bindingRenderBinding (readonly)

Returns:



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

def render_binding
  @render_binding
end

Instance Method Details

#call(*args) ⇒ Object

— Calling methods



55
56
57
# File 'lib/bade/runtime/block.rb', line 55

def call(*args)
  call!(*args) unless @block.nil?
end

#call!(*args) ⇒ Object



59
60
61
62
63
# File 'lib/bade/runtime/block.rb', line 59

def call!(*args)
  raise MissingBlockDefinitionError.new(name, :call) if @block.nil?

  render_binding.__buff.concat(@block.call(*args))
end

#render(*args) ⇒ Object

— Rendering methods



67
68
69
70
71
72
73
# File 'lib/bade/runtime/block.rb', line 67

def render(*args)
  if @block.nil?
    ''
  else
    render!(*args)
  end
end

#render!(*args) ⇒ Object



75
76
77
78
79
# File 'lib/bade/runtime/block.rb', line 75

def render!(*args)
  raise MissingBlockDefinitionError.new(name, :render) if @block.nil?

  @block.call(*args).join
end