Class: Handlebarsjs::BaseHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/handlebarsjs/base_helper.rb

Overview

Extend base helper for each of your custom handlebars-helpers

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmdlet = nil, safe: nil, parameter_names: nil) ⇒ BaseHelper

Preferred way to register the internal command is via register_cmdlet but you can also register the command directly in the initializer and that can be handy if you use a custom configured cmdlet



14
15
16
17
18
19
# File 'lib/handlebarsjs/base_helper.rb', line 14

def initialize(cmdlet = nil, safe: nil, parameter_names: nil)
  initialize_cmdlet(cmdlet)
  initialize_safe(safe)
  initialize_block(block)
  initialize_parameter_names(parameter_names)
end

Class Attribute Details

.blockObject (readonly)

Returns the value of attribute block.



23
24
25
# File 'lib/handlebarsjs/base_helper.rb', line 23

def block
  @block
end

.cmdletObject (readonly)

Returns the value of attribute cmdlet.



22
23
24
# File 'lib/handlebarsjs/base_helper.rb', line 22

def cmdlet
  @cmdlet
end

.parameter_namesObject (readonly)

Returns the value of attribute parameter_names.



25
26
27
# File 'lib/handlebarsjs/base_helper.rb', line 25

def parameter_names
  @parameter_names
end

.safeObject (readonly)

Returns the value of attribute safe.



24
25
26
# File 'lib/handlebarsjs/base_helper.rb', line 24

def safe
  @safe
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



7
8
9
# File 'lib/handlebarsjs/base_helper.rb', line 7

def block
  @block
end

#cmdletObject (readonly)

Returns the value of attribute cmdlet.



6
7
8
# File 'lib/handlebarsjs/base_helper.rb', line 6

def cmdlet
  @cmdlet
end

#parameter_namesObject (readonly)

Returns the value of attribute parameter_names.



9
10
11
# File 'lib/handlebarsjs/base_helper.rb', line 9

def parameter_names
  @parameter_names
end

#safeObject (readonly)

Returns the value of attribute safe.



8
9
10
# File 'lib/handlebarsjs/base_helper.rb', line 8

def safe
  @safe
end

Class Method Details

.register_cmdlet(cmdlet, block: false, safe: false, parameter_names: []) ⇒ Object

Register the cmdlet to be used by the helper

By default, it will register an function expression helper You can use HTML by setting safe to true

Parameters:

  • cmdlet (Handlebarsjs::Cmdlet::Base)

    the cmdlet to be used by the helper

  • block (Boolean) (defaults to: false)

    whether or not this is a wrapped block

  • safe (Boolean) (defaults to: false)

    whether or not the cmdlet should return HTML

  • parameter_names (Array) (defaults to: [])

    the parameter names to be used by the cmdlet



36
37
38
39
40
41
# File 'lib/handlebarsjs/base_helper.rb', line 36

def register_cmdlet(cmdlet, block: false, safe: false, parameter_names: [])
  @cmdlet = cmdlet
  @block = block
  @safe = safe
  @parameter_names = parameter_names
end

Instance Method Details

#to_procObject

Wrap the cmdlet call method in a handlebars context aware block and return as a lambda/proc so that it is available to the Handlebars template engine



52
53
54
# File 'lib/handlebarsjs/base_helper.rb', line 52

def to_proc
  ->(value, _opts) { wrapper(cmdlet.call(value)) }
end

#wrapper(value) ⇒ Object

If you need to wrap the return value then you can override this method



45
46
47
# File 'lib/handlebarsjs/base_helper.rb', line 45

def wrapper(value)
  value
end