class Puppet::Pops::Types::PPatternType

Represents a subtype of String that narrows the string to those matching the patterns If specified without a pattern it is basically the same as the String type.

@api public

Constants

DEFAULT

Attributes

patterns[R]

Public Class Methods

new(patterns) click to toggle source
     # File lib/puppet/pops/types/types.rb
1778 def initialize(patterns)
1779   @patterns = patterns.freeze
1780 end
register_ptype(loader, ir) click to toggle source
     # File lib/puppet/pops/types/types.rb
1772 def self.register_ptype(loader, ir)
1773   create_ptype(loader, ir, 'ScalarDataType', 'patterns' => PArrayType.new(PRegexpType::DEFAULT))
1774 end

Public Instance Methods

accept(visitor, guard) click to toggle source
Calls superclass method Puppet::Pops::Types::PAnyType#accept
     # File lib/puppet/pops/types/types.rb
1782 def accept(visitor, guard)
1783   super
1784   @patterns.each { |p| p.accept(visitor, guard) }
1785 end
eql?(o) click to toggle source
     # File lib/puppet/pops/types/types.rb
1791 def eql?(o)
1792   self.class == o.class && @patterns.size == o.patterns.size && (@patterns - o.patterns).empty?
1793 end
hash() click to toggle source
     # File lib/puppet/pops/types/types.rb
1787 def hash
1788   @patterns.hash
1789 end
instance?(o, guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
1795 def instance?(o, guard = nil)
1796   o.is_a?(String) && (@patterns.empty? || @patterns.any? { |p| p.regexp.match(o) })
1797 end

Protected Instance Methods

_assignable?(o, guard) click to toggle source

@api private

     # File lib/puppet/pops/types/types.rb
1805 def _assignable?(o, guard)
1806   return true if self == o
1807   case o
1808   when PStringType
1809     v = o.value
1810     if v.nil?
1811       # Strings cannot all match a pattern, but if there is no pattern it is ok
1812       # (There should really always be a pattern, but better safe than sorry).
1813       @patterns.empty?
1814     else
1815       # the string in String type must match one of the patterns in Pattern type,
1816       # or Pattern represents all Patterns == all Strings
1817       regexps = @patterns.map { |p| p.regexp }
1818       regexps.empty? || regexps.any? { |re| re.match(v) }
1819     end
1820   when PEnumType
1821     if o.values.empty?
1822       # Enums (unknown which ones) cannot all match a pattern, but if there is no pattern it is ok
1823       # (There should really always be a pattern, but better safe than sorry).
1824       @patterns.empty?
1825     else
1826       # all strings in String/Enum type must match one of the patterns in Pattern type,
1827       # or Pattern represents all Patterns == all Strings
1828       regexps = @patterns.map { |p| p.regexp }
1829       regexps.empty? || o.values.all? { |s| regexps.any? {|re| re.match(s) } }
1830     end
1831   when PPatternType
1832     @patterns.empty?
1833   else
1834     false
1835   end
1836 end