Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/std++/main.rb

Overview

Ruby default Hash class

Debug Tools collapse

Dev Tools collapse

Class Method Details

.from_one_template(tmpl) ⇒ Object

Generate Hash from ONe template string



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/std++/main.rb', line 98

def self.from_one_template tmpl
  lines = tmpl.split("\n")
  res, i = {}, 0
  while i < lines.length do
    raise StandardError.new("Template Syntax Error: Bracket isn't paired") if lines[i].nil?

    puts i, lines[i]
    line = lines[i]
    key, value = line.split("=").map { | el | el.strip }
    if value != '[' then
      res[key] = value[1...(value.length - 1)]
    else
      res[key] = {}
      i += 1
      until lines[i].strip == ']' do
        raise StandardError.new("Template Syntax Error: Bracket isn't paired") if lines[i].nil?

        puts i, lines[i]
        line = lines[i]
        sub_key, value = line.split("=").map { | el | el.strip }
        value.delete_suffix! ','
        puts value, value.length
        res[key][sub_key] = value[1...(value.length - 1)]
        i += 1
      end
    end
    i += 1
  end
  res
end

Instance Method Details

#debug_outObject

Returns hash as 'pretty generated' JSON String with replaced JSON(':' to '=>' and 'null' to 'nil')



14
15
16
# File 'lib/std++/main.rb', line 14

def debug_out
  JSON.pretty_generate(self).gsub("\": ", "\" => ").gsub(" => null", " => nil")
end

#deep_merge(second) ⇒ Object

Generic #merge method better version



130
131
132
133
# File 'lib/std++/main.rb', line 130

def deep_merge(second)
  merger = proc { | _, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
  self.merge(second, &merger)
end

#get(*keys) ⇒ Array

Returns array of values with given keys

Parameters:

  • keys (Array)
    • Array of values

Returns:



55
56
57
58
59
# File 'lib/std++/main.rb', line 55

def get *keys
  keys.collect do | key |
    self[key]
  end
end

#keys_to_i!Hash

Converts all keys to Integer

Returns:



45
46
47
48
49
50
# File 'lib/std++/main.rb', line 45

def keys_to_i!
  self.keys.each do |key|
    self[key.to_i] = self.delete key if key.class != Integer
  end
  self
end

#outObject

Returns hash as 'pretty generated' JSON String



9
10
11
# File 'lib/std++/main.rb', line 9

def out
  JSON.pretty_generate(self)
end

#to_one_templateObject

Transforms Hash to OpenNebula Template. Only two layers allowed => 2, 'DISK' => {'SIZE' => '64'} -> CPU = “2” DISK = [

SIZE = "64" ]

Examples:

VM hash to template



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/std++/main.rb', line 74

def to_one_template
  result = ""
  self.each do | key, value |
    key = key.to_s.upcase
    if value.class == String || value.class == Integer then
      result += "#{key}=\"#{value.to_s.gsub("\"", "\\\"")}\"\n"
    elsif value.class == Hash then
      result += "#{key}=[\n"
      size = value.size - 1
      value.each_with_index do | el, i |
        result += "  #{el[0]}=\"#{el[1].to_s.gsub("\"", "\\\"")}\"#{i == size ? '' : ",\n"}"
      end
      result += " ]\n"
    elsif value.class == Array then
      value.each do | el |
        result += { key => el }.to_one_template + "\n"
      end
    end
  end
  result.chomp!
  result.nil? ? "" : result
end

#to_s!Hash

Replaces all keys with String keys

Returns:



33
34
35
36
37
38
39
40
41
# File 'lib/std++/main.rb', line 33

def to_s!
  self.keys.each do |key|
    self[key.to_s] = self.delete key if key.class != String
    if self[key.to_s].class == Hash then
      self[key.to_s].to_s!
    end
  end
  self
end

#to_sym!Hash

Replaces string keys with Symbol keys

Returns:



24
25
26
27
28
29
# File 'lib/std++/main.rb', line 24

def to_sym!
  self.keys.each do |key|
    self[key.to_sym] = self.delete key if key.class == String
  end
  self
end

#without(*keys) ⇒ Object

Returns Hash copy without given keys



62
63
64
65
66
# File 'lib/std++/main.rb', line 62

def without(*keys)
  cpy = self.dup
  keys.each { |key| cpy.delete(key) }
  cpy
end