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.


1730
1731
1732
1733
1734
1735
1736
1737
# File 'lib/puppet/pops/types/types.rb', line 1730

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)


1697
1698
1699
# File 'lib/puppet/pops/types/types.rb', line 1697

def pattern
  @pattern
end

Class Method Details

.append_flags_group(rx_string, options) ⇒ Object


1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
# File 'lib/puppet/pops/types/types.rb', line 1711

def self.append_flags_group(rx_string, options)
  if options == 0
    rx_string
  else
    bld = '(?'
    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


1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
# File 'lib/puppet/pops/types/types.rb', line 1684

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


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

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


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

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

.register_ptype(loader, ir) ⇒ Object


1673
1674
1675
1676
1677
1678
1679
# File 'lib/puppet/pops/types/types.rb', line 1673

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)

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

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

#hashObject


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

def hash
  @pattern.hash
end

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

Returns:

  • (Boolean)

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

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

#regexpObject


1739
1740
1741
# File 'lib/puppet/pops/types/types.rb', line 1739

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