Module: Eye::Utils

Defined in:
lib/eye/utils.rb

Defined Under Namespace

Classes: AliveArray, CelluloidChain, Tail

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.deep_clone(value) ⇒ Object


8
9
10
11
12
13
14
# File 'lib/eye/utils.rb', line 8

def self.deep_clone(value)
  case
    when value.is_a?(Array) then value.map{|v| deep_clone(v) }
    when value.is_a?(Hash) then value.inject({}){|r, (k, v)| r[ deep_clone(k) ] = deep_clone(v); r }
    else value
  end
end

.deep_merge!(a, b, allowed_keys = nil) ⇒ Object

deep merging b into a (a deeply changed)


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/eye/utils.rb', line 17

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


32
33
34
35
36
37
# File 'lib/eye/utils.rb', line 32

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


41
42
43
# File 'lib/eye/utils.rb', line 41

def self.human_time2(unix_time)
  Time.at(unix_time.to_i).strftime(DF)
end

.load_env(filename) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/eye/utils.rb', line 45

def self.load_env(filename)
  content = File.read(filename)
  env_vars = content.split("\n")
  h = {}
  env_vars.each do |e|
    e = e.gsub(/#.+$/, '').strip
    next unless e.include?('=')
    k, v = e.split('=', 2)
    h[k] = v
  end
  h
end