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


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

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)


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

def pattern
  @pattern
end

Class Method Details

.append_flags_group(rx_string, options) ⇒ Object


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

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


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

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


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

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


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

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

.register_ptype(loader, ir) ⇒ Object


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

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


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

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

#hashObject


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

def hash
  @pattern.hash
end

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


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

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

#regexpObject


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

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