Class: YamlToStringsEncoder

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yaml_hash = {}) ⇒ YamlToStringsEncoder

Returns a new instance of YamlToStringsEncoder.



3
4
5
# File 'lib/yaml_strings/yaml_to_strings_encoder.rb', line 3

def initialize(yaml_hash = {})
  @yaml_hash = yaml_hash
end

Instance Attribute Details

#yaml_hashObject

Returns the value of attribute yaml_hash.



2
3
4
# File 'lib/yaml_strings/yaml_to_strings_encoder.rb', line 2

def yaml_hash
  @yaml_hash
end

Class Method Details

.convert_hash_to_dict_property(hsh, prev_keys = nil) ⇒ Object

Returns an array of dict properties as strings

hsh - a hash to convert into the string representation prev_keys - a string representing the previous keys of enclosing hashes

Ex: { a: :b } # => “a” = “b”; Ex: { a: { b: :c } } # => “a.b” = “c”;



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/yaml_strings/yaml_to_strings_encoder.rb', line 20

def convert_hash_to_dict_property(hsh, prev_keys = nil)
  result = []
  hsh.each_pair {|k,v|
    if v.is_a?(Hash)
      new_prev_keys  = "#{[prev_keys, k].compact.join('.')}"
      result        += convert_hash_to_dict_property(v, new_prev_keys)
    else
      result        += ["\"#{[prev_keys, k].compact.join('.')}\" = \"#{v}\" ;"]
    end
  }
  result
end

.to_property_dict_string(array_of_dict_propertes) ⇒ Object

Returns a string representing an old ASCII style property list



34
35
36
# File 'lib/yaml_strings/yaml_to_strings_encoder.rb', line 34

def to_property_dict_string(array_of_dict_propertes)
  (["{"] + array_of_dict_propertes + ["}", nil]).join("\n")
end

Instance Method Details

#to_sObject



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

def to_s
  array = YamlToStringsEncoder.convert_hash_to_dict_property(yaml_hash)
  YamlToStringsEncoder.to_property_dict_string(array)
end