Module: Katello::Util::Data

Defined in:
app/lib/katello/util/data.rb

Class Method Summary collapse

Class Method Details

.array_with_indifferent_access(variable) ⇒ Object



4
5
6
# File 'app/lib/katello/util/data.rb', line 4

def self.array_with_indifferent_access(variable)
  variable.map { |x| x.with_indifferent_access }
end

.hexdigest(string) ⇒ Object



8
9
10
# File 'app/lib/katello/util/data.rb', line 8

def self.hexdigest(string)
  defined?(ActiveSupport::Digest) ? ActiveSupport::Digest.hexdigest(string) : Digest::MD5.hexdigest(string)
end

.ostructize(obj, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/lib/katello/util/data.rb', line 12

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

  case obj
  when 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

  when Array

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

  end
  return obj
end