class Handlebars::Helpers::RegisterHelpers

Register helpers against handlebars context

Attributes

handlebars[RW]
helpers[R]

Public Class Methods

new(&block) click to toggle source

def initialize(handlebars = Handlebars::Context.new, &block)

# File lib/handlebars/helpers/register_helpers.rb, line 15
def initialize(&block)
  @handlebars = Handlebars::Context.new

  if block_given?
    block.call(self) # , handlebars)
  else
    register_from_config
  end
end

Public Instance Methods

helper(*names, &helper) click to toggle source
# File lib/handlebars/helpers/register_helpers.rb, line 25
def helper(*names, &helper)
  names.each do |name|
    handlebars.register_helper(name, &helper)
  end
end
register_from_config() click to toggle source

Register from configuration file

Sample configuration “groups”: [

{
  "name": "string_formatting",
  "base_require": "handlebars/helpers/string_formatting",
  "base_namespace": "Handlebars::Helpers::StringFormatting",
  "description": "Case modification string manipulation methods",
  "helpers": [
    {
      "name": "camel",
      "description": "convert to camel case with first word uppercase and following words uppercase",
      "aliases": ["camel", "camelUpper", "camelU"],
      "require_path": "handlebars/helpers/string_formatting/camel",
      "class_namespace": "Handlebars::Helpers::StringFormatting::Camel"
    }
  ]
}

]

# File lib/handlebars/helpers/register_helpers.rb, line 51
def register_from_config
  file = File.read Handlebars::Helpers.configuration.helper_config_file
  config = JSON.parse(file)

  config['groups'].each do |group|
    group['helpers'].each do |helper_config|
      register_config_item(helper_config)
    end
  end
end

Private Instance Methods

register_config_item(config) click to toggle source
# File lib/handlebars/helpers/register_helpers.rb, line 64
def register_config_item(config)
  require config['require_path']

  helper_instance = Object.const_get(config['class_namespace']).new

  helper(*config['aliases'].map(&:to_sym), &helper_instance.handlebars_helper)
end