Sha256: a447c9b02b65cb56a876ecea0857a0b0e573ce857de6f211c78dd844c374abf6

Contents?: true

Size: 1.14 KB

Versions: 117

Compression:

Stored size: 1.14 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, exp = c
          result << [:code, condition == :else ? 'else' : "when #{condition}"] << compile(exp)
        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

117 entries across 103 versions & 7 rubygems

Version Path
brakeman-3.7.2 bundle/ruby/2.3.0/gems/temple-0.7.7/lib/temple/filters/control_flow.rb
brakeman-3.7.1 bundle/ruby/2.3.0/gems/temple-0.7.7/lib/temple/filters/control_flow.rb
brakeman-3.7.0 bundle/ruby/2.3.0/gems/temple-0.7.7/lib/temple/filters/control_flow.rb
brakeman-3.6.2 bundle/ruby/2.3.0/gems/temple-0.7.7/lib/temple/filters/control_flow.rb
brakeman-3.6.1 bundle/ruby/2.3.0/gems/temple-0.7.7/lib/temple/filters/control_flow.rb
brakeman-3.6.0 bundle/ruby/2.3.0/gems/temple-0.7.7/lib/temple/filters/control_flow.rb
temple-0.8.0 lib/temple/filters/control_flow.rb
brakeman-3.5.0 bundle/ruby/2.3.0/gems/temple-0.7.7/lib/temple/filters/control_flow.rb
brakeman-3.4.1 bundle/ruby/2.3.0/gems/temple-0.7.7/lib/temple/filters/control_flow.rb
brakeman-3.4.0 bundle/ruby/2.3.0/gems/temple-0.7.7/lib/temple/filters/control_flow.rb
brakeman-3.3.5 bundle/ruby/2.3.0/gems/temple-0.7.7/lib/temple/filters/control_flow.rb
brakeman-3.3.4 bundle/ruby/2.3.0/gems/temple-0.7.7/lib/temple/filters/control_flow.rb
brakeman-3.3.3 bundle/ruby/2.3.0/gems/temple-0.7.7/lib/temple/filters/control_flow.rb
brakeman-3.3.2 bundle/ruby/2.3.0/gems/temple-0.7.7/lib/temple/filters/control_flow.rb
brakeman-3.3.1 bundle/ruby/2.3.0/gems/temple-0.7.7/lib/temple/filters/control_flow.rb
temple-0.7.7 lib/temple/filters/control_flow.rb
brakeman-3.3.0 bundle/ruby/2.3.0/gems/temple-0.7.6/lib/temple/filters/control_flow.rb
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/temple-0.7.6/lib/temple/filters/control_flow.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/temple-0.7.6/lib/temple/filters/control_flow.rb
angular-rails4-templates-0.3.0 vendor/ruby/2.1.0/gems/temple-0.7.6/lib/temple/filters/control_flow.rb