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

Constructor Details

#initialize(values) ⇒ PEnumType

Returns a new instance of PEnumType.



758
759
760
# File 'lib/puppet/pops/types/types.rb', line 758

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

Instance Attribute Details

#valuesObject (readonly)



756
757
758
# File 'lib/puppet/pops/types/types.rb', line 756

def values
  @values
end

Class Method Details

.register_ptype(loader, ir) ⇒ Object



752
753
754
# File 'lib/puppet/pops/types/types.rb', line 752

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'ScalarDataType', 'values' => PArrayType.new(PStringType::NON_EMPTY))
end

Instance Method Details

#each(&block) ⇒ Object

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



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

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

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#generalizeObject



769
770
771
772
773
774
775
776
777
# File 'lib/puppet/pops/types/types.rb', line 769

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



788
789
790
# File 'lib/puppet/pops/types/types.rb', line 788

def hash
  @values.hash
end

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

Returns:

  • (Boolean)


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

def instance?(o, guard = nil)
  o.is_a?(String) && @values.any? { |p| p == o }
end

#iterable?(guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

def iterable?(guard = nil)
  true
end

#iterable_type(guard = nil) ⇒ Object



783
784
785
786
# File 'lib/puppet/pops/types/types.rb', line 783

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