module Kentico::Kontent::Utils

Public Instance Methods

normalize_object(object) click to toggle source

Transforms any object into easily seriazible format. OpenStruct is converted to hash and Symbol keys are transformed to string keys.

# File lib/kontent-jekyll/utils/normalize_object.rb, line 8
def normalize_object(object)
  stringify_all_keys(to_hash(object))
end

Private Instance Methods

array_values_to_hash(array) click to toggle source
# File lib/kontent-jekyll/utils/normalize_object.rb, line 76
def array_values_to_hash(array)
  array.map do |item|
    case item
    when Array then array_values_to_hash item
    when Hash then hash_values_to_hash item
    when OpenStruct then open_struct_values_to_hash item
    else item
    end
  end
end
hash_values_to_hash(hash) click to toggle source
# File lib/kontent-jekyll/utils/normalize_object.rb, line 22
def hash_values_to_hash(hash)
  hash.reduce({}) do |reduced, pair|
    key = pair[0]
    value = pair[1]
    new_pair = { key => to_hash(value) }
    reduced.merge(new_pair)
  end
end
open_struct_values_to_hash(struct) click to toggle source
# File lib/kontent-jekyll/utils/normalize_object.rb, line 14
def open_struct_values_to_hash(struct)
  hash = {}
  struct.each_pair do |key, value|
    hash[key] = to_hash(value)
  end
  hash
end
stringify_all_keys(object) click to toggle source
# File lib/kontent-jekyll/utils/normalize_object.rb, line 68
def stringify_all_keys(object)
  case object
  when Hash then stringify_all_keys_in_hash object
  when Array then stringify_all_keys_in_array object
  else object
  end
end
stringify_all_keys_in_array(array) click to toggle source
# File lib/kontent-jekyll/utils/normalize_object.rb, line 44
def stringify_all_keys_in_array(array)
  array.map do |item|
    case item
    when Hash then stringify_all_keys_in_hash item
    when Array then stringify_all_keys_in_array item
    else item end
  end
end
stringify_all_keys_in_hash(hash) click to toggle source
# File lib/kontent-jekyll/utils/normalize_object.rb, line 53
def stringify_all_keys_in_hash(hash)
  stringified_hash = {}

  hash.each do |k, v|
    stringified_hash[k.to_s] =
      case v
      when Array then stringify_all_keys_in_array v
      when Hash then stringify_all_keys_in_hash v
      else v
      end
  end

  stringified_hash
end
to_hash(object) click to toggle source
# File lib/kontent-jekyll/utils/normalize_object.rb, line 31
def to_hash(object)
  case object
  when OpenStruct
    open_struct_values_to_hash object
  when Array
    array_values_to_hash object
  when Hash
    hash_values_to_hash object
  else
    object
  end
end