Module: TurbotRunner::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/turbot_runner/utils.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.deep_copy(thing) ⇒ Object



5
6
7
# File 'lib/turbot_runner/utils.rb', line 5

def deep_copy(thing)
  Marshal.load(Marshal.dump(thing))
end

.flatten(hash) ⇒ Object

This turns a hash of the form:

{ 'a' => { 'b' => { 'c' => '123', 'd' => '124', }, 'e' => { 'f' => '156', } } }

into a hash of the form:

{ 'a.b.c' => '123', 'a.b.d' => '124', 'a.e.f' => '156', }



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/turbot_runner/utils.rb', line 30

def flatten(hash)
  pairs = []

  hash.each do |k, v|
    case v
    when Hash
      flatten(v).each do |k1, v1|
        pairs << ["#{k}.#{k1}", v1]
      end
    else
      pairs << [k, v]
    end
  end

  Hash[pairs]
end

Instance Method Details

#deep_copy(thing) ⇒ Object



5
6
7
# File 'lib/turbot_runner/utils.rb', line 5

def deep_copy(thing)
  Marshal.load(Marshal.dump(thing))
end

#flatten(hash) ⇒ Object

This turns a hash of the form:

{ 'a' => { 'b' => { 'c' => '123', 'd' => '124', }, 'e' => { 'f' => '156', } } }

into a hash of the form:

{ 'a.b.c' => '123', 'a.b.d' => '124', 'a.e.f' => '156', }



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/turbot_runner/utils.rb', line 30

def flatten(hash)
  pairs = []

  hash.each do |k, v|
    case v
    when Hash
      flatten(v).each do |k1, v1|
        pairs << ["#{k}.#{k1}", v1]
      end
    else
      pairs << [k, v]
    end
  end

  Hash[pairs]
end