File: deep_to_i.rb

package info (click to toggle)
puppet-module-voxpupuli-elasticsearch 9.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,496 kB
  • sloc: ruby: 9,906; sh: 392; makefile: 4
file content (22 lines) | stat: -rw-r--r-- 731 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# frozen_string_literal: true

module Puppet_X # rubocop:disable Style/ClassAndModuleCamelCase
  # Custom Elastic functions
  module Elastic
    # This ugly hack is required due to the fact Puppet passes in the
    # puppet-native hash with stringified numerics, which causes the
    # decoded JSON from the Elasticsearch API to be seen as out-of-sync
    # when the parsed template hash is compared against the puppet hash.
    def self.deep_to_i(obj)
      if obj.is_a?(String) && obj =~ %r{^-?[0-9]+$}
        obj.to_i
      elsif obj.is_a? Array
        obj.map { |element| deep_to_i(element) }
      elsif obj.is_a? Hash
        obj.merge(obj) { |_key, val| deep_to_i(val) }
      else
        obj
      end
    end
  end
end