Class: Epo::Ops::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/epo/ops/util.rb

Class Method Summary collapse

Class Method Details

.find_in_data(epo_hash, path) ⇒ Object

the path should be an array of strings indicating the path you want to go in the hash



5
6
7
# File 'lib/epo/ops/util.rb', line 5

def self.find_in_data(epo_hash, path)
  path.reduce(epo_hash) { |res, c| parse_hash_flat(res, c) }
end

.parse_change_gazette_num(num) ⇒ Object



25
26
27
28
29
# File 'lib/epo/ops/util.rb', line 25

def self.parse_change_gazette_num(num)
  res = /^(?<year>\d{4})\/(?<week>\d{2})$/.match(num)
  return nil if res.nil?
  Date.commercial(Integer(res[:year], 10), week = Integer(res[:week], 10))
end

.parse_hash_flat(hash_layer, target) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/epo/ops/util.rb', line 9

def self.parse_hash_flat(hash_layer, target)
  result = []
  if hash_layer.nil?
    return []
  elsif hash_layer.class == String
    return []
  elsif hash_layer.class == Array
    result.concat(hash_layer.map { |x| parse_hash_flat(x, target) })
  elsif hash_layer[target]
    result << hash_layer[target]
  elsif hash_layer.class == Hash || hash_layer.respond_to?(:to_h)
    result.concat(hash_layer.to_h.map { |_x, y| parse_hash_flat(y, target) })
  end
  result.flatten
end