Module: ImmosquareYaml::SharedMethods

Included in:
ImmosquareYaml, Translate
Defined in:
lib/immosquare-yaml/shared_methods.rb

Constant Summary collapse

INDENT_SIZE =
2
NOTHING =
"".freeze
SPACE =
" ".freeze
NEWLINE =
"\n".freeze
SIMPLE_QUOTE =
"'".freeze
DOUBLE_QUOTE =
'"'.freeze
DOUBLE_SIMPLE_QUOTE =
"''".freeze
WEIRD_QUOTES_REGEX =
/‘|’|“|”|‛|‚|„|‟|#{Regexp.quote(DOUBLE_SIMPLE_QUOTE)}/.freeze
YML_SPECIAL_CHARS =
["-", "`", "{", "}", "|", "[", "]", ">", ":", "\"", "'", "*", "=", "%", ",", "!", "?", "&", "#", "@"].freeze
RESERVED_KEYS =
[
  "yes", "no", "on", "off", "true", "false",
  "Yes", "No", "On", "Off", "True", "False",
  "YES", "NO", "ON", "OFF", "TRUE", "FALSE"
].freeze

Instance Method Summary collapse

Instance Method Details

#deep_transform_values(hash, &block) ⇒ Object

##

Deep transform values resursively

##


22
23
24
25
26
27
28
29
30
# File 'lib/immosquare-yaml/shared_methods.rb', line 22

def deep_transform_values(hash, &block)
  hash.transform_values do |value|
    if value.is_a?(Hash)
      deep_transform_values(value, &block)
    else
      block.call(value)
    end
  end
end

#sort_by_key(hash, recursive = false, &block) ⇒ Object

##

sort_by_key Function Purpose: Sort a hash by its keys, optionally recursively, with case-insensitive comparison and stripping of double quotes.

#


37
38
39
40
41
42
43
# File 'lib/immosquare-yaml/shared_methods.rb', line 37

def sort_by_key(hash, recursive = false, &block)
  block ||= proc {|a, b| a.to_s.downcase.gsub("\"", "") <=> b.to_s.downcase.gsub("\"", "") }
  hash.keys.sort(&block).each_with_object({}) do |key, seed|
    seed[key] = hash[key]
    seed[key] = sort_by_key(seed[key], true, &block) if recursive && seed[key].is_a?(Hash)
  end
end