Sha256: 50fc31fc46488ada15e3f80af00fcbe89b8244031699f35e3f691a3323932ffd

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

#
# Copyright 2014 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public
# License as published by the Free Software Foundation; either version
# 2 of the License (GPLv2) or (at your option) any later version.
# There is NO WARRANTY for this software, express or implied,
# including the implied warranties of MERCHANTABILITY,
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
# have received a copy of GPLv2 along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.

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

1 entries across 1 versions & 1 rubygems

Version Path
katello-2.2.2 app/lib/katello/util/data.rb