Module: ASUtils

Defined in:
common/asutils.rb

Class Method Summary (collapse)

Class Method Details

+ (Object) as_array(thing)



20
21
22
23
# File 'common/asutils.rb', line 20

def self.as_array(thing)
  return [] if thing.nil?
  thing.kind_of?(Array) ? thing : [thing]
end

+ (Object) deep_merge(hash1, hash2)

Recursively overlays hash2 onto hash 1



157
158
159
160
161
162
163
164
165
166
167
# File 'common/asutils.rb', line 157

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

+ (Object) dump_diagnostics(exception = nil)



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'common/asutils.rb', line 131

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

+ (Object) extract_nested_strings(coll)



102
103
104
105
106
107
108
109
110
# File 'common/asutils.rb', line 102

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

+ (Object) find_base_directory(root = nil)



81
82
83
84
85
86
87
# File 'common/asutils.rb', line 81

def self.find_base_directory(root = nil)
  [java.lang.System.get_property("ASPACE_LAUNCHER_BASE"),
   java.lang.System.get_property("catalina.base"),
   File.join(*[File.dirname(__FILE__), "..", root].compact)].find {|dir|
    dir && Dir.exists?(dir)
  }
end

+ (Object) find_local_directories(base = nil, *plugins)



90
91
92
93
94
# File 'common/asutils.rb', line 90

def self.find_local_directories(base = nil, *plugins)
  plugins = AppConfig[:plugins] if plugins.empty?
  base_directory = self.find_base_directory
  Array(plugins).map { |plugin| File.join(*[base_directory, "plugins", plugin, base].compact) }
end

+ (Object) find_locales_directories(base = nil)



97
98
99
# File 'common/asutils.rb', line 97

def self.find_locales_directories(base = nil)
  [File.join(*[self.find_base_directory("common"), "locales", base].compact)]
end

+ (Object) get_diagnostics(exception = nil)



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'common/asutils.rb', line 112

def self.get_diagnostics(exception = nil )
   runtime = java.lang.Runtime.getRuntime
  {
     :version =>ASConstants.VERSION,  
     :environment => java.lang.System.getenv.to_hash,
     :jvm_properties => java.lang.System.getProperties.to_hash,
     :globals => Hash[global_variables.map {|v| [v, eval(v.to_s)]}],
     :appconfig => defined?(AppConfig) ? AppConfig.dump_sanitized : "not loaded",
     :memory => {
       :free => runtime.freeMemory,
       :max => runtime.maxMemory,
       :total => runtime.totalMemory
     },
     :cpu_count => runtime.availableProcessors,
     :exception => exception && {:msg => exception, :backtrace => exception.backtrace}
   }
  
end

+ (Object) json_parse(s)



42
43
44
# File 'common/asutils.rb', line 42

def self.json_parse(s)
  JSON.parse(s, :max_nesting => false, :create_additions => false)
end

+ (Object) jsonmodels_to_hashes(elt)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'common/asutils.rb', line 26

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

+ (Object) keys_as_strings(hash)



9
10
11
12
13
14
15
16
17
# File 'common/asutils.rb', line 9

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

+ (Object) load_plugin_gems(context)



170
171
172
173
174
175
176
177
# File 'common/asutils.rb', line 170

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

+ (Object) search_nested(hash, key)

find a nested key inside a hash



192
193
194
195
196
197
198
199
200
# File 'common/asutils.rb', line 192

def self.search_nested(hash,key)
  if hash.respond_to?(:key?) && hash.key?(key)
    hash[key]
  elsif hash.respond_to?(:each)
    obj = nil
    hash.find{ |*a| obj=self.search_nested(a.last,key) }
    obj 
  end
end

+ (Object) tempfile(base)

A bit funny to wrap this ourselves, but there’s an interesting case when running under rspec.

Rspec reseeds the random number generator at the beginning of every suite run, which means that Tempfile’s “random” filenames are often IDENTICAL between subsequent Tempfile invocations across test runs.

This shouldn’t really matter, except when this happens:

  • Test1 runs and produces tempfile “somerandomfile”, which gets closed and unlinked.

  • Test2 runs and gets given “somerandomfile” too. It starts working with it.

  • Then, bam! Garbage collection runs, and finalizes the object from Test1. This unlinks the underlying temp file while Test2 is still using it!

So yeah, not cool. We try to inject a little randomness into “base” to avoid these sort of shenanigans, even though it really shouldn’t matter in production.



67
68
69
# File 'common/asutils.rb', line 67

def self.tempfile(base)
  Tempfile.new("#{base}_#{java.lang.System.currentTimeMillis}")
end

+ (Object) to_json(obj, opts = {})



72
73
74
75
76
77
78
# File 'common/asutils.rb', line 72

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

+ (Object) wrap(object)

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



181
182
183
184
185
186
187
188
189
# File 'common/asutils.rb', line 181

def self.wrap(object)
  if object.nil?
    []
  elsif object.respond_to?(:to_ary)
    object.to_ary || [object]
  else
    [object]
  end
end