Sha256: fbe78a749729fa12411a7799d3f59a076186d329e6e13fc25b5e40842e44b1fe

Contents?: true

Size: 908 Bytes

Versions: 3

Compression:

Stored size: 908 Bytes

Contents

module Hotcell
  class Block < Hotcell::Command
    class_attribute :subcommands, instance_writter: false, instance_reader: false
    self.subcommands = {}

    def self.subcommand name_or_hash, &block
      if name_or_hash.is_a? Hash
        self.subcommands = subcommands.merge(name_or_hash.stringify_keys)
      else
        subcommand name_or_hash => Class.new(Hotcell::Command, &block)
      end
    end

    def validate!
      subcommands.each do |subcommand|
        raise Hotcell::BlockError.new(
          "Unexpected subcommand `#{subcommand.name}` for `#{name}` command",
          *subcommand.position_info
        ) unless self.class.subcommands.key?(subcommand.name)
      end

      super

      subcommands.each(&:validate!)
    end

    def subnodes
      options[:subnodes] || []
    end

    def subcommands
      subnodes.select { |node| node.is_a?(Hotcell::Command) }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hotcell-0.3.0 lib/hotcell/node/block.rb
hotcell-0.2.0 lib/hotcell/node/block.rb
hotcell-0.1.0 lib/hotcell/node/block.rb