Module: Glyph::Macro::Validators
- Included in:
- Glyph::Macro
- Defined in:
- lib/glyph/macro_validators.rb
Overview
Instance Method Summary collapse
-
#exact_parameters(n, options = {:level=>:error}) ⇒ Boolean
Ensures that the macro receives exactly n parameters.
-
#max_parameters(n, options = {:level=>:error}) ⇒ Boolean
Ensures that the macro receives up to n parameters.
-
#min_parameters(n, options = {:level=>:error}) ⇒ Boolean
Ensures that the macro receives at least n parameters.
-
#no_mutual_inclusion_in(arg) ⇒ Object
Ensures that no mutual inclusion occurs within the specified parameter or attribute.
-
#no_parameters(options = {:level=>:error}) ⇒ Boolean
Ensures that the macro receives no parameters.
-
#not_within(arg, options = {:level => :error}) ⇒ Boolean
Ensures that the macros is not within another.
-
#required_attribute(name, options = {:level=>:error}) ⇒ Boolean
Ensures that the macro receives the specified attribute.
-
#safety_check ⇒ Object
Raises a macro error if Glyph is running in safe mode.
-
#valid_xml_attribute(name, options = {:level => :warning}) ⇒ Boolean
Ensures that a macro attribute name is a valid XML attribute name.
-
#valid_xml_element(name, options = {:level => :error}) ⇒ Boolean
Ensures that the provided name is a valid XML element name.
-
#validate(message, options = {:level => :error}, &block) ⇒ Boolean
Validates the macro according to the specified block.
-
#within(arg, options = {:level => :error}) ⇒ Boolean
Ensures that the macros is within another.
Instance Method Details
#exact_parameters(n, options = {:level=>:error}) ⇒ Boolean
Ensures that the macro receives exactly n parameters.
73 74 75 76 77 78 79 80 81 |
# File 'lib/glyph/macro_validators.rb', line 73 def exact_parameters(n, ={:level=>:error}) validate("Macro '#{@name}' takes exactly #{n} parameter(s) (#{@node.params.length} given)", ) do if n == 0 then no_parameters else @node.params.length == n end end end |
#max_parameters(n, options = {:level=>:error}) ⇒ Boolean
Ensures that the macro receives up to n parameters.
49 50 51 52 53 54 55 56 57 |
# File 'lib/glyph/macro_validators.rb', line 49 def max_parameters(n, ={:level=>:error}) validate("Macro '#{@name}' takes up to #{n} parameter(s) (#{@node.params.length} given)", ) do if n == 0 then no_parameters else @node.params.length <= n end end end |
#min_parameters(n, options = {:level=>:error}) ⇒ Boolean
Ensures that the macro receives at least n parameters.
64 65 66 |
# File 'lib/glyph/macro_validators.rb', line 64 def min_parameters(n, ={:level=>:error}) validate("Macro '#{@name}' takes at least #{n} parameter(s) (#{@node.params.length} given)", ) { @node.params.length >= n } end |
#no_mutual_inclusion_in(arg) ⇒ Object
Ensures that no mutual inclusion occurs within the specified parameter or attribute
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/glyph/macro_validators.rb', line 128 def no_mutual_inclusion_in(arg) check_type = arg.is_a?(Symbol) ? :attribute : :parameter check_value = nil found = @node.find_parent do |n| if n.is_a?(Glyph::MacroNode) && Glyph::MACROS[n[:name]] == Glyph::MACROS[@name] then case check_type when :attribute then check_value = n.children.select do |node| node.is_a?(Glyph::AttributeNode) && node[:name] == arg end[0][:value] rescue nil check_value == attr(arg) when :parameter then check_value = n.children.select do |node| node.is_a?(Glyph::ParameterNode) && node[:name] == :"#{arg}" end[0][:value] rescue nil check_value == param(arg) end end end if found then macro_error "Mutual Inclusion in #{check_type}(#{arg}): '#{check_value}'", Glyph::MutualInclusionError end end |
#no_parameters(options = {:level=>:error}) ⇒ Boolean
Ensures that the macro receives no parameters.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/glyph/macro_validators.rb', line 99 def no_parameters(={:level=>:error}) validate("Macro '#{@name}' takes no parameters (#{@node.params.length} given)", ) do case @node.params.length when 0 then true when 1 then result = true @node.param(0).children.each do |p| result = p.is_a?(Glyph::TextNode) && p[:value].blank? break unless result end result else false end end end |
#not_within(arg, options = {:level => :error}) ⇒ Boolean
Ensures that the macros is not within another
170 171 172 173 174 |
# File 'lib/glyph/macro_validators.rb', line 170 def not_within(arg, ={:level => :error}) validate("Macro '#{@name}' must not be within a '#{arg}' macro", ) do !@node.find_parent {|n| Glyph.macro_eq? arg.to_sym, n[:name]} end end |
#required_attribute(name, options = {:level=>:error}) ⇒ Boolean
Ensures that the macro receives the specified attribute
89 90 91 92 93 |
# File 'lib/glyph/macro_validators.rb', line 89 def required_attribute(name, ={:level=>:error}) validate("Macro '#{@name}' requires a '#{name}' attribute", ) do !raw_attribute(name.to_sym).blank? end end |
#safety_check ⇒ Object
Raises a macro error if Glyph is running in safe mode.
120 121 122 |
# File 'lib/glyph/macro_validators.rb', line 120 def safety_check macro_error "Macro '#@name' cannot be used in safe mode" if Glyph.safe? end |
#valid_xml_attribute(name, options = {:level => :warning}) ⇒ Boolean
Ensures that a macro attribute name is a valid XML attribute name.
40 41 42 |
# File 'lib/glyph/macro_validators.rb', line 40 def valid_xml_attribute(name, ={:level => :warning}) validate("Invalid XML attribute '#{name}'", ) { name.to_s.match(/^([^[:punct:]0-9<>]|_)[^<>"']*/) } end |
#valid_xml_element(name, options = {:level => :error}) ⇒ Boolean
Ensures that the provided name is a valid XML element name.
30 31 32 |
# File 'lib/glyph/macro_validators.rb', line 30 def valid_xml_element(name, ={:level => :error}) validate("Invalid XML element '#{name}'", ) { name.to_s.match(/^([^[:punct:]0-9<>]|_)[^<>"']*/) } end |
#validate(message, options = {:level => :error}, &block) ⇒ Boolean
Validates the macro according to the specified block
17 18 19 20 21 22 23 |
# File 'lib/glyph/macro_validators.rb', line 17 def validate(, ={:level => :error}, &block) result = instance_eval(&block) unless result then send("macro_#{[:level]}".to_sym, ) end result end |
#within(arg, options = {:level => :error}) ⇒ Boolean
Ensures that the macros is within another
158 159 160 161 162 |
# File 'lib/glyph/macro_validators.rb', line 158 def within(arg, ={:level => :error}) validate("Macro '#{@name}' must be within a '#{arg}' macro", ) do @node.find_parent {|n| Glyph.macro_eq? arg.to_sym, n[:name]} end end |