Sha256: 367c86f6770d05885bbbb2123ec5617a45f446ea442030e13ba50dbf1eb39cb4

Contents?: true

Size: 1.18 KB

Versions: 53

Compression:

Stored size: 1.18 KB

Contents

module Temple
  module Filters
    # Control flow filter which processes [:if, condition, yes-exp, no-exp]
    # and [:block, code, content] expressions.
    # This is useful for ruby code generation with lots of conditionals.
    #
    # @api public
    class ControlFlow < Filter
      def on_if(condition, yes, no = nil)
        result = [:multi, [:code, "if #{condition}"], compile(yes)]
        while no && no.first == :if
          result << [:code, "elsif #{no[1]}"] << compile(no[2])
          no = no[3]
        end
        result << [:code, 'else'] << compile(no) if no
        result << [:code, 'end']
        result
      end

      def on_case(arg, *cases)
        result = [:multi, [:code, arg ? "case (#{arg})" : 'case']]
        cases.map do |c|
          condition, *exps = c
          result << [:code, condition == :else ? 'else' : "when #{condition}"]
          exps.each {|e| result << compile(e) }
        end
        result << [:code, 'end']
        result
      end

      def on_cond(*cases)
        on_case(nil, *cases)
      end

      def on_block(code, exp)
        [:multi,
         [:code, code],
         compile(exp),
         [:code, 'end']]
      end
    end
  end
end

Version data entries

53 entries across 53 versions & 2 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/temple-0.4.0/lib/temple/filters/control_flow.rb
classiccms-0.7.4 vendor/bundle/gems/temple-0.4.0/lib/temple/filters/control_flow.rb
classiccms-0.7.3 vendor/bundle/gems/temple-0.4.0/lib/temple/filters/control_flow.rb
classiccms-0.7.2 vendor/bundle/gems/temple-0.4.0/lib/temple/filters/control_flow.rb
classiccms-0.7.1 vendor/bundle/gems/temple-0.4.0/lib/temple/filters/control_flow.rb
classiccms-0.7.0 vendor/bundle/gems/temple-0.4.0/lib/temple/filters/control_flow.rb
classiccms-0.6.9 vendor/bundle/gems/temple-0.4.0/lib/temple/filters/control_flow.rb
classiccms-0.6.8 vendor/bundle/gems/temple-0.4.0/lib/temple/filters/control_flow.rb
classiccms-0.6.7 vendor/bundle/gems/temple-0.4.0/lib/temple/filters/control_flow.rb
classiccms-0.6.6 vendor/bundle/gems/temple-0.4.0/lib/temple/filters/control_flow.rb
classiccms-0.6.5 vendor/bundle/gems/temple-0.4.0/lib/temple/filters/control_flow.rb
classiccms-0.6.4 vendor/bundle/gems/temple-0.4.0/lib/temple/filters/control_flow.rb
classiccms-0.6.3 vendor/bundle/gems/temple-0.4.0/lib/temple/filters/control_flow.rb
classiccms-0.6.2 vendor/bundle/gems/temple-0.4.0/lib/temple/filters/control_flow.rb
classiccms-0.6.1 vendor/bundle/gems/temple-0.4.0/lib/temple/filters/control_flow.rb
classiccms-0.6.0 vendor/bundle/gems/temple-0.4.0/lib/temple/filters/control_flow.rb
classiccms-0.5.17 vendor/bundle/gems/temple-0.4.0/lib/temple/filters/control_flow.rb
classiccms-0.5.16 vendor/bundle/gems/temple-0.4.0/lib/temple/filters/control_flow.rb
classiccms-0.5.15 vendor/bundle/gems/temple-0.4.0/lib/temple/filters/control_flow.rb
classiccms-0.5.14 vendor/bundle/gems/temple-0.4.0/lib/temple/filters/control_flow.rb