Sha256: 6f9a6b4ad9219eee7d44f23f1f5c8908601075036684c2c5876fb06e49ae3f0c

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

require 'singleton'

module Hotcell
  class Config
    include Singleton

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

    def initialize
      @commands = {}
      @blocks = {}
      @helpers = []
      @resolver = Hotcell::Resolver.new
    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

1 entries across 1 versions & 1 rubygems

Version Path
hotcell-0.1.0 lib/hotcell/config.rb