Sha256: 3ac554d21cd9fd3356572749918086d3aabe54f55cfff17efe8b0e72e508ba37

Contents?: true

Size: 1.01 KB

Versions: 9

Compression:

Stored size: 1.01 KB

Contents

module Katello
  module Util
    module Data
      def self.array_with_indifferent_access(variable)
        variable.map { |x| x.with_indifferent_access }
      end

      def self.ostructize(obj, options = {})
        options[:prefix_keys] ||= []
        options[:prefix]      ||= '_'

        if obj.is_a? Hash

          ostructized_hash = {}
          obj.each do |key, value|
            if options[:prefix_keys].include? key
              new_key = (options[:prefix].to_s + key.to_s).to_sym
            else
              new_key = key
            end

            if Object.respond_to? new_key
              fail "Error occured while converting Hash to OpenStruct. Key '%s' conflicts with method OpenStruct#%s." % [new_key, new_key]
            end

            ostructized_hash[new_key] = ostructize(value, options)
          end
          return OpenStruct.new ostructized_hash

        elsif obj.is_a? Array

          return obj.map { |r| ostructize(r, options) }

        end
        return obj
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
katello-2.4.5 app/lib/katello/util/data.rb
katello-2.4.4 app/lib/katello/util/data.rb
katello-2.4.3 app/lib/katello/util/data.rb
katello-2.4.2 app/lib/katello/util/data.rb
katello-2.4.1 app/lib/katello/util/data.rb
katello-2.4.0 app/lib/katello/util/data.rb
katello-2.4.0.rc3 app/lib/katello/util/data.rb
katello-2.4.0.rc2 app/lib/katello/util/data.rb
katello-2.4.0.rc1 app/lib/katello/util/data.rb