From 3e34f2eaa48f3b162c378f30ef1d07f163704ec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Such=C3=BD?= Date: Thu, 11 Oct 2012 16:18:13 +0200 Subject: [PATCH] 860952 - do not call with_indifferent_access on Array created new function array_with_indifferent_access and value on line 384 passed to this function. Additionaly all code in this module which do the same, flip to calling this function, which result in shorter code. --- src/lib/resources/candlepin.rb | 20 +++++++++++--------- src/lib/util/data.rb | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 src/lib/util/data.rb diff --git a/src/lib/resources/candlepin.rb b/src/lib/resources/candlepin.rb index 138a38877bc..59236a2cd4d 100644 --- a/src/lib/resources/candlepin.rb +++ b/src/lib/resources/candlepin.rb @@ -10,6 +10,8 @@ # have received a copy of GPLv2 along with this software; if not, see # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. +require 'util/data' + module Resources module Candlepin @@ -146,7 +148,7 @@ def regenerate_identity_certificates uuid def entitlements uuid response = Candlepin::CandlepinResource.get(join_path(path(uuid), 'entitlements'), self.default_headers).body - JSON.parse(response).collect { |e| e.with_indifferent_access } + Util::Data::array_with_indifferent_access JSON.parse(response) end def consume_entitlement uuid, pool, quantity = nil @@ -172,7 +174,7 @@ def remove_certificate uuid, serial_id def guests uuid response = Candlepin::CandlepinResource.get(join_path(path(uuid), 'guests'), self.default_headers).body - JSON.parse(response).map { |e| e.with_indifferent_access } + Util::Data::array_with_indifferent_access JSON.parse(response) rescue => e return [] end @@ -200,7 +202,7 @@ def compliance uuid def events uuid response = Candlepin::CandlepinResource.get(join_path(path(uuid), 'events'), self.default_headers).body unless response.empty? - JSON.parse(response).collect {|s| s.with_indifferent_access} + Util::Data::array_with_indifferent_access JSON.parse(response) else return [] end @@ -272,7 +274,7 @@ def destroy_imports organization_name def imports organization_name imports_json = self.get(join_path(path(organization_name), 'imports'), self.default_headers) - JSON.parse(imports_json).collect {|s| s.with_indifferent_access} + Util::Data::array_with_indifferent_access JSON.parse(imports_json) end def pools key, filter = {} @@ -281,12 +283,12 @@ def pools key, filter = {} else jsonStr = self.get(join_path('candlepin', 'pools') + hash_to_query(filter), self.default_headers).body end - JSON.parse(jsonStr).collect {|p| p.with_indifferent_access } + Util::Data::array_with_indifferent_access JSON.parse(jsonStr) end def statistics key jsonStr = self.get(join_path(path(key), 'statistics'), self.default_headers).body - JSON.parse(jsonStr).collect {|p| p.with_indifferent_access } + Util::Data::array_with_indifferent_access JSON.parse(jsonStr) end def generate_ueber_cert key @@ -308,7 +310,7 @@ def get_ueber_cert_pkcs12 key, name = nil, password = nil def events key response = self.get(join_path(path(key), 'events'), self.default_headers).body - JSON.parse(response).collect { |e| e.with_indifferent_access } + Util::Data::array_with_indifferent_access JSON.parse(response) end def service_levels uuid @@ -379,7 +381,7 @@ class Pool < CandlepinResource class << self def find pool_id pool_json = self.get(path(pool_id), self.default_headers).body - JSON.parse(pool_json).with_indifferent_access + Util::Data::array_with_indifferent_access JSON.parse(pool_json) end def get_for_owner owner_key @@ -506,7 +508,7 @@ def get id=nil products_json = super(path(id), self.default_headers).body products = JSON.parse(products_json) products = [products] unless id.nil? - products.collect {|p| p.with_indifferent_access } + Util::Data::array_with_indifferent_access products end diff --git a/src/lib/util/data.rb b/src/lib/util/data.rb new file mode 100644 index 00000000000..33016ccd661 --- /dev/null +++ b/src/lib/util/data.rb @@ -0,0 +1,21 @@ +# +# Copyright 2012 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 Util + module Data + + def self.array_with_indifferent_access variable + variable.map { |x| x.with_indifferent_access } + end + + end +end