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 PAnyType

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

Methods inherited from TypedModelObject

_ptype, create_ptype, register_ptypes

Methods included from Visitable

#accept

Methods included from PuppetObject

#_ptype

Constructor Details

#initialize(pattern) ⇒ PRegexpType

Returns a new instance of PRegexpType.



1576
1577
1578
1579
1580
1581
1582
1583
# File 'lib/puppet/pops/types/types.rb', line 1576

def initialize(pattern)
  if pattern.is_a?(Regexp)
    @regexp = pattern
    @pattern = pattern.options == 0 ? pattern.source : pattern.to_s
  else
    @pattern = pattern
  end
end

Instance Attribute Details

#patternObject (readonly)



1574
1575
1576
# File 'lib/puppet/pops/types/types.rb', line 1574

def pattern
  @pattern
end

Class Method Details

.new_function(_, loader) ⇒ Object

Returns a new function that produces a Regexp instance



1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
# File 'lib/puppet/pops/types/types.rb', line 1562

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

    def from_string(pattern)
      Regexp.new(pattern)
    end
  end
end

.register_ptype(loader, ir) ⇒ Object



1551
1552
1553
1554
1555
1556
1557
# File 'lib/puppet/pops/types/types.rb', line 1551

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)


1593
1594
1595
# File 'lib/puppet/pops/types/types.rb', line 1593

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

#hashObject



1589
1590
1591
# File 'lib/puppet/pops/types/types.rb', line 1589

def hash
  @pattern.hash
end

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

Returns:

  • (Boolean)


1597
1598
1599
# File 'lib/puppet/pops/types/types.rb', line 1597

def instance?(o, guard=nil)
  o.is_a?(Regexp) && (@pattern.nil? || @pattern == (o.options == 0 ? o.source : o.to_s))
end

#regexpObject



1585
1586
1587
# File 'lib/puppet/pops/types/types.rb', line 1585

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