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

.block ⇒ Object (readonly)

Returns the value of attribute block.



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

def block
  @block
end

.cmdlet ⇒ Object (readonly)

Returns the value of attribute cmdlet.



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

def cmdlet
  @cmdlet
end

.parameter_names ⇒ Object (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

.safe ⇒ Object (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

#block ⇒ Object (readonly)

Returns the value of attribute block.



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

def block
  @block
end

#cmdlet ⇒ Object (readonly)

Returns the value of attribute cmdlet.



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

def cmdlet
  @cmdlet
end

#parameter_names ⇒ Object (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

#safe ⇒ Object (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



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_proc ⇒ Object

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