Class: StringsToYamlEncoder
- Inherits:
-
Object
- Object
- StringsToYamlEncoder
- Defined in:
- lib/yaml_strings/strings_to_yaml_encoder.rb
Instance Attribute Summary collapse
-
#strings_array ⇒ Object
Returns the value of attribute strings_array.
Instance Method Summary collapse
-
#initialize(strings_array = []) ⇒ StringsToYamlEncoder
constructor
A new instance of StringsToYamlEncoder.
-
#nested_hash(keys) ⇒ Object
%{ a b c d } =>.
-
#parse_strings_array(array) ⇒ Object
Returns a hash when given.
-
#parse_strings_key_value(line) ⇒ Object
Validates the format.
- #remove_quotes_from_key_string(key_string) ⇒ Object
- #to_s ⇒ Object
-
#valid_string?(value) ⇒ Boolean
Returns true if the value is a valid string NSString.
-
#valid_terminator?(value) ⇒ Boolean
Returns true if the last character of the string, ignoring spaces, is a semi-colon.
Constructor Details
#initialize(strings_array = []) ⇒ StringsToYamlEncoder
Returns a new instance of StringsToYamlEncoder.
7 8 9 |
# File 'lib/yaml_strings/strings_to_yaml_encoder.rb', line 7 def initialize(strings_array = []) @strings_array = strings_array end |
Instance Attribute Details
#strings_array ⇒ Object
Returns the value of attribute strings_array.
6 7 8 |
# File 'lib/yaml_strings/strings_to_yaml_encoder.rb', line 6 def strings_array @strings_array end |
Instance Method Details
#nested_hash(keys) ⇒ Object
%{ a b c d } =>
{ a => { b => { c => d } } }
34 35 36 37 38 39 40 41 |
# File 'lib/yaml_strings/strings_to_yaml_encoder.rb', line 34 def nested_hash(keys) first = keys.shift return first if keys.empty? # base case hash = Hash.new hash[first] = nested_hash(keys) hash end |
#parse_strings_array(array) ⇒ Object
Returns a hash when given
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/yaml_strings/strings_to_yaml_encoder.rb', line 16 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_from_key_string(key_string) keys = key_string.split('.') hash.merge!(nested_hash(keys + [value])) end end |
#parse_strings_key_value(line) ⇒ Object
Validates the format
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/yaml_strings/strings_to_yaml_encoder.rb', line 68 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 |
#remove_quotes_from_key_string(key_string) ⇒ Object
61 62 63 64 65 |
# File 'lib/yaml_strings/strings_to_yaml_encoder.rb', line 61 def remove_quotes_from_key_string(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 end |
#to_s ⇒ Object
11 12 13 |
# File 'lib/yaml_strings/strings_to_yaml_encoder.rb', line 11 def to_s parse_strings_array(@strings_array) end |
#valid_string?(value) ⇒ Boolean
Returns true if the value is a valid string NSString
See developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/OldStylePlists/OldStylePLists.html#//apple_ref/doc/uid/20001012-BBCBDBJE for more info
47 48 49 50 51 |
# File 'lib/yaml_strings/strings_to_yaml_encoder.rb', line 47 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.
55 56 57 58 59 |
# File 'lib/yaml_strings/strings_to_yaml_encoder.rb', line 55 def valid_terminator?(value) return false if value.nil? || value == "" value.gsub(' ', '')[-1] == ";" end |