Module: Glyph::Macro::Validators

Included in:
Glyph::Macro
Defined in:
lib/glyph/macro_validators.rb

Overview

Since:

  • 0.2.0

Instance Method Summary collapse

Instance Method Details

#exact_parameters(n, options = {:level=>:error}) ⇒ Boolean

Ensures that the macro receives exactly n parameters.

Parameters:

  • n (Integer)

    the number of parameters allowed for the macro.

  • options (Hash) (defaults to: {:level=>:error})

    a hash containing validation options

Options Hash (options):

  • :level (Object)

    the error level (:error, :warning)

Returns:

  • (Boolean)

    whether the validation passed or not

Since:

  • 0.2.0


73
74
75
76
77
78
79
80
81
# File 'lib/glyph/macro_validators.rb', line 73

def exact_parameters(n, options={:level=>:error})
	validate("Macro '#{@name}' takes exactly #{n} parameter(s) (#{@node.params.length} given)", options) do
		if n == 0 then
			no_parameters options
		else
			@node.params.length == n 
		end
	end
end

#max_parameters(n, options = {:level=>:error}) ⇒ Boolean

Ensures that the macro receives up to n parameters.

Parameters:

  • n (Integer)

    the maximum number of parameters allowed for the macro.

  • options (Hash) (defaults to: {:level=>:error})

    a hash containing validation options

Options Hash (options):

  • :level (Object)

    the error level (:error, :warning)

Returns:

  • (Boolean)

    whether the validation passed or not

Since:

  • 0.2.0


49
50
51
52
53
54
55
56
57
# File 'lib/glyph/macro_validators.rb', line 49

def max_parameters(n, options={:level=>:error})
	validate("Macro '#{@name}' takes up to #{n} parameter(s) (#{@node.params.length} given)", options) do
		if n == 0 then
			no_parameters options
		else
			@node.params.length <= n 
		end
	end
end

#min_parameters(n, options = {:level=>:error}) ⇒ Boolean

Ensures that the macro receives at least n parameters.

Parameters:

  • n (Integer)

    the minimum number of parameters allowed for the macro.

  • options (Hash) (defaults to: {:level=>:error})

    a hash containing validation options

Options Hash (options):

  • :level (Object)

    the error level (:error, :warning)

Returns:

  • (Boolean)

    whether the validation passed or not

Since:

  • 0.2.0


64
65
66
# File 'lib/glyph/macro_validators.rb', line 64

def min_parameters(n, options={:level=>:error})
	validate("Macro '#{@name}' takes at least #{n} parameter(s) (#{@node.params.length} given)", options) { @node.params.length >= n }
end

#no_mutual_inclusion_in(arg) ⇒ Object

Ensures that no mutual inclusion occurs within the specified parameter or attribute

Parameters:

  • the (Fixnum, Symbol)

    parameter index or attribute name to check

Raises:

Since:

  • 0.3.0


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.

Parameters:

  • options (Hash) (defaults to: {:level=>:error})

    a hash containing validation options

Options Hash (options):

  • :level (Object)

    the error level (:error, :warning)

Returns:

  • (Boolean)

    whether the validation passed or not

Since:

  • 0.2.0


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(options={:level=>:error})
	validate("Macro '#{@name}' takes no parameters (#{@node.params.length} given)", options) 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

Parameters:

  • arg (String, Symbol)

    the name of the container macro

  • options (Hash) (defaults to: {:level => :error})

    a hash containing validation options

Options Hash (options):

  • :level (Object)

    the error level (:error, :warning)

Returns:

  • (Boolean)

    whether the validation passed or not

Since:

  • 0.4.0


170
171
172
173
174
# File 'lib/glyph/macro_validators.rb', line 170

def not_within(arg, options={:level => :error})
	validate("Macro '#{@name}' must not be within a '#{arg}' macro", options) 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

Parameters:

  • name (String, Symbol)

    the name of the attribute

  • options (Hash) (defaults to: {:level=>:error})

    a hash containing validation options

Options Hash (options):

  • :level (Object)

    the error level (:error, :warning)

Returns:

  • (Boolean)

    whether the validation passed or not

Since:

  • 0.4.0


89
90
91
92
93
# File 'lib/glyph/macro_validators.rb', line 89

def required_attribute(name, options={:level=>:error})
	validate("Macro '#{@name}' requires a '#{name}' attribute", options) do
		!raw_attribute(name.to_sym).blank?
	end
end

#safety_checkObject

Raises a macro error if Glyph is running in safe mode.

Raises:

Since:

  • 0.3.0


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.

Parameters:

  • name (String, Symbol)

    the attribute name to validate

  • options (Hash) (defaults to: {:level => :warning})

    a hash containing validation options

Options Hash (options):

  • :level (Object)

    the error level (:error, :warning)

Returns:

  • (Boolean)

    whether the validation passed or not

Since:

  • 0.3.0


40
41
42
# File 'lib/glyph/macro_validators.rb', line 40

def valid_xml_attribute(name, options={:level => :warning})
	validate("Invalid XML attribute '#{name}'", options) { 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.

Parameters:

  • name (String, Symbol)

    the element name to validate

  • options (Hash) (defaults to: {:level => :error})

    a hash containing validation options (for now the only option is :level)

Returns:

  • (Boolean)

    whether the validation passed or not

Since:

  • 0.3.0


30
31
32
# File 'lib/glyph/macro_validators.rb', line 30

def valid_xml_element(name, options={:level => :error})
	validate("Invalid XML element '#{name}'", options) { name.to_s.match(/^([^[:punct:]0-9<>]|_)[^<>"']*/) }
end

#validate(message, options = {:level => :error}, &block) ⇒ Boolean

Validates the macro according to the specified block

Examples:

validate("Invalid macro value", :level => :error) {value == 'valid'} # Raises an error in case of failure
validate("Invalid macro value", :level => :warning) {value == 'valid'} # Displays a warning in case of failure

Parameters:

  • message (String)

    the message to display if the validation fails.

  • options (Hash) (defaults to: {:level => :error})

    a hash containing validation options

Options Hash (options):

  • :level (Object)

    the error level (:error, :warning)

Returns:

  • (Boolean)

    whether the validation passed or not

Since:

  • 0.2.0


17
18
19
20
21
22
23
# File 'lib/glyph/macro_validators.rb', line 17

def validate(message, options={:level => :error}, &block)
	result = instance_eval(&block)
	unless result then
		send("macro_#{options[:level]}".to_sym, message)
	end
	result
end

#within(arg, options = {:level => :error}) ⇒ Boolean

Ensures that the macros is within another

Parameters:

  • arg (String, Symbol)

    the name of the container macro

  • options (Hash) (defaults to: {:level => :error})

    a hash containing validation options

Options Hash (options):

  • :level (Object)

    the error level (:error, :warning)

Returns:

  • (Boolean)

    whether the validation passed or not

Since:

  • 0.4.0


158
159
160
161
162
# File 'lib/glyph/macro_validators.rb', line 158

def within(arg, options={:level => :error})
	validate("Macro '#{@name}' must be within a '#{arg}' macro", options) do 
		@node.find_parent {|n| Glyph.macro_eq? arg.to_sym, n[:name]}
	end
end