Class: StringsToHashEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/yaml_strings/strings_to_hash_encoder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strings_array = []) ⇒ StringsToHashEncoder

Returns a new instance of StringsToHashEncoder.



7
8
9
# File 'lib/yaml_strings/strings_to_hash_encoder.rb', line 7

def initialize(strings_array = [])
  @strings_array = strings_array
end

Instance Attribute Details

#strings_arrayObject

Returns the value of attribute strings_array.



6
7
8
# File 'lib/yaml_strings/strings_to_hash_encoder.rb', line 6

def strings_array
  @strings_array
end

Class Method Details

.nested_hash(keys) ⇒ Object

%{ a b c d } =>

{ a => { b => { c => d } } }



41
42
43
# File 'lib/yaml_strings/strings_to_hash_encoder.rb', line 41

def nested_hash(keys)
  keys.reverse.inject { |hsh, elem| { elem => hsh } }
end

.parse_strings_array(array) ⇒ Object

Returns a hash when given

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/yaml_strings/strings_to_hash_encoder.rb', line 17

def parse_strings_array(array)
  raise ArgumentError.new("Invalid strings format: missing '{\\n'") unless /{/.match(array.shift)
  raise ArgumentError.new("Invalid strings format: missing '}'")    unless /}/.match(array.pop)

  array.inject({}) do |hash,line|
    key_string, value = parse_strings_key_value(line)

    key_string = remove_quotes(key_string)
    value      = remove_quotes(value)
    keys = key_string.split('.') + [value]

    recursive_merge(hash, nested_hash(keys))
  end
end

.parse_strings_key_value(line) ⇒ Object

Validates the format

Raises:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/yaml_strings/strings_to_hash_encoder.rb', line 71

def parse_strings_key_value(line)
  key_string, value = line.split('=')

  key_string.strip!
  value.strip!

  raise InvalidValueError.new("Invalid strings format: Missing value for line after '=': '#{line}'")     unless valid_string?(value)
  raise InvalidKeyError.new("Invalid strings format: Missing key for line before '=': '#{line}'")        unless valid_string?(key_string)
  raise InvalidTerminatorError.new("Invalid strings format: Missing terminator ';' for line: '#{line}'") unless valid_terminator?(value)

  value = value[0..-2] # Removes the ; from the value

  return key_string, value
end

.recursive_merge(a, b) ⇒ Object



33
34
35
# File 'lib/yaml_strings/strings_to_hash_encoder.rb', line 33

def recursive_merge(a, b)
  a.merge(b) {|key, a_item, b_item| recursive_merge(a_item, b_item) } 
end

.remove_quotes(key_string) ⇒ Object



63
64
65
66
67
68
# File 'lib/yaml_strings/strings_to_hash_encoder.rb', line 63

def remove_quotes(key_string)
  key_string = key_string.strip[1..-1] if key_string.strip[0]  == '"'
  key_string = key_string.strip[0..-2] if key_string.strip[-1] == '"'
  key_string.strip!
  key_string
end

.valid_string?(value) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/yaml_strings/strings_to_hash_encoder.rb', line 49

def valid_string?(value)
  (value       =~ /\s*"[^"]*"\s*/ ||
   value.strip =~ /\A\S+\Z/) &&
   value.strip != ';'                # handles case where no text in the value
end

.valid_terminator?(value) ⇒ Boolean

Returns true if the last character of the string, ignoring spaces, is a semi-colon.

Returns:

  • (Boolean)


57
58
59
60
61
# File 'lib/yaml_strings/strings_to_hash_encoder.rb', line 57

def valid_terminator?(value)
  return false if value.nil? || value == ""

  value.gsub(' ', '')[-1] == ";"
end

Instance Method Details

#to_hashObject



11
12
13
# File 'lib/yaml_strings/strings_to_hash_encoder.rb', line 11

def to_hash
  StringsToHashEncoder.parse_strings_array(@strings_array)
end