Class: Puppet::Pops::Types::PEnumType

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

Overview

A string type describing the set of strings having one of the given values

Constant Summary collapse

DEFAULT =
PEnumType.new(EMPTY_ARRAY)

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, #kind_of_callable?, #loader, #name, #new_function, 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(values, case_insensitive = false) ⇒ PEnumType

Returns a new instance of PEnumType


768
769
770
771
# File 'lib/puppet/pops/types/types.rb', line 768

def initialize(values, case_insensitive = false)
  @values = values.uniq.sort.freeze
  @case_insensitive = case_insensitive
end

Instance Attribute Details

#case_insensitiveObject (readonly)


766
767
768
# File 'lib/puppet/pops/types/types.rb', line 766

def case_insensitive
  @case_insensitive
end

#valuesObject (readonly)


766
767
768
# File 'lib/puppet/pops/types/types.rb', line 766

def values
  @values
end

Class Method Details

.register_ptype(loader, ir) ⇒ Object


760
761
762
763
764
# File 'lib/puppet/pops/types/types.rb', line 760

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'ScalarDataType',
    'values' => PArrayType.new(PStringType::NON_EMPTY),
    'case_insensitive' => { 'type' => PBooleanType::DEFAULT, 'value' => false })
end

Instance Method Details

#case_insensitive?Boolean


773
774
775
# File 'lib/puppet/pops/types/types.rb', line 773

def case_insensitive?
  @case_insensitive
end

#each(&block) ⇒ Object

Returns Enumerator if no block is given, otherwise, calls the given block with each of the strings for this enum


779
780
781
782
# File 'lib/puppet/pops/types/types.rb', line 779

def each(&block)
  r = Iterable.on(self)
  block_given? ? r.each(&block) : r
end

#eql?(o) ⇒ Boolean


807
808
809
# File 'lib/puppet/pops/types/types.rb', line 807

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

#generalizeObject


784
785
786
787
788
789
790
791
792
# File 'lib/puppet/pops/types/types.rb', line 784

def generalize
  # General form of an Enum is a String
  if @values.empty?
    PStringType::DEFAULT
  else
    range = @values.map(&:size).minmax
    PStringType.new(PIntegerType.new(range.min, range.max))
  end
end

#hashObject


803
804
805
# File 'lib/puppet/pops/types/types.rb', line 803

def hash
  @values.hash ^ @case_insensitive.hash
end

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


811
812
813
814
815
816
817
# File 'lib/puppet/pops/types/types.rb', line 811

def instance?(o, guard = nil)
  if o.is_a?(String)
    @case_insensitive ? @values.any? { |p| p.casecmp(o) == 0 } : @values.any? { |p| p == o }
  else
    false
  end
end

#iterable?(guard = nil) ⇒ Boolean


794
795
796
# File 'lib/puppet/pops/types/types.rb', line 794

def iterable?(guard = nil)
  true
end

#iterable_type(guard = nil) ⇒ Object


798
799
800
801
# File 'lib/puppet/pops/types/types.rb', line 798

def iterable_type(guard = nil)
  # An instance of an Enum is a String
  PStringType::ITERABLE_TYPE
end