Module: Eye::Utils

Defined in:
lib/eye/utils.rb

Defined Under Namespace

Classes: AliveArray, Tail

Constant Summary collapse

D1 =
'%H:%M'.freeze
D2 =
'%b%d'.freeze
DF =
'%d %b %H:%M'.freeze

Class Method Summary collapse

Class Method Details

.deep_clone(value) ⇒ Object



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

deep merging b into a (a deeply changed)



19
20
21
22
23
24
25
26
27
28
29
# 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) ⇒ Object



34
35
36
37
38
39
# 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) ⇒ Object



43
44
45
# 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) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# 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) ⇒ Object



60
61
62
63
64
65
66
67
# 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