Class: Brandish::Processors::All::If

Inherits:
Brandish::Processor::Base show all
Includes:
Brandish::Processor::Block
Defined in:
lib/brandish/processors/all/if.rb

Overview

Note:

Using the :embed option is very dangerous - ONLY USE IF YOU TRUST THE SOURCE. :embed provides no sandboxing by default. This is why its value must be set to be true exactly, and not any other value.

Provides both an if and an unless block into the output. The processor takes one option: :embed. If embed is true (using strict equalivalence equal?), then the if statement takes a single pair, named "condition". This condition is executed using Execute, and if it is true, and if it is an if block, the contents are included; otherwise, if it is false, and if it is an unless block, the contents are included; otherwise, they are ignored. If embed is false, the block can take multiple pairs, and each pair is matched to a name. If the pair's value equals the matched name's value, then the condition is true. If the condition is true, and the block is an if block, the contents are included; otherwise, if it is false, and if it is an unless block, the contents are included; otherwise, they are not. The pair conditions that are matched by default are "format" and "form". If pairs are included that are not provided, it errors.

Options:

  • :embed - Optional. Whether or not the condition should be treaded like an embedded condition. This must be set to the exact value of true for it to be accepted.

Pairs:

  • "condition" - Optional. The embedded condition for the if or unless block. Only applies if the :embed option is set.
  • "format" - Optional. If this is provided, and the :embed option is not set, it is added as a condition.
  • "form" - Optional. If this is provided, and the :embed option is not set, it is added as a condition.

Examples:

non-embed

<if format="html">
  <import file="html-style" />
</if>

embed

<if condition="@format == :html">
  <import file="html-style" />
</if>

Instance Attribute Summary

Attributes inherited from Brandish::Processor::Base

#context

Instance Method Summary collapse

Methods included from Brandish::Processor::Block

included, #process_block

Methods inherited from Brandish::Processor::Base

#accept, #call, #initialize, #postprocess, #process_block, #process_command, #process_root, #process_text, register, #setup

Constructor Details

This class inherits a constructor from Brandish::Processor::Base

Instance Method Details

#meets_conditions?::Boolean

If the name of this block is "if", and all of the conditions are matched as outlined in the class direction, then this returns true; otherwise, if the name of this block is "if", and any of the conditions are not matched as outlined in the class description, then this returns false; otherwise, if the name of this block is anything else (i.e. "unless"), and all of the conditions are matched as outlined in the class description, then this returns false; otherwise, if any of the conditions are not matched as outlined in the class description, then this returns true.

Returns:

  • (::Boolean)


77
78
79
# File 'lib/brandish/processors/all/if.rb', line 77

def meets_conditions?
  @name == "if" ? match_conditions : !match_conditions
end

#performParser::Node?

If #meets_conditions? is true, this accepts the body of the block for processing; otherwise, it returns nil, effectively causing the body to be ignored.

Returns:



62
63
64
# File 'lib/brandish/processors/all/if.rb', line 62

def perform
  accept(@body) if meets_conditions?
end