Sha256: cc036ed6d76241cb7c6cd2f020f94cc5010932866c32fac16b75787aa5f34820

Contents?: true

Size: 1.04 KB

Versions: 11

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module Handlebarsjs
  # Extend base helper for each of your custom handlebars-helpers
  class BaseHelper
    attr_reader :cmdlet

    # 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
    def initialize(cmdlet = nil)
      @cmdlet = cmdlet
      @cmdlet = self.class.cmdlet.new if @cmdlet.nil? && self.class.cmdlet
    end

    class << self
      attr_reader :cmdlet

      def register_cmdlet(cmdlet)
        @cmdlet = cmdlet
      end
    end

    # If you need to wrap the return value in a specific
    # Handlebars Type, eg. SafeString, then you can override this method
    def wrapper(value)
      value
    end

    # 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
    def to_proc
      ->(value, _opts) { wrapper(cmdlet.call(value)) }
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
handlebarsjs-0.7.0 lib/handlebarsjs/base_helper.rb
handlebarsjs-0.6.3 lib/handlebarsjs/base_helper.rb
handlebarsjs-0.6.2 lib/handlebarsjs/base_helper.rb
handlebarsjs-0.6.1 lib/handlebarsjs/base_helper.rb
handlebarsjs-0.6.0 lib/handlebarsjs/base_helper.rb
handlebarsjs-0.5.7 lib/handlebarsjs/base_helper.rb
handlebarsjs-0.5.6 lib/handlebarsjs/base_helper.rb
handlebarsjs-0.5.5 lib/handlebarsjs/base_helper.rb
handlebarsjs-0.5.4 lib/handlebarsjs/base_helper.rb
handlebarsjs-0.5.3 lib/handlebarsjs/base_helper.rb
handlebarsjs-0.5.2 lib/handlebarsjs/base_helper.rb