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.



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

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

Instance Attribute Details

#case_insensitiveObject (readonly)



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

def case_insensitive
  @case_insensitive
end

#valuesObject (readonly)



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

def values
  @values
end

Class Method Details

.register_ptype(loader, ir) ⇒ Object



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

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)


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

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



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

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

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#generalizeObject



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

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



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

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

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

Returns:

  • (Boolean)


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

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)


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

def iterable?(guard = nil)
  true
end

#iterable_type(guard = nil) ⇒ Object



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

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