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
64
65
# File 'lib/bade/runtime/block.rb', line 59

def call!(*args)
  if @block.nil?
    raise MissingBlockDefinitionError.new(name, :call)
  else
    render_binding.__buff.concat(@block.call(*args))
  end
end

#render(*args) ⇒ Object

— Rendering methods



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

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

#render!(*args) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/bade/runtime/block.rb', line 77

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