Module: ASUtils

Defined in:
lib/aspace_client/asutils.rb

Class Method Summary collapse

Class Method Details

.as_array(thing) ⇒ Object



18
19
20
21
# 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) ⇒ Object

Recursively overlays hash2 onto hash 1



108
109
110
111
112
113
114
115
116
117
118
# 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) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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 = "A trace file has been written to the following location: \#{tmp}\n\nThis file contains information that will assist developers in diagnosing\nproblems with your ArchivesSpace installation.  Please review the file's\ncontents for sensitive information (such as passwords) that you might not\nwant to share.\n"

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

  raise exception if exception
end

.extract_nested_strings(coll) ⇒ Object



73
74
75
76
77
78
79
80
81
# 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) ⇒ Object



55
56
57
58
59
# 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) ⇒ Object



62
63
64
65
# 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) ⇒ Object



68
69
70
# 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) ⇒ Object



40
41
42
# 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) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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) ⇒ Object



7
8
9
10
11
12
13
14
15
# 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) ⇒ Object



121
122
123
124
125
126
127
128
# 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 = {}) ⇒ Object



46
47
48
49
50
51
52
# 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) ⇒ Object

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



132
133
134
135
136
137
138
139
140
# 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