Class: Puppet::Pops::Types::PRegexpType

Inherits:
PScalarType show all
Defined in:
lib/puppet/pops/types/types.rb

Constant Summary collapse

DEFAULT =
PRegexpType.new(nil)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PScalarType

#roundtrip_with_string?

Methods inherited from PAnyType

#==, #accept, #assignable?, #callable?, #callable_args?, #callable_with?, #check_self_recursion, create, #create, #generalize, #iterable?, #iterable_type, #kind_of_callable?, #loader, #name, #new_function, #normalize, #really_instance?, #resolve, #roundtrip_with_string?, #simple_name, simple_name, #to_alias_expanded_s, #to_s

Methods inherited from TypedModelObject

_pcore_type, create_ptype, register_ptypes

Methods included from Visitable

#accept

Methods included from PuppetObject

#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type, #to_s

Constructor Details

#initialize(pattern) ⇒ PRegexpType

Returns a new instance of PRegexpType.



1735
1736
1737
1738
1739
1740
1741
1742
# File 'lib/puppet/pops/types/types.rb', line 1735

def initialize(pattern)
  if pattern.is_a?(Regexp)
    @regexp = pattern
    @pattern = PRegexpType.regexp_to_s(pattern)
  else
    @pattern = pattern
  end
end

Instance Attribute Details

#patternObject (readonly)



1702
1703
1704
# File 'lib/puppet/pops/types/types.rb', line 1702

def pattern
  @pattern
end

Class Method Details

.append_flags_group(rx_string, options) ⇒ Object



1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
# File 'lib/puppet/pops/types/types.rb', line 1716

def self.append_flags_group(rx_string, options)
  if options == 0
    rx_string
  else
    bld = '(?'.dup
    bld << 'i' if (options & Regexp::IGNORECASE) != 0
    bld << 'm' if (options & Regexp::MULTILINE) != 0
    bld << 'x' if (options & Regexp::EXTENDED) != 0
    unless options == (Regexp::IGNORECASE | Regexp::MULTILINE | Regexp::EXTENDED)
      bld << '-'
      bld << 'i' if (options & Regexp::IGNORECASE) == 0
      bld << 'm' if (options & Regexp::MULTILINE) == 0
      bld << 'x' if (options & Regexp::EXTENDED) == 0
    end
    bld << ':' << rx_string << ')'
    bld.freeze
  end
end

.new_function(type) ⇒ Object

Returns a new function that produces a Regexp instance



1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
# File 'lib/puppet/pops/types/types.rb', line 1689

def self.new_function(type)
  @new_function ||= Puppet::Functions.create_loaded_function(:new_float, type.loader) do
    dispatch :from_string do
      param 'String', :pattern
      optional_param 'Boolean', :escape
    end

    def from_string(pattern, escape = false)
      Regexp.new(escape ? Regexp.escape(pattern) : pattern)
    end
  end
end

.regexp_to_s(regexp) ⇒ String

Returns the Regexp as a string without escaped slash.

Parameters:

  • regexp (Regexp)

    the regular expression

Returns:

  • (String)

    the Regexp as a string without escaped slash



1712
1713
1714
# File 'lib/puppet/pops/types/types.rb', line 1712

def self.regexp_to_s(regexp)
  append_flags_group(regexp.source, regexp.options)
end

.regexp_to_s_with_delimiters(regexp) ⇒ String

Returns the Regexp as a slash delimited string with slashes escaped.

Parameters:

  • regexp (Regexp)

    the regular expression

Returns:

  • (String)

    the Regexp as a slash delimited string with slashes escaped



1706
1707
1708
# File 'lib/puppet/pops/types/types.rb', line 1706

def self.regexp_to_s_with_delimiters(regexp)
  regexp.options == 0 ? regexp.inspect : "/#{regexp}/"
end

.register_ptype(loader, ir) ⇒ Object



1679
1680
1681
1682
1683
1684
1685
# File 'lib/puppet/pops/types/types.rb', line 1679

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'ScalarType',
               'pattern' => {
                 KEY_TYPE => PVariantType.new([PUndefType::DEFAULT, PStringType::DEFAULT, PRegexpType::DEFAULT]),
                 KEY_VALUE => nil
               })
end

Instance Method Details

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


1752
1753
1754
# File 'lib/puppet/pops/types/types.rb', line 1752

def eql?(o)
  self.class == o.class && @pattern == o.pattern
end

#hashObject



1748
1749
1750
# File 'lib/puppet/pops/types/types.rb', line 1748

def hash
  @pattern.hash
end

#instance?(o, guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


1756
1757
1758
# File 'lib/puppet/pops/types/types.rb', line 1756

def instance?(o, guard = nil)
  o.is_a?(Regexp) && @pattern.nil? || regexp == o
end

#regexpObject



1744
1745
1746
# File 'lib/puppet/pops/types/types.rb', line 1744

def regexp
  @regexp ||= Regexp.new(@pattern || '')
end