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.



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

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)



1695
1696
1697
# File 'lib/puppet/pops/types/types.rb', line 1695

def pattern
  @pattern
end

Class Method Details

.append_flags_group(rx_string, options) ⇒ Object



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

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



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

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

    def from_string(pattern)
      Regexp.new(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



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

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



1699
1700
1701
# File 'lib/puppet/pops/types/types.rb', line 1699

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

.register_ptype(loader, ir) ⇒ Object



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

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)


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

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

#hashObject



1741
1742
1743
# File 'lib/puppet/pops/types/types.rb', line 1741

def hash
  @pattern.hash
end

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

Returns:

  • (Boolean)


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

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

#regexpObject



1737
1738
1739
# File 'lib/puppet/pops/types/types.rb', line 1737

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