Class: Spectre::Directives::MinMaxDirective

Inherits:
Spectre::Directive show all
Defined in:
lib/spectre/generic/directives.rb

Overview

Causes every parser wrapped in it to fail unless the specified minimum and maximum requirements for the matched values are met. Both values may be nil, indicating a don’t-care condition. Also, min can be set to a range, in which case max will be ignored and the range will be taken as the indication.

The comparison is performed via

_min_ > _value_
_max_ < _value_

That allows you to supply your own classes for min and max that implement those methods, which allows for more complex comparisons.

NOTE: The values set are Parser specific and may thus not be applicable to all Parsers.

Shortcut: limit_d.

Instance Attribute Summary

Attributes inherited from Spectre::Directive

#iter_skipper, #iter_transformation, #pre_skip

Attributes inherited from Node

#actions, #backtrace, #left, #parent, #parser, #policy, #right, #symbols

Instance Method Summary collapse

Methods inherited from Spectre::Directive

#[], #parse, policy, policy!, skipper!, transformation!

Methods inherited from Node

#%, #&, #*, #**, #+, #-, #-@, #>>, #[], #^, #backtrack, #chain, #closure, #closure=, #closure?, #find, #initialize_copy, #leaf?, #parse, #replace_with, #root?, #shallow_copy, #to_p, #|, #~@

Constructor Details

#initialize(min = nil, max = nil) ⇒ MinMaxDirective

Returns a new instance of MinMaxDirective.



65
66
67
68
69
70
71
72
73
# File 'lib/spectre/generic/directives.rb', line 65

def initialize min = nil, max = nil
    super()

    if min.is_a? Range then hsh = { :min => min.first, :max => min.last }
    else hsh = { :min => min, :max => max }
    end

    @policy ? @policy.merge!(hsh) : @policy = hsh
end

Instance Method Details

#inspectObject



75
76
77
# File 'lib/spectre/generic/directives.rb', line 75

def inspect
    "[limit_d:#{@left.inspect}]"
end