Sha256: 7220857981844757bd17368ae552bbc3edf64c3c31e1a0146d110cc5d8652d58

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

require 'singleton'

module Hotcell
  class Config
    include Singleton

    attr_reader :commands, :blocks, :helpers
    attr_accessor :resolver, :escape_tags

    def initialize
      @commands = {}
      @blocks = {}
      @helpers = []
      @resolver = Hotcell::Resolver.new
      @escape_tags = false
    end

    # Adds command or block to the list of default commands or blocks returned
    # by `Hotcell.commands` and `Hotcell.blocks` accessors respectively
    #
    # Usage:
    #
    #   Hotcell.register_command :block, BlockClass
    #   Hotcell.register_command :command, CommandClass
    #   Hotcell.register_command block: BlockClass, command: CommandClass
    #
    def register_command name, klass = nil
      if name.is_a? Hash
        name.each do |(name, klass)|
          register_command name, klass
        end
        return
      end

      name = name.to_s
      if klass < ::Hotcell::Block
        raise "Command `#{name}` already defined, you can not define block with the same name" if commands.key?(name)
        blocks[name] = klass
      elsif klass < ::Hotcell::Command
        raise "Block `#{name}` already defined, you can not define command with the same name" if blocks.key?(name)
        commands[name] = klass
      else
        raise "Cannot register command `#{name}` because handler is not a Hotcell::Command or Hotcell::Block"
      end
    end

    # Adds helper to the list of default helpers, accessible by `Hotcell.helpers`
    #
    # Usage:
    #
    #   Hotcell.register_helpers Helper
    #   Hotcell.register_helpers Helper1, Helper2
    #   Hotcell.register_helpers helpers_array
    #
    def register_helpers *helpers
      @helpers |= helpers.flatten
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hotcell-0.3.0 lib/hotcell/config.rb
hotcell-0.2.0 lib/hotcell/config.rb