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
1779 def initialize(patterns)
1780   @patterns = patterns.freeze
1781 end
register_ptype(loader, ir) click to toggle source
     # File lib/puppet/pops/types/types.rb
1773 def self.register_ptype(loader, ir)
1774   create_ptype(loader, ir, 'ScalarDataType', 'patterns' => PArrayType.new(PRegexpType::DEFAULT))
1775 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
1783 def accept(visitor, guard)
1784   super
1785   @patterns.each { |p| p.accept(visitor, guard) }
1786 end
eql?(o) click to toggle source
     # File lib/puppet/pops/types/types.rb
1792 def eql?(o)
1793   self.class == o.class && @patterns.size == o.patterns.size && (@patterns - o.patterns).empty?
1794 end
hash() click to toggle source
     # File lib/puppet/pops/types/types.rb
1788 def hash
1789   @patterns.hash
1790 end
instance?(o, guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
1796 def instance?(o, guard = nil)
1797   o.is_a?(String) && (@patterns.empty? || @patterns.any? { |p| p.regexp.match(o) })
1798 end

Protected Instance Methods

_assignable?(o, guard) click to toggle source

@api private

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