lib/hotcell/config.rb in hotcell-0.0.1 vs lib/hotcell/config.rb in hotcell-0.1.0

- old
+ new

@@ -2,30 +2,57 @@ module Hotcell class Config include Singleton - attr_reader :commands, :blocks, :subcommands + attr_reader :commands, :blocks, :helpers + attr_accessor :resolver def initialize @commands = {} @blocks = {} - @subcommands = {} + @helpers = [] + @resolver = Hotcell::Resolver.new end - def register_command name, klass + # 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 - klass.subcommands.each do |subcommand| - subcommands[subcommand] = klass - end 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