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.


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

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

Instance Attribute Details

#case_insensitiveObject (readonly)


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

def case_insensitive
  @case_insensitive
end

#valuesObject (readonly)


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

def values
  @values
end

Class Method Details

.register_ptype(loader, ir) ⇒ Object


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

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

Returns:

  • (Boolean)

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

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


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

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

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)

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

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

#generalizeObject


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

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


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

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

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

Returns:

  • (Boolean)

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

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

Returns:

  • (Boolean)

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

def iterable?(guard = nil)
  true
end

#iterable_type(guard = nil) ⇒ Object


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

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