Sha256: fa2e0659ec44de97a4588fd24e16412e5aada29dd5c3efa3523469a155f74516

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

module Hotcell
  module Commands
    class If < Hotcell::Block
      class Else < Hotcell::Command
        validate_arguments_count 0
      end

      class Elsif < Hotcell::Command
        validate_arguments_count 1
      end

      subcommand else: Else, elsif: Elsif
      validate_arguments_count 1

      def subcommand_error subcommand, *allowed_names
        raise Hotcell::BlockError.new(
          "Unexpected `#{subcommand.name}` for `#{name}` command",
          *subcommand.position_info
        ) unless allowed_names.flatten.include?(subcommand.name)
      end

      def validate!
        last = subcommands.length - 1
        subcommands.each_with_index do |subcommand, index|
          subcommand_error subcommand, (index == last ? ['elsif', 'else'] : ['elsif'])
        end

        super
      end

      def process context, condition
        conditions = [[condition]]
        subnodes.each do |subnode|
          if subnode.is_a?(Hotcell::Command)
            conditions.last[1] = '' if conditions.last[1] == nil
            conditions << (subnode.name == 'elsif' ? [subnode] : [true])
          else
            conditions.last[1] = subnode
          end
        end
        condition = conditions.detect do |condition|
          condition[0].is_a?(Hotcell::Command) ?
            condition[0].render_children(context).first : condition[0]
        end
        condition ? condition[1].render(context) : nil
      end
    end
  end
end

Hotcell.register_command if: Hotcell::Commands::If

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hotcell-0.3.0 lib/hotcell/commands/if.rb
hotcell-0.2.0 lib/hotcell/commands/if.rb
hotcell-0.1.0 lib/hotcell/commands/if.rb