Module: Zafu::Process::Conditional

Defined in:
lib/zafu/process/conditional.rb

Overview

This module manages conditional rendering (if, else, elsif, case, when).

Instance Method Summary collapse

Instance Method Details

#expand_if(condition, new_node_context = self.node, alt_markup = @markup) ⇒ Object

Expand blocks with conditional processing enabled (else, elsif, etc).

Parameters

  • condition - ruby condition for the conditional execution.

  • new_node_context - (optional) new node context to enter if the clause succeeds.

  • alt_markup - (optional) alternative markup to use for the ‘else’, ‘elsif’ clauses.



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/zafu/process/conditional.rb', line 58

def expand_if(condition, new_node_context = self.node, alt_markup = @markup)
  res = ""
  res << "<% if #{condition} -%>"

  with_context(:node => new_node_context) do
    res << wrap(expand_with)
  end

  res << expand_with(:in_if => true, :only => /^[A-Z]|else|elsif|when/, :markup => alt_markup)
  res << "<% end -%>"
  res
end

#r_caseObject



11
12
13
# File 'lib/zafu/process/conditional.rb', line 11

def r_case
  r_if('false')
end

#r_elseObject



15
16
17
# File 'lib/zafu/process/conditional.rb', line 15

def r_else
  r_elsif('true')
end

#r_elsif(cond = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/zafu/process/conditional.rb', line 23

def r_elsif(cond = nil)
  return '' unless @context[:in_if]
  cond ||= get_condition
  return unless cond

  res = expand_with(:in_if => false, :markup => nil)

  # We use 'elsif' just in case there are more then one 'else' clause
  if markup = @context[:markup]
    @markup.tag ||= markup.tag
    @markup.steal_html_params_from(@params)
    markup.params.each do |k, v|
      next if @markup.params[k]
      @markup.set_param(k, v)
    end

    markup.dyn_params.each do |k, v|
      next if @markup.params[k] || @markup.dyn_params[k]
      @markup.set_dyn_param(k, v)
    end

    out "<% elsif #{cond} -%>#{wrap(res)}" # do not propagate
  else
    @markup.done = true # never wrap else/elsif clause
    out "<% elsif #{cond} -%>#{res}" # do not propagate
  end
end

#r_if(cond = nil) ⇒ Object



5
6
7
8
9
# File 'lib/zafu/process/conditional.rb', line 5

def r_if(cond = nil)
  cond ||= get_condition
  return unless cond
  expand_if(cond)
end

#r_whenObject



19
20
21
# File 'lib/zafu/process/conditional.rb', line 19

def r_when
  r_elsif
end