Module: HmcString

Defined in:
lib/HMC/HmcString.rb

Instance Method Summary collapse

Instance Method Details

#make_string(parameter, string) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/HMC/HmcString.rb', line 76

def make_string(parameter, string)

  unless string.nil?
    string = parameter + '=' + string
    if string.include?('"') || string.include?(',')
      string = '"' + string + '"'
    end
  end

  string
end

#parse(string) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/HMC/HmcString.rb', line 3

def parse(string)

  my_result = {}
  key = ''
  value = ''
  inside_index = 1
  insite_quotation_mark = 0

  i = 0
  while i < string.length

    if string[i] == '"' && string[i + 1] == '"'
      i += 2
      value << '""'
      next
    end

    if string[i] == '"' && string[i + 1] != '"'
      insite_quotation_mark.zero? ? insite_quotation_mark = 1 : insite_quotation_mark = 0
      i += 1
      next
    end

    if string[i] == ',' && insite_quotation_mark.zero?
      my_result[key] = value
      key = ''
      value = ''
      inside_index = 1
      i += 1
      next
    end

    if string[i] == '=' && inside_index == 1
      inside_index = 0
      i += 1
      next
    end

    inside_index == 1 ? key << string[i] : value << string[i]
    i += 1
  end
  my_result[key]= value # last key and value (loop is ended as we haven't last ",")
  my_result
end

#parse_value(string) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/HMC/HmcString.rb', line 48

def parse_value(string)
  i = 0
  insite_quotation_mark = 0
  value = ''
  array = []

  while i < string.length
    if string[i] == '"' && string[i + 1] == '"'
      insite_quotation_mark.zero? ? insite_quotation_mark = 1 : insite_quotation_mark = 0
      i += 2
      next
    end

    if string[i] == ',' && insite_quotation_mark.zero?
      array.push(value)
      value = ''
      i += 1
      next
    end

    value << string[i]
    i += 1
  end

  array.push(value)
  array
end