module ASUtils

Public Class Methods

as_array(thing) click to toggle source
# File lib/aspace_client/asutils.rb, line 18
def self.as_array(thing)
  return [] if thing.nil?
  thing.kind_of?(Array) ? thing : [thing]
end
deep_merge(hash1, hash2) click to toggle source

Recursively overlays hash2 onto hash 1

# File lib/aspace_client/asutils.rb, line 108
def self.deep_merge(hash1, hash2)
  target = hash1.dup
  hash2.keys.each do |key|
    if hash2[key].is_a? Hash and hash1[key].is_a? Hash
      target[key] = self.deep_merge(target[key], hash2[key])
      next
    end
    target[key] = hash2[key]
  end
  target
end
dump_diagnostics(exception = nil) click to toggle source
# File lib/aspace_client/asutils.rb, line 83
  def self.dump_diagnostics(exception = nil)
    diagnostics = self.get_diagnostics( exception ) 
    tmp = File.join(Dir.tmpdir, "aspaue_diagnostic_#{Time.now.to_i}.txt")
    File.open(tmp, "w") do |fh|
      fh.write(JSON.pretty_generate(diagnostics))
    end

    msg = <<EOF
A trace file has been written to the following location: #{tmp}

This file contains information that will assist developers in diagnosing
problems with your ArchivesSpace installation.  Please review the file's
contents for sensitive information (such as passwords) that you might not
want to share.
EOF

    $stderr.puts("=" * 72)
    $stderr.puts(msg)
    $stderr.puts("=" * 72)

    raise exception if exception
  end
extract_nested_strings(coll) click to toggle source
# File lib/aspace_client/asutils.rb, line 73
def self.extract_nested_strings(coll)
  if coll.is_a?(Hash)
    coll.values.map {|v| self.extract_nested_strings(v)}.flatten.compact
  elsif coll.is_a?(Array)
    coll.map {|v| self.extract_nested_strings(v)}.flatten.compact
  else
    coll
  end
end
find_base_directory(root = nil) click to toggle source
# File lib/aspace_client/asutils.rb, line 55
def self.find_base_directory(root = nil)
  [ File.join(*[File.dirname(__FILE__), "..", root].compact)].find {|dir|
    Dir.exists?(dir)
  }
end
find_local_directories(base = nil, *plugins) click to toggle source
# File lib/aspace_client/asutils.rb, line 62
def self.find_local_directories(base = nil, *plugins)
  base_directory = self.find_base_directory
  Array(plugins).map { |plugin| File.join(*[base_directory, "plugins", plugin, base].compact) }
end
find_locales_directories(base = nil) click to toggle source
# File lib/aspace_client/asutils.rb, line 68
def self.find_locales_directories(base = nil)
  [File.join(*[self.find_base_directory("common"), "locales", base].compact)]
end
json_parse(s) click to toggle source
# File lib/aspace_client/asutils.rb, line 40
def self.json_parse(s)
  JSON.parse(s, :max_nesting => false, :create_additions => false)
end
jsonmodels_to_hashes(elt) click to toggle source
# File lib/aspace_client/asutils.rb, line 24
def self.jsonmodels_to_hashes(elt)

  if elt.is_a?(JSONModelType)
    elt = elt.to_hash(:raw)
  end

  if elt.is_a?(Hash)
    Hash[elt.map {|k, v| [k, self.jsonmodels_to_hashes(v)]}]
  elsif elt.is_a?(Array)
    elt.map {|v| self.jsonmodels_to_hashes(v)}
  else
    elt
  end
end
keys_as_strings(hash) click to toggle source
# File lib/aspace_client/asutils.rb, line 7
def self.keys_as_strings(hash)
  result = {}

  hash.each do |key, value|
    result[key.to_s] = value.is_a?(Date) ? value.to_s : value 
  end

  result
end
load_plugin_gems(context) click to toggle source
# File lib/aspace_client/asutils.rb, line 121
def self.load_plugin_gems(context)
  ASUtils.find_local_directories.each do |plugin|
    gemfile = File.join(plugin, 'Gemfile')
    if File.exists?(gemfile)
      context.instance_eval(File.read(gemfile))
    end
  end
end
to_json(obj, opts = {}) click to toggle source
# File lib/aspace_client/asutils.rb, line 46
def self.to_json(obj, opts = {})
  if obj.respond_to?(:jsonize)
    obj.jsonize(opts.merge(:max_nesting => false))
  else
    obj.to_json(opts.merge(:max_nesting => false))
  end
end
wrap(object) click to toggle source

Borrowed from: file activesupport/lib/active_support/core_ext/array/wrap.rb, line 36

# File lib/aspace_client/asutils.rb, line 132
def self.wrap(object)
  if object.nil?
    []
  elsif object.respond_to?(:to_ary)
    object.to_ary || [object]
  else
    [object]
  end
end