Sha256: 8dfe9ba84280520276fc2d26f1a6d89c331039c9d58f29a993e89f7eda835e5c

Contents?: true

Size: 1.3 KB

Versions: 37

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module Synvert::Core
  # Helper is used to define shared snippet.
  class Helper
    attr_reader :name, :block

    class << self
      # Register a helper with its name.
      #
      # @param name [String] the unique rewriter name.
      # @param helper [Synvert::Core::Helper] the helper to register.
      def register(name, helper)
        name = name.to_s
        helpers[name] = helper
      end

      # Fetch a helper by name.
      #
      # @param name [String] rewrtier name.
      # @return [Synvert::Core::Helper] the matching helper.
      def fetch(name)
        name = name.to_s
        helpers[name]
      end

      # Get all available helpers
      #
      # @return [Hash<String, Synvert::Core::Helper>]
      def availables
        helpers
      end

      # Clear all registered helpers.
      def clear
        helpers.clear
      end

      private

      def helpers
        @helpers ||= {}
      end
    end

    # Initialize a Helper.
    # When a helper is initialized, it is already registered.
    #
    # @param name [String] name of the helper.
    # @yield defines the behaviors of the helper, block code won't be called when initialization.
    def initialize(name, &block)
      @name = name
      @block = block
      self.class.register(name, self)
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
synvert-core-2.2.0 lib/synvert/core/helper.rb
synvert-core-2.1.2 lib/synvert/core/helper.rb
synvert-core-2.1.1 lib/synvert/core/helper.rb
synvert-core-2.1.0 lib/synvert/core/helper.rb
synvert-core-2.0.3 lib/synvert/core/helper.rb
synvert-core-2.0.2 lib/synvert/core/helper.rb
synvert-core-2.0.1 lib/synvert/core/helper.rb
synvert-core-2.0.0 lib/synvert/core/helper.rb
synvert-core-1.35.4 lib/synvert/core/helper.rb
synvert-core-1.35.3 lib/synvert/core/helper.rb
synvert-core-1.35.2 lib/synvert/core/helper.rb
synvert-core-1.35.1 lib/synvert/core/helper.rb
synvert-core-1.35.0 lib/synvert/core/helper.rb
synvert-core-1.34.0 lib/synvert/core/helper.rb
synvert-core-1.33.3 lib/synvert/core/helper.rb
synvert-core-1.33.2 lib/synvert/core/helper.rb
synvert-core-1.33.1 lib/synvert/core/helper.rb
synvert-core-1.33.0 lib/synvert/core/helper.rb
synvert-core-1.32.1 lib/synvert/core/helper.rb
synvert-core-1.32.0 lib/synvert/core/helper.rb