module Eye::Utils

Constants

D1
D2
DF

Public Class Methods

deep_clone(value) click to toggle source
# File lib/eye/utils.rb, line 8
def self.deep_clone(value)
  if value.is_a?(Array)
    value.map { |v| deep_clone(v) }
  elsif value.is_a?(Hash)
    value.each_with_object({}) { |(k, v), r| r[deep_clone(k)] = deep_clone(v) }
  else
    value
  end
end
deep_merge!(a, b, allowed_keys = nil) click to toggle source

deep merging b into a (a deeply changed)

# File lib/eye/utils.rb, line 19
def self.deep_merge!(a, b, allowed_keys = nil)
  b.each do |k, v|
    next if allowed_keys && !allowed_keys.include?(k)
    if a[k].is_a?(Hash) && v.is_a?(Hash)
      deep_merge!(a[k], v)
    else
      a[k] = v
    end
  end
  a
end
human_time(unix_time) click to toggle source
# File lib/eye/utils.rb, line 34
def self.human_time(unix_time)
  time = Time.at(unix_time.to_i)
  d1 = time.to_date
  d2 = Time.now.to_date
  time.strftime(d1 == d2 ? D1 : D2)
end
human_time2(unix_time) click to toggle source
# File lib/eye/utils.rb, line 43
def self.human_time2(unix_time)
  Time.at(unix_time.to_i).strftime(DF)
end
load_env(filename) click to toggle source
# File lib/eye/utils.rb, line 47
def self.load_env(filename)
  content = File.read(filename)
  env_vars = content.split("\n")
  h = {}
  env_vars.each do |e|
    e = e.gsub(%r[#.+$], '').strip
    next unless e.include?('=')
    k, v = e.split('=', 2)
    h[k] = v.gsub(%r/^["']+(.*)["']+$/, '\1')
  end
  h
end
wait_signal(timeout = nil, &block) click to toggle source
# File lib/eye/utils.rb, line 60
def self.wait_signal(timeout = nil, &block)
  signal = Celluloid::Condition.new
  block.call(signal)
  signal.wait((timeout || 600).to_f)
  :ok
rescue Celluloid::ConditionError
  :timeouted
end