Class: Puppet::Pops::Types::Enumeration

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/types/enumeration.rb

Overview

The Enumeration class provides default Enumerable::Enumerator creation for Puppet Programming Language runtime objects that supports the concept of enumeration.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.enumerator(o) ⇒ Object

Produces an Enumerable::Enumerator for Array, Hash, Integer, Integer Range, and String.



7
8
9
10
# File 'lib/puppet/pops/types/enumeration.rb', line 7

def self.enumerator(o)
  @@singleton ||= new
  @@singleton.enumerator(o)
end

Instance Method Details

#enumerator(o) ⇒ Object

Produces an Enumerator for Array, Hash, Integer, Integer Range, and String.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/puppet/pops/types/enumeration.rb', line 14

def enumerator(o)
  case o
  when String
    x = o.chars
    # Ruby 1.8.7 returns Enumerable::Enumerator, Ruby 1.8.9 Enumerator, and 2.0.0 an Array
    x.is_a?(Array) ? x.each : x
  when Integer
    o.times
  when Array
    o.each
  when Hash
    o.each
  when Puppet::Pops::Types::PIntegerType
    # Not enumerable if representing an infinite range
    return nil if o.to.nil? || o.from.nil?
    o.each
  else
    nil
  end
end